我想填充窗體上的組合框。我有存儲過程來獲取我的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