我正在尝试从后面的代码绑定下拉框,但是我收到了编译错误:
'System.Web.UI.WebControls.SqlDataSource'不包含'DataSource'的定义
我试图搞清楚但似乎无法解决这个问题。
<asp:DropDownList ID="MYDDL" Width="300px" DataTextField="PRJ_TITLE" AutoPostBack="true"
DataValueField="PRJ_ID" runat="server">
</asp:DropDownList>
这是我的功能:
private void Bind_DD()
{
String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["myCon"].ConnectionString;
SqlConnection con2 = new SqlConnection(strConnString);
SqlDataAdapter sda = new SqlDataAdapter();
SqlCommand cmd1 = new SqlCommand("SELECT ID, PRJ_TITLE FROM myTable");
cmd1.Connection = con2;
con2.Open();
myDDL.DataSource = cmd1.ExecuteReader();
myDDL.DataTextField = "PRJ_TITLE";
myDDL.DataValueField = "ID";
myDDL.DataBind();
con2.Close();
}
答案 0 :(得分:4)
所以看起来你可能有点混淆sql数据连接,因为有很多方法可以做到这一点。我选择通过sqlDataAdapter对DataTable进行数据处理。
另外,请确保标记中没有任何元素在标记中的任何位置包含asp:SqlDataSource。
private void Bind_DD()
{
DataTable dt = new DataTable();
using(SqlConnection con2 = new SqlConnection(ConfigurationManager.ConnectionStrings["myCon"].ConnectionString))
{
con2.Open();
SqlCommand cmd1 = new SqlCommand("SELECT ID, PRJ_TITLE FROM myTable",con2);
SqlDataAdapter sda = new SqlDataAdapter(cmd1);
sda.Fill(dt);
}
myDDL.DataSource = dt;
myDDL.DataTextField = "PRJ_TITLE";
myDDL.DataValueField = "ID";
myDDL.DataBind();
}
答案 1 :(得分:1)
试试这个:
private void Bind_DD()
{
String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["myCon"].ConnectionString;
SqlConnection con2 = new SqlConnection(strConnString);
DataSet ds = new DataSet();
SqlCommand cmd1 = new SqlCommand("SELECT ID, PRJ_TITLE FROM myTable");
cmd1.Connection = con2;
con2.Open();
SqlDataAdapter sda = new SqlDataAdapter(cmd1);
sda.Fill(ds);
myDDL.DataSource = ds; //cmd1.ExecuteReader();
myDDL.DataTextField = "PRJ_TITLE";
myDDL.DataValueField = "ID";
myDDL.DataBind();
con2.Close();
//myDDL.DataBind();
}