我有一个winform应用程序,包含2个表单和一个数据库,form 1
用户在选择pictureboxes
之后从多个form2
中选择textboxes
并且labels
和form1
将根据form1
中的选择显示。
问题是,应用程序无法知道在 cc();
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select prumos from dbo.modelos";
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
object value;
try
{
value = dr["prumos"];
}
catch (IndexOutOfRangeException)
{
value = null;
}
string check = value != null ? value.ToString() : null;
textBox13.Visible = (check == "2" || check == "3");
textBox18.Visible = (check == "2" || check == "3");
textBox17.Visible = (check == "2" || check == "3");
textBox14.Visible = check == "3";
textBox16.Visible = check == "3";
textBox15.Visible = check == "3";
label16.Visible = (check == "2" || check == "3");
label20.Visible = check == "3";
}
else
{
}
dr.Close();
con.Close();
}
中选择了哪个项目来处理来自sql的信息。
在表中有3列id,description和prumos。
如何让应用程序识别出我选择了带有id x的图片框,这对应于" prumos"这样应用程序可以显示正确的文本框吗?
我感谢您的帮助的代码是:
Array (
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)
非常感谢你的帮助和时间
答案 0 :(得分:1)
更改
var check = dr["prumos"].ToString();
与
var check = dr[0].ToString();
我认为在您的DataReader中不存在' prumos'列,但是一个简单的文字'结果未找到'
试试吧
答案 1 :(得分:0)
我只是在猜测,因为问题中没有太多信息。 所以你真正想要实现的是: 仅获取某个查询的第一行并更改某些控件“的可见性,具体取决于列的值?
如果情况确实如此,那很简单:
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select prumos from dbo.modelos";
SqlDataReader dr = cmd.ExecuteReader();
if(dr.Read())
{
// if dr.Read() returns true we already know that dr.HasRows was also true
object value = null;
try
{
value = dr["prumos"];
}
catch(IndexOutOfRangeException) // thrown if there is no such column
{
}
string check = value != null ? value.ToString() : null;
textBox13.Visible = (check == "2" || check == "3");
textBox18.Visible = (check == "2" || check == "3");
textBox17.Visible = (check == "2" || check == "3");
textBox14.Visible = check == "3";
textBox16.Visible = check == "3";
textBox15.Visible = check == "3";
label16.Visible = (check == "2" || check == "3");
label20.Visible = check == "3";
}
else
{
// what to do if there is no row?
}
dr.Close();
con.Close();