【发布时间】:2020-11-17 00:52:07
【问题描述】:
我为 2 个组合框创建了一个相同的事件来填充我的输入控件,以便用户可以看到他们将要删除或更新的内容。当我制作单独的事件时,它起作用了,但在制作相同的事件时它不起作用。我应该在 if 语句中添加什么条件
如果条件 error 我得到 id "Index was outside the bounds of the array"
private void BoardComboBo(object sender, EventArgs e)
{
if (comboBox12.SelectedIndex!=0)//error is here
{
con.Open();
cmd = new SqlCommand("Select * from Users where user_Id='" + comboBox12.Text + "';", con);
SqlDataReader DR1 = cmd.ExecuteReader();
if (DR1.Read())
{
//code to fill textboxes
}
con.Close();
}
if(comboBox1.SelectedIndex!=0)//error is here
{
//to split combobox values
string selectedvalue = comboBox1.Text;
split = selectedvalue.Split();
//add values on fields
con.Open();
cmd = new SqlCommand("Select * from Users where firstName='" + split[0] + "' and lastName='" + split[1] + "';", con);
SqlDataReader DR1 = cmd.ExecuteReader();
if (DR1.Read())
{
//code to fill textboxes
}
con.Close();
}
}
我也改变了第二个 if 条件:
if (comboBox12.Text!=" ")// this works
{
//to split combobox values
string selectedvalue = comboBox1.Text;
split = selectedvalue.Split();
//add values on fields
con.Open();
cmd = new SqlCommand("Select * from Users where firstName='" + split[0] + "' and lastName='" + split[1] + "';", con);
SqlDataReader DR1 = cmd.ExecuteReader();
if (DR1.Read())
{
//code to fill textboxes
}
con.Close();
}
当我尝试按 user_Id
进行选择时,我现在遇到了同样的错误【问题讨论】:
-
“错误在这里” 错误是什么?
-
在这种情况下,我得到“索引超出了数组的范围”
-
既然你想让它做一些完全不同的事情,具体取决于哪个组合,你为什么要把它结合起来?此外,这从来都不是为 NET 应用程序构建 SQL 的正确方法——它非常容易出错,甚至很危险。
-
它填写相同的表格,一个按 id 填写,另一个按名字和姓氏填写,这就是为什么我认为合并事件会更好
标签: c# combobox selectedindexchanged