0

Hi I'm trying to populate a dynamic created comboBox using data from a database, but it shows an exception (Object reference not set to an instance of an object). And I'm tired of looking. What is missing here? Thanks For all the help! (newbie here)

private void SetComboBoxItems()
    {
        foreach (Control control in panelMain.Controls)
        {
            ComboBox comboBox = control as ComboBox;
            try
            {
                using (MySqlConnection connection = new MySqlConnection(Properties.Settings.Default.connectionString))
                {
                    connection.Open();
                    MySqlCommand command = new MySqlCommand("SELECT * FROM home.data", connection);
                    MySqlDataReader dataReader = command.ExecuteReader();
                    while (dataReader.Read())
                    {
                        comboBox.Items.Add((string)dataReader["temp"]);
                    }

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
Gabriel Vieira
  • 47
  • 1
  • 1
  • 7
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Ňɏssa Pøngjǣrdenlarp Feb 22 '19 at 20:18
  • 1
    Why are you looping for the ComboBox? Give the ComboBox a name when you create it and then you can reference it from the container's collection property. Example: `var cb = panelMain.Controls[comboName] as ComboBox;` Use the ContainsKey function on the controls collection to verify the name exists. – LarsTech Feb 22 '19 at 20:21

1 Answers1

2

Your question is a little vague but as a guess I'd say that you're iterating though all of the controls in panelMain.Controls and trying to cast them to ComboBox. Any that aren't ComboBox, I can't remember any off the top of my head, will be null. You don't check for null but still make the DB call that then fails when you try to set the items. I's suggest the following:

foreach (Control control in panelMain.Controls)
        {
            ComboBox comboBox = control as ComboBox;
            if(comboBox != null){
                try
                {

or

foreach (Control control in panelMain.Controls.Where(c => c is ComboBox))
Orbittman
  • 306
  • 1
  • 6
  • Thanks a lot Orbittman! I tried both of them and **the first is the solution**. About the second… unfortunately it doesn't work (because panelMain.Controls.Where() don't exist in my VS). Tanks again XD – Gabriel Vieira Feb 22 '19 at 20:34
  • 2
    @GabrielVieira That line is wrong. It should read something like this: `foreach (ComboBox cb in panelMain.Controls.OfType())` – LarsTech Feb 22 '19 at 20:38
  • This will catch all ComboBoxes though and @LarsTech's solutions will fix that – Orbittman Feb 22 '19 at 20:39
  • Sorry it was just typed in the comment box, it was more to get the idea across. `var bob = new List { "hello", 1, 2, new DateTime()}; foreach (var fred in bob.Where(c => c is string)) { // ... }` – Orbittman Feb 27 '19 at 08:37