Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ADO.NET Events Forms
Hi everyone!
 
I have several combo-boxes that are supposed to contain information about different aspects of vehicles. Sometimes the combo-boxes may only have 1 element in them. I've been asked to set the default value of the combo-box to that element if there is only 1 in there. My question is how can I prevent an event from being skipped to set a value of a combo-box to that one element.
 
I have one vehicle that has 3 combo-boxes that all have 1 element in them. This code fires the 1st and the 3rd one, but skips the 2nd one. Can someone shed some light as to what I'm missing? I'm not getting any errors, it's just not filling in the engine-combobox in when there is one element in that combobox. It fills in the year and the transmissionCode ones just fine.
private void modelComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Sets year combobox to true and all others to false when
            // selected index changed event changes the existing value.
            yearComboBox.Enabled = true;
            engineCodeComboBox.Enabled = false;
            transmissionCodeComboBox.Enabled = false;
            okButton.Enabled = false;
 
            // Sets currently disabled comboboxes to have null values
            // and waits for further action from end-user.
            yearComboBox.Text = "";
            engineCodeComboBox.Text = "";
            transmissionCodeComboBox.Text = "";
 
            // Clears out items in comboboxes and waits for a new filtered
            // query with results.
            yearComboBox.Items.Clear();
            engineCodeComboBox.Items.Clear();
            transmissionCodeComboBox.Items.Clear();
 
            if (modelComboBox.SelectedIndex == -1)
            {
                yearComboBox.Enabled = false;
                engineCodeComboBox.Enabled = false;
                transmissionCodeComboBox.Enabled = false;
            }
 
            // Query to select non-repeating years (filtering on selected make and model)
            SqlCeCommand year_comm = new SqlCeCommand("SELECT DISTINCT year FROM vehicles WHERE make = @make AND model = @model ORDER BY year DESC", conn);
            
            year_comm.CommandType = CommandType.Text;
            year_comm.Parameters.AddWithValue("@make", makeComboBox.Text);
            year_comm.Parameters.AddWithValue("@model", modelComboBox.Text);
            year_comm.ExecuteNonQuery();
 
            try
            {
                SqlCeDataReader year_dr = year_comm.ExecuteReader();
                while (year_dr.Read())
                {
                    // Fills year combobox with results from query
                    yearComboBox.Items.Add(year_dr["year"]);
                }
                                                
                if (yearComboBox.Items.Count == 1)
                {
                    yearComboBox.SelectedIndex = 0;
                }                                
                                
                year_dr.Close();
                year_dr.Dispose();
            }
            catch (SqlCeException ex)
            {
                MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                year_comm.Dispose();
            }
        }
 
        // Listener for yearcombobox selectedindexchanged event and loads the engine
        // combobox with engine type information
        private void yearComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Sets engine and transmission boxes to true.  OK button remains false until all 5 boxes have non null values.
            engineCodeComboBox.Enabled = true;
            transmissionCodeComboBox.Enabled = true;
            okButton.Enabled = false;
 
            // Should the selected index value change, these 2 boxes would
            // be reset to null values and wait for further selections.
            engineCodeComboBox.Text = "";
            transmissionCodeComboBox.Text = "";
 
            // Clears / Resets items in boxes and waits for new selections.
            engineCodeComboBox.Items.Clear();
            transmissionCodeComboBox.Items.Clear();
 
            if (yearComboBox.SelectedIndex == -1)
            {
                engineCodeComboBox.Enabled = false;
                transmissionCodeComboBox.Enabled = false;
            }
 
            SqlCeCommand engine_comm = new SqlCeCommand("SELECT DISTINCT engine_code FROM vehicles WHERE make = @make AND model = @model AND year = @year", conn);
 
            engine_comm.CommandType = CommandType.Text;
            engine_comm.Parameters.AddWithValue("@make", makeComboBox.Text);
            engine_comm.Parameters.AddWithValue("@model", modelComboBox.Text);
            engine_comm.Parameters.AddWithValue("@year", yearComboBox.Text);
            engine_comm.ExecuteNonQuery();
 
            try
            {
                SqlCeDataReader engine_dr = engine_comm.ExecuteReader();
                while (engine_dr.Read())
                {
                    engineCodeComboBox.Items.Add(engine_dr["engine_code"]);                    
                }
 
                if (engineCodeComboBox.Items.Count == 1)
                {
                    engineCodeComboBox.SelectedIndex = 0;
                }
 
                engine_dr.Close();
                engine_dr.Dispose();
            }
            catch (SqlCeException ex)
            {
                MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                engine_comm.Dispose();
            }
        }
 
        // Listener for enginecombobox selectedindexchanged event and loads the transmission
        // combobox with transmission type information
        private void engineCodeComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            transmissionCodeComboBox.Enabled = true;
            okButton.Enabled = false;
            engineCodeComboBox.Text = "";
            transmissionCodeComboBox.Text = "";
            transmissionCodeComboBox.Items.Clear();
 
            // Query to select transmission type (based on previously selected make, model and year)
            SqlCeCommand trans_comm = new SqlCeCommand("SELECT DISTINCT transmission_code FROM vehicles WHERE make = @make AND model = @model AND year = @year AND engine_code = @engine_code", conn);
 
            trans_comm.CommandType = CommandType.Text;
            trans_comm.Parameters.AddWithValue("@make", makeComboBox.SelectedItem);
            trans_comm.Parameters.AddWithValue("@model", modelComboBox.SelectedItem);
            trans_comm.Parameters.AddWithValue("@year", yearComboBox.SelectedItem);
            trans_comm.Parameters.AddWithValue("@engine_code", engineCodeComboBox.SelectedItem);
            trans_comm.ExecuteNonQuery();
 
            try
            {
                SqlCeDataReader trans_dr = trans_comm.ExecuteReader();
                while (trans_dr.Read())
                {
                    // Fills trans combobox with results from query
                    transmissionCodeComboBox.Items.Add(trans_dr["transmission_code"]);
                }
 
                if (transmissionCodeComboBox.Items.Count == 1)
                {
                    transmissionCodeComboBox.SelectedIndex = 0;
                }      
 
                trans_dr.Close();
                trans_dr.Dispose();
            }
            catch (SqlCeException ex)
            {
                MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                trans_comm.Dispose();
            }
        }
Posted 15-Oct-12 9:38am
Edited 15-Oct-12 10:41am
v3
Comments
Wes Aday at 15-Oct-12 14:49pm
   
Okay... what does " not filling in the engine-combobox in when there is one element in that combobox" mean exactly? Does your yearComboBox_SelectedIndexChanged code run at all? Do you get results back from your query? What do you see happening when you step through your code?
joshrduncan2012 at 15-Oct-12 14:53pm
   
What I meant to say is that I get results from all of the queries. If there is one element for that combobox, I want to set the SelectedIndex to 0. The SelectedIndex = 0 works for yearComboBox and transmissionCodeComboBox, but not engineCodeComboBox. EngineCodeCombox, however, does have a choice to select, though.
Sergey Alexandrovich Kryukov at 15-Oct-12 15:40pm
   
First of all, you need to tag the application type or UI library you use. WPF? Forms? ASP.NET? Silverlight? Metro? what?
--SA
joshrduncan2012 at 15-Oct-12 15:41pm
   
Thanks, I added Forms and ADO.NET.
Sergey Alexandrovich Kryukov at 15-Oct-12 17:19pm
   
Thank you for tagging it. It's good for you and should always be done.
And now, I'm not sure what is that skipping. First thing to do is to put break points on the beginning of each even handler and see what it does under debugger. Is some are not invoked at all, set a break point on each += operators of all event instances in question -- what is some handlers are never actually added to the invocation lists? This is all pretty simple; and you always come to figure out what's wrong.
--SA
geekbond at 17-Oct-12 6:53am
   
I do not understand what you mean by skipping an event, can you please explain what you really want?
joshrduncan2012 at 22-Oct-12 17:10pm
   
What is happening is that I have 3 comboboxes that have auto-fill-in if there is only one element in that list. I have the combobox.selectedindex = 0 in the same place in all 3 combobox events and it is skipping one. I can't tell why it is skipping and it's frustrating me to no end. :(
joshrduncan2012 at 23-Oct-12 9:34am
   
The one that is being skipped is just skipping on the selectedindex event. All of the other events work on that one no problem. Any other ways of setting the first element of the list to be the selectedvalue, other than selectedindex, if possible?
Daxa Rughani at 23-Oct-12 16:39pm
   
You have seem to have 4 comboboxes = model,year,engineCode and transmissionCode but no eventhandler for transmissionCode.
Just a thought engineCodeComboBox.Text="" in the engineCodeComboBox event handler does this not overwrite the value added by year eventhandler.
joshrduncan2012 at 23-Oct-12 16:43pm
   
That did the trick! Thanks Daxa! :)

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

  Print Answers RSS
0 OriginalGriff 200
1 Kamal Rocks 184
2 PIEBALDconsult 150
3 BillWoodruff 148
4 Jochen Arndt 135
0 OriginalGriff 5,695
1 DamithSL 4,506
2 Maciej Los 4,007
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,190


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 15 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100