我想填充窗体上的组合框。我有存储过程来获取我的ComboBoxes的数据。我的代码如下。问题是我的组合框没有被填充。当我遍历代码时,它会到达“reader = sc.ExecuteReader();”并且不会执行Pop_ComboBox()中的任何后续行。存储过程返回“ID”和“Description”(仅)。我究竟做错了什么?未从存储过程填充组合框
private void frmInput_Load(object sender, EventArgs e)
{
//Connect to the db
this.sqlConn = new SqlConnection("Server=\"our_server";Database=\"Astra\";Trusted_Connection=yes;");
//Populate the Equipment Type Listbox
Pop_ComboBox("exec uspASTRA_GetEquipTypeList;", cboEquipType);
}
private void Pop_ComboBox(string sQuery, ComboBox NameOfcbo)
{
SqlCommand sc = new SqlCommand(sQuery, this.sqlConn);
SqlDataReader reader;
reader = sc.ExecuteReader();
DataTable dt = new DataTable();
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("Description", typeof(string));
dt.Load(reader);
NameOfcbo.ValueMember = "ID";
NameOfcbo.DisplayMember = "Description";
NameOfcbo.DataSource = dt;
}
您可能会在调用ExecuteReader时发生异常。在Visual Studio中,进入'Debug-> Exceptions',检查一切并再次调试。或者将调用放入try-catch块并查看异常。 –
你还没有打开你的连接...... !! this.sqlConn.Open(); –
@Azhar - 感谢您的支持!并感谢达里奥的建议。我现在看到我的SqlConn工作不正常。 – johncroc