Click here to Skip to main content
15,886,963 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how can i get data in textboxes from database if my combobox selected values are from table1 and the values i want to get in textboxes are from table2 ?


i am getting name of just 1 subject in all textboxes i want all subject to show in textboxes after selecting class from combox

What I have tried:

private void Result_Load(object sender, EventArgs e)
       {

           string query = "SELECT  class_id , class_name FROM class";
           fillcombo(comboBox3, query, "class_name", "class_id");
          // comboBox3_SelectedIndexChanged(null, null);

       }
       public void fillcombo(ComboBox combo, string query, string displayMember, string valueMember)
       {
           command = new SqlCommand(query, con);
           adapter = new SqlDataAdapter(command);
           table = new DataTable();
           adapter.Fill(table);
           combo.DataSource = table;
           combo.DisplayMember = displayMember;
           combo.ValueMember = valueMember;
       }



private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
       {

           con.Open();
           int val;
           Int32.TryParse(comboBox3.SelectedValue.ToString(), out val);
           //string query = "SELECT  sub_name, class_id FROM subject  WHERE class_id = " + val;
           string query = "SELECT dbo.Subject.sub_name, dbo.Class.class_name FROM     dbo.Class full JOIN dbo.Subject ON dbo.Class.class_id = dbo.Subject.class_id where Class.class_id="+val;
           //string query = "SELECT sub_name FROM subject";


          SqlCommand command = new SqlCommand(query, con);

           using (var reader = command.ExecuteReader())
           {
               //SqlDataReader sdr = command.ExecuteReader();
               if (reader.Read())
               {


                   textBox1.Text = reader["sub_name"].ToString();
                   textBox2.Text = reader["sub_name"].ToString();
                   textBox3.Text = reader["sub_name"].ToString();
                   textBox4.Text = reader["sub_name"].ToString();
                   textBox5.Text = reader["sub_name"].ToString();
                   textBox7.Text = reader["sub_name"].ToString();
                   textBox8.Text = reader["sub_name"].ToString();
                   textBox9.Text = reader["sub_name"].ToString();
                   textBox10.Text = reader["sub_name"].ToString();
                   textBox11.Text = reader["sub_name"].ToString();




               }
               reader.Close();
           }
           con.Close();
       }
Posted
Updated 4-Mar-18 20:35pm
Comments
BillWoodruff 3-Mar-18 20:37pm    
1. why use TextBoxes to display data the user does not edit ... or do the contents ? consider Labels ?

2. you want to DataBind each TextBox to an element of the collection of objects returned from your query ?

You keep moving the same column ("sub_name") to the different text boxes.

You need to:

1) Use different names when accessing different columns
2) Read more than one row, if needed ... looks like a join.
 
Share this answer
 
Take one counter variable and depending counter value assign subject name to textbox
as example below


Int32 rowcounter = 0;

using (var reader = command.ExecuteReader())
{
//SqlDataReader sdr = command.ExecuteReader();
if (reader.Read())
{


if(rowcounter == 0 )
{
textBox1.Text = reader["sub_name"].ToString();
}
else if(rowcounter == 1 )
{
textBox2.Text = reader["sub_name"].ToString();
}
else if(rowcounter == 2 )
{
textBox3.Text = reader["sub_name"].ToString();
}
else if(rowcounter == 3 )
{
textBox4.Text = reader["sub_name"].ToString();
}
else if(rowcounter == 4 )
{
textBox5.Text = reader["sub_name"].ToString();
}
else if(rowcounter == 5 )
{
textBox6.Text = reader["sub_name"].ToString();
}

.
.
.



rowcounter++;


}
reader.Close();
}
con.Close();
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900