大家快来帮我看看这段代码 dropdownlist绑定 无数据显示 C# .net
SqlConnection sqlcon = new SqlConnection();
string connstr = @"Data Source=.\SQLEXPRESS;AttachDBFilename=|DataDirectory|\xiangdan.mdf;Integrated Security=True;User Instance=True";
sqlcon.ConnectionString = connstr;
SqlCommand cmd = new SqlCommand();
cmd.Connection = sqlcon;
cmd.CommandText = "select*from xiangdan";
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = cmd;
DataSet bown = new DataSet();
adapter.Fill(bown);
DataTable dt = bown.Tables["xiangdan"];
DropDownList1.DataSource = dt;
DropDownList1.SelectedValue = "nianji";
DropDownList1.DataValueField = "xueyuan";
DropDownList1.DataTextField = "banji";
DropDownList1.DataBind();
Response.Write("this.DropDownList1.DataTextField = 'banji'");
------解决方案--------------------
C# code
string connstr = @"Data Source=.\SQLEXPRESS;AttachDBFilename=|DataDirectory|\xiangdan.mdf;Integrated Security=True;User Instance=True";
DataSet bown = new DataSet();
using(SqlConnection sqlcon = new SqlConnection(connstr))
{
string strSQL = "select * from xiangdan";
using(SqlDataAdapter adapter = new SqlDataAdapter(strSQL,sqlcon))
{
adapter.Fill(bown);
DropDownList1.DataSource = bown;
DropDownList1.DataTextField = "banji";
DropDownList1.DataValueField = "xueyuan";
DropDownList1.DataBind();
// DropDownList1.SelectedValue = "nianji";
}
}