MarcIT MarcIT - 29 days ago 7
SQL Question

check value from SQL to enable textbox and label

I have a WinForm app in C# in witch i need to hide some textboxes and labels depending on the item selected on the previous form.
I disabled the textboxes and labels in the designer and would make them visible again depending on a value from a SQL table.
The code i have is this one but i cant figure out how to check the value.

con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select prumos from dbo.modelos";
cmd.ExecuteNonQuery();
if () ; //database value prumos =2
{
textBox13.Visible = true;
textBox18.Visible = true;
textBox17.Visible = true;
label16.Visible = true;
return;
}
else if () ; //database value prumos = 3
{
textBox13.Visible = true;
textBox18.Visible = true;
textBox17.Visible = true;
textBox14.Visible = true;
textBox16.Visible = true;
textBox15.Visible = true;
label16.Visible = true;
label20.Visible = true;
return;
}


Can anyone help please,

Thanks in advance.

Answer
 con.Open();
    SqlCommand cmd = con.CreateCommand();
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = "select prumos from dbo.modelos";
    int value = (int)cmd.ExecuteScalar();
    if (value == 2) ; //database value prumos =2
    {
        textBox13.Visible = true;
        textBox18.Visible = true;
        textBox17.Visible = true;
        label16.Visible = true;
        return;
    }
    else if (value == 3) ; //database value prumos = 3 
    {
        textBox13.Visible = true;
        textBox18.Visible = true;
        textBox17.Visible = true;
        textBox14.Visible = true;
        textBox16.Visible = true;
        textBox15.Visible = true;
        label16.Visible = true;
        label20.Visible = true;
        return;
    }

You could do something similar to what I have shown Above by editing you code. By using ExecuteScalar() will return you object you can cast it to int or string as per your requirement or the value that is coming from the database and use it in the way I have mentioned above.