繁体   English   中英

C#显示文本框取决于SQL值

[英]c# show textbox depending on sql value

我有2种形式和DB的winform应用程序,在form 1的用户从多个选择pictureboxes作出选择后form2打开和一些textboxeslabels将出现根据作出的选择form1 问题是,应用程序无法知道在form1中选择了哪个项目来处理来自sql的信息。 表格中有3列ID,说明和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();
    }

例

非常感谢您的帮助和时间

更改

var check = dr["prumos"].ToString();

var check = dr[0].ToString();

我认为您的DataReader中不存在“ prumos”列,而是一个简单的文本“找不到结果”

试试吧

我只是在猜测,因为问题中没有太多信息。 因此,您真正想要实现的是:仅获取某些查询的第一行,并“根据列的值”更改某些控件的可见性?

好吧,如果是这样,那就很容易了:

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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM