无法在数据库的下拉列表中发布数据

时间:2016-03-10 11:14:16

标签: c# asp.net sql-server drop-down-menu

我是编写代码的新手。尝试从DB下拉列表中发布数据时出现以下错误。 我的目标是:从数据库中下载列表中发布数据。

  

错误消息:进程因为正在使用而无法访问该文件   通过另一个过程

enter image description here

using System.Data;
using System.Configuration;



public partial class Default : System.Web.UI.Page
{
    //SqlCommand cmd;
    SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;Database=Database1;AttachDbFilename=C:\Database1.mdf;Integrated Security=True;User Instance=True");


    protected void Page_Load(object sender, EventArgs e)
    {
        Populate1();

    }


    protected void Button1_Click1(object sender, EventArgs e)
    {

        /* SqlCommand cmd = new SqlCommand("insert into service_type (type) values(' " + TextBox1.Text + " ')",
             new SqlConnection(ConfigurationManager.AppSettings["ConnString"]));

         cmd.Connection.Open(); 
         cmd.Connection.Close();
         cmd.Connection.Dispose(); */

        con.Open();
         SqlCommand cmd = con.CreateCommand();
         cmd.CommandType = CommandType.Text;
         cmd.CommandText = "insert into service_type (type) values(' " + TextBox1.Text + " ')";

         cmd.ExecuteNonQuery();
         con.Close();
    }

    public void Populate1()
    {

        con.Open();


        SqlCommand cmd = new SqlCommand("SELECT * FROM [serice_type]", new SqlConnection(ConfigurationManager.AppSettings["ConnString"]));
       cmd.Connection.Open();

        SqlDataReader ddlValues;
        ddlValues = cmd.ExecuteReader();

        DropDownList1.DataSource = ddlValues;
        DropDownList1.DataValueField = "theType";
        DropDownList1.DataTextField = "theType";
        DropDownList1.DataBind();

        cmd.Connection.Close();
        cmd.Connection.Dispose();
    }



}

0 个答案:

没有答案