C#MDI can';t在子窗体上填充列表框
我正试图从SQLServer中的一个表填充C#中的一个列表框。该表设置为: tblTables 表ID int 座位容量 电流容量int 我尝试使用的代码是:C#MDI can';t在子窗体上填充列表框,c#,sql-server,listbox,mdi,C#,Sql Server,Listbox,Mdi,我正试图从SQLServer中的一个表填充C#中的一个列表框。该表设置为: tblTables 表ID int 座位容量 电流容量int 我尝试使用的代码是: private void fillListBox() { String connectionString = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString; SqlConnection conn
private void fillListBox()
{
String connectionString = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;
SqlConnection conn = new SqlConnection(connectionString);
SqlDataAdapter da = new SqlDataAdapter();
String commandString = "SELECT TableID, SeatingCapacity, CurrentCapacity from tblTables";
SqlCommand cmd = new SqlCommand();
SqlDataReader dr;
conn.Open();
cmd.CommandText = commandString;
cmd.Connection = conn;
dr = cmd.ExecuteReader();
lstTableList.Items.Clear();
lstTableList.BeginUpdate();
while (dr.Read())
{
lstTableList.Items.Add(dr.GetInt32(0) + " - " + dr.GetInt32(1) + " - " + dr.GetInt32(2));
}
lstTableList.EndUpdate();
dr.Close();
conn.Close();
}
要转移的父窗体的代码为:
private void mnuFormsTables_Click(object sender, EventArgs e)
{
frmTables aTables = new frmTables();
aTables.MdiParent = this;
aTables.Show();
aTables.Focus();
}
我调用fillListBox()是frmTables的表单加载。然而,当我把一个断点放在
String connectionString = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;
它显示在该行之后,代码立即跳转到
aTables.Show();
任何帮助都将不胜感激。我也不确定这是否是填充列表框的正确方法。如果列表框可以显示表格ID,并在其旁边添加“Full”(如果当前容量>=座位容量),则最好是这样。
此文件的github repo URL为
编辑:如果我使用:
String connectionString = Properties.Settings.Default.StaufferRestaurantConnectionString;
它不使用ConfigurationManager,而是工作。我有System.Configuration.dll作为参考和正确的using语句,因此我不知道它为什么不能正常工作。您可能在app.settings中拼写了connectionString。这将解释为什么会出现错误,以及为什么很难找到它,因为当您查看代码时,一切看起来都很好。我现在使用的硬编码字符串是直接从app.config复制的。我的意思是,您可能拼写错了app.config中的connectionString键,可能是ConctionString而不是connectionString。对不起,我不清楚。