combobox depend another combobox problem

Feb 25 2018 10:54 AM
I wanna select from first combobox id of a train and in the second combobox give me the wagon that train has (number of a wagon ) I did a database: that first table stored the train id and the second table stored wagon id + id train foreign key i tried this code and its work but when i choose another id it doesn't change the wagon combobox result
 
What I have tried:
  1. adapter = new SqlDataAdapter("select * from train", cn);  
  2. SqlCommand cmd = new SqlCommand("select * from train", cn);  
  3. cmd.Parameters.Clear();  
  4. adapter.Fill(ds, "train");  
  5. comboBox1.DataSource = ds.Tables["train"];  
  6. comboBox1.DisplayMember = "id";  
  7. comboBox1.ValueMember = "id";  
  8. adapter = new SqlDataAdapter("select id_w from wagon where id='" + comboBox1.SelectedValue + "'", cn);  
  9. adapter.Fill(ds, "wagon");  
  10. // adapter1.SelectCommand.Parameters.Clear();  
  11. comboBox2.DataSource = ds.Tables["wagon"];  
  12. comboBox2.DisplayMember = "id_w";  
  13. comboBox2.ValueMember = "id_w";  

Answers (1)