two comboBoxs
-
I have a C# and Silverlight project with two combo boxes (CB1, CB2)
-CB1 is initialized and populated with few items.
(e.g. car, plane, motorcycle)
-User selects an item (e.g. car) and CB2 gets automatically populated
(e.g. model, year, color) via the cb1_SelectionChanged()
-user selects the item from CB2 (e.g. model) and a datagid is populated accordingly.Problem:
Once I selected item from CB2 and datagrid is populated correctly, attempting to select a different item from CB1 generates a “Object reference not set to an instance of an object”
To go around just select a different item then back again to the item and that woks but is really annoying. :)
Any ideas will be greatly appreciated -
I have a C# and Silverlight project with two combo boxes (CB1, CB2)
-CB1 is initialized and populated with few items.
(e.g. car, plane, motorcycle)
-User selects an item (e.g. car) and CB2 gets automatically populated
(e.g. model, year, color) via the cb1_SelectionChanged()
-user selects the item from CB2 (e.g. model) and a datagid is populated accordingly.Problem:
Once I selected item from CB2 and datagrid is populated correctly, attempting to select a different item from CB1 generates a “Object reference not set to an instance of an object”
To go around just select a different item then back again to the item and that woks but is really annoying. :)
Any ideas will be greatly appreciatedPost the piece of code for better understanding...
-
Post the piece of code for better understanding...
Yes, I should have done fist. Thank you public Home() { InitializeComponent(); //initilize the combobox comboBox1.Items.Add("CAR"); comboBox1.Items.Add("PLANE"); comboBox1.Items.Add("MOTORCYCLE"); for CB1 private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e) { string selec = comboBox1.SelectedItem.ToString(); switch (selec) { case "CAR": { comboBox2.Items.Clear(); label3.Content = "Select cars"; comboBox2.Items.Add("Model"); comboBox2.Items.Add("Color"); } break; case "PLANE": { comboBox2.Items.Clear(); label3.Content = "Select Site"; comboBox2.Items.Add("model"); comboBox2.Items.Add("EngineType"); comboBox2.Items.Add("choice 1"); comboBox2.Items.Add("choice 2"); } break; }//end of switch } FOR CB2 private void comboBox2_SelectionChanged(object sender, SelectionChangedEventArgs e) { EclipseMTXDomainContext context1 = new EclipseMTXDomainContext(); //load datagrid from database string selec = comboBox2.SelectedItem.ToString(); switch (selec) { case "model": { //GET ALL MOdels dataGrid1.ItemsSource = context1.Protons; context1.Load(context1.GetProtonsFilteredQuery(1)); } break; case "EngineType": { //GET ALL Engine Types dataGrid1.ItemsSource = context1.Protons; context1.Load(context1.GetProtonsFilteredQuery(1)); } break; }//end of switch }
-
Yes, I should have done fist. Thank you public Home() { InitializeComponent(); //initilize the combobox comboBox1.Items.Add("CAR"); comboBox1.Items.Add("PLANE"); comboBox1.Items.Add("MOTORCYCLE"); for CB1 private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e) { string selec = comboBox1.SelectedItem.ToString(); switch (selec) { case "CAR": { comboBox2.Items.Clear(); label3.Content = "Select cars"; comboBox2.Items.Add("Model"); comboBox2.Items.Add("Color"); } break; case "PLANE": { comboBox2.Items.Clear(); label3.Content = "Select Site"; comboBox2.Items.Add("model"); comboBox2.Items.Add("EngineType"); comboBox2.Items.Add("choice 1"); comboBox2.Items.Add("choice 2"); } break; }//end of switch } FOR CB2 private void comboBox2_SelectionChanged(object sender, SelectionChangedEventArgs e) { EclipseMTXDomainContext context1 = new EclipseMTXDomainContext(); //load datagrid from database string selec = comboBox2.SelectedItem.ToString(); switch (selec) { case "model": { //GET ALL MOdels dataGrid1.ItemsSource = context1.Protons; context1.Load(context1.GetProtonsFilteredQuery(1)); } break; case "EngineType": { //GET ALL Engine Types dataGrid1.ItemsSource = context1.Protons; context1.Load(context1.GetProtonsFilteredQuery(1)); } break; }//end of switch }
picasso2 wrote:
string selec = comboBox1.SelectedItem.ToString();
Comboboxes use to send two SelectionChanged events when the user changes the selection: in the first event, the previously selected item is un-selected, at this moment no item is selected, comboBox1.SelectedItem is null, and consequently comboBox1.SelectedItem.ToString(); causes a NullReferenceException. Then the new item gets selected and SelectedItem has a value again. Solution: check SelectedItem for null.