【发布时间】:2017-03-30 09:20:42
【问题描述】:
我有一个带有 2 个表单和一个 DB 的 winform 应用程序,在 form 1 中,用户从几个 pictureboxes 中进行选择后,form2 打开,一些 textboxes 和 labels 将根据在form1 中进行的选择。
问题是,应用程序无法知道在form1 中选择了哪个项目来处理来自 sql 的信息。
表中有 3 列 id、description 和 prumos。
如何让应用程序识别出我选择了 id x 的图片框,它对应于 x 个“prumos”,以便应用程序可以显示正确的文本框?
感谢您的帮助,我得到的代码已经是:
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();
}
非常感谢您的帮助和时间
【问题讨论】:
-
为什么
if和else if的条件一样? -
我必须用它来检查值 2 或 3。如果我删除该行,我会收到错误:当前上下文中不存在名称“检查”但我可能错了: \
-
如果您使用 Read() 应用程序将无法访问数据库是什么意思?您是否尝试过 while(dr.Read()){}?
-
我有一些从数据库中以该形式加载的值,但如果我使用 dr.read 这些值将不会加载。如果我删除它们出现但没有文本框规则的行
-
如果 (dr.HasRows) 失败,你的 else 部分总是会抛出异常,var check = dr["prumos"].ToString();在 else 部分会抛出异常。
标签: c# sql winforms if-statement textbox