简体   繁体   English

使用来自SQL Server的数据填充ComboBox

[英]Fill ComboBox with data from SQL Server

I want to fill a ComboBox with data from sql server and I use the code below, but it doesn't work: 我想用来自SQL Server的数据填充一个ComboBox,我使用下面的代码,但是它不起作用:

protected void FillComboBox()
{
    SqlDataAdapter da = new SqlDataAdapter("SELECT ItemName FROM TBItemList", oSqlConn);
    DataTable dt = new DataTable();
    da.Fill(dt);
    {
         cbxparameter.Items.Add(dt.ToString());
    }
}

Then to make sure my code is okay I'm trying to debug, but I can't know where the trouble lies. 然后,要确保我的代码正常,我正在尝试调试,但是我不知道麻烦在哪里。 Why? 为什么?

SqlDataAdapter da = new SqlDataAdapter("SELECT ItemName FROM TBItemList", oSqlConn);
DataTable dt = new DataTable();
da.Fill(dt);
cbxparameter.DataSource = dt;
cbxparameter.DisplayMember = "ItemName";
cbxparameter.ValueMember = "ItemName";
SqlCommand sqlCmd = new SqlCommand(sql, sqlCon);
                sqlCmd.CommandTimeout = 0;
                SqlDataReader dr;
                dr = sqlCmd.ExecuteReader();
                MyDDLst.Items.Insert(0, "--Select--");
                while (dr.Read())
                    MyDDLst.Items.Add(dr[0].ToString());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM