Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone.

I am trying to populate my text boxes with the values in my database so when a user selects a name of a teacher from the combobox, the text-boxes will populate with their contact details. This is the code I have so far. There are no errors however the textboxes are still blank when I select a value from combo box.

private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
      {
          MySqlConnection cs = new MySqlConnection(connectionSQL);
          cs.Open();

          DataSet ds = new DataSet();

          MySqlDataAdapter da = new MySqlDataAdapter("Select * from Teacher WHERE name='" + comboBox1.Text + "'", cs);

          MySqlCommandBuilder cmd = new MySqlCommandBuilder(da);

          da.Fill(ds);


          if (comboBox1.SelectedIndex > 0)
          {


              NameBox.Text = ds.Tables[0].Rows[0]["name"].ToString();
              AddressBox.Text = ds.Tables[0].Rows[0]["address"].ToString();

          }

Any help or advice would be greatly appreciated


UPDATED CODE:
private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
MySqlConnection cs = new MySqlConnection(connectionSQL);

string query = "SELECT name FROM Teacher WHERE name= 'Liz Murphy'";
MySqlDataAdapter adapter = new MySqlDataAdapter(query, cs);
DataSet DS = new DataSet();

adapter.Fill(DS);

NameBox.Text = DS.Tables[0].Rows[0].ToString();
Posted
Updated 3-Apr-13 9:46am
v4
Comments
Richard C Bishop 3-Apr-13 12:01pm    
I think you want to use "comboBox1.SelectedValue" when you query the database.
Member 9611735 3-Apr-13 12:05pm    
Thank you. I changed my code but the textboxes are still not being populated
Richard C Bishop 3-Apr-13 12:07pm    
Is your dataset getting filled with data?
Member 9611735 3-Apr-13 12:15pm    
I just checked and it's not. Do you think there is something wrong with my query?
Richard C Bishop 3-Apr-13 12:23pm    
Check my solution below and see if you can get that to work.

Try something like this:
MySqlConnection cs = new MySqlConnection(connectionSQL);

string query = "SELECT * FROM Teacher WHERE name= '" + comboBox1.SelectedValue + "'";
adapter = new MySqlDataAdapter(query, cs);
DataSet DS = new DataSet();

adapter.Fill(DS);

You will have to alter how you get your textboxes filled after the query.
 
Share this answer
 
v5
PROBLEM SOLVED!!! I hadn't assigned combobox selectedValuePath when I was populating combo box. Many thanks to richcb for helping me
 
Share this answer
 
Comments
Richard C Bishop 3-Apr-13 17:29pm    
You are welcome.

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