MarcIT MarcIT - 20 days ago 6
SQL Question

c# show textbox depending on sql value

I have an winform app with 2 forms and a DB, in

form 1
the user selects from several
pictureboxes
after the selection is made
form2
opens and some
textboxes
and
labels
will appear according to the selection made in
form1
.
the problem is, that there is no way for the app to know what item was selected in
form1
to process the information from sql.
In the table there are 3 columns id, description and prumos.
how can i make the app recognize that i selected the picturebox with the id x, that corresponds to x number of "prumos" so that the app can display the correct textboxes?

the code i have thanks to your help already is:

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


thanks very much for your help and time

Answer

I'm just guessing, because there isn't much information in the question. So what you really want to achieve is: Fetch only the first row of some query and change the visibility of some controls "depending on the value of a column"?

Well if that's the case then it's easy:

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