If statement inside of foreach loop.

I have the following statement to save information to an Access db:

private void testButton_Click(object sender, EventArgs e)
        {

            try
            {
                //Create OleDB connection
                OleDbConnection connection = new OleDbConnection();
                connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0; Data Source=MyDb;
Persist Security Info=False;";

                //connect to DB
                connection.Open();

                //Setup 
                var ComboBoxList = Controls.OfType<ComboBox>();
                OleDbCommand command = new OleDbCommand();
                command.Connection = connection;

                //loop
                foreach (ComboBox taskComboBox in ComboBoxList)
                {
                    //Setup loop to assigned comboxes to corresponding stopwatch.
                    switch (taskComboBox.Name)
                    {
                        case "taskcombobox1":
                            stopwatch = stopwatch1;
                            break;
                        case "taskcombobox2":
                            stopwatch = stopwatch2;
                            break;
                        case "taskcombobox3":
                            stopwatch = stopwatch3;
                            break;
                        case "taskcombobox4":
                            stopwatch = stopwatch4;
                            break;
                        case "taskcombobox5":
                            stopwatch = stopwatch5;
                            break;
                        case "taskcombobox6":
                            stopwatch = stopwatch6;
                            break;
                        case "taskcombobox7":
                            stopwatch = stopwatch7;
                            break;
                        case "taskcombobox8":
                            stopwatch = stopwatch8;
                            break;
                        case "taskcombobox9":
                            stopwatch = stopwatch9;
                            break;
                        case "taskcombobox10":
                            stopwatch = stopwatch10;
                            break;
                        case "taskcombobox11":
                            stopwatch = stopwatch11;
                            break;
                        case "taskcombobox12":
                            stopwatch = stopwatch12;
                            break;
                        case "taskcombobox13":
                            stopwatch = stopwatch13;
                            break;
                        case "taskcombobox14":
                            stopwatch = stopwatch14;
                            break;
                        case "taskcombobox15":
                            stopwatch = stopwatch15;
                            break;
                        default:
                            break;

                    }
                    if (taskComboBox.Text != null)
                    {
                        //Insert data into table through loop
                        string query = "INSERT INTO MasterTable(Task, TimeWorked, RefDate, Analyst, HoursWorked) values('" + taskComboBox.Text + "', '" + stopwatch.Elapsed.ToString("hh\\:mm\\:ss") + "', '" + ReferenceDate + "', '" + AnalystName + "', '" + (stopwatch.Elapsed.TotalMinutes / 60d).ToString("N2") + "')";
                        command.CommandText = query;
                        command.ExecuteNonQuery();
                    }
                }

                //close connection to db
                connection.Close();
            }
            //handle errors
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex);
            }
}

Open in new window


I tried using an if statement inside the foreach statement to notrun the SQL insert command for that combobox if it's value is null:

if (taskComboBox.Text != null)
                    {
                        //Insert data into table through loop
                        string query = "INSERT INTO MasterTable(Task, TimeWorked, RefDate, Analyst, HoursWorked) values('" + taskComboBox.Text + "', '" + stopwatch.Elapsed.ToString("hh\\:mm\\:ss") + "', '" + ReferenceDate + "', '" + AnalystName + "', '" + (stopwatch.Elapsed.TotalMinutes / 60d).ToString("N2") + "')";
                        command.CommandText = query;
                        command.ExecuteNonQuery();
                    }

Open in new window


This does not work. I am not sure it is because of the placement of the command. Any ideas?

Thank you,
Shannon
LVL 2
ShannonCallahanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Sam JacobsDirector of Technology Development, IPMCommented:
The placement looks fine.
What if you tested against a blank entry?
                    if (taskComboBox.Text != "")
                    {
                        //Insert data into table through loop
                        string query = "INSERT INTO MasterTable(Task, TimeWorked, RefDate, Analyst, HoursWorked) values('" + taskComboBox.Text + "', '" + stopwatch.Elapsed.ToString("hh\\:mm\\:ss") + "', '" + ReferenceDate + "', '" + AnalystName + "', '" + (stopwatch.Elapsed.TotalMinutes / 60d).ToString("N2") + "')";
                        command.CommandText = query;
                        command.ExecuteNonQuery();
                    }

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ShannonCallahanAuthor Commented:
2/2! Thank you! I wonder why if (taskComboBox.Text != "") works but if (taskComboBox.Text != null) does not?
ShannonCallahanAuthor Commented:
Thank you again! Saving me on this deadline!
Shannon
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

NorieAnalyst Assistant Commented:
Shannon

Do you want to check if something has been selected in the combobox?
if (taskComboBox.SelectedIndex > -1)

Open in new window

Sam JacobsDirector of Technology Development, IPMCommented:
Because null is the absence of ANY value, which is different then an empty string value.
ShannonCallahanAuthor Commented:
Thank you both for the assistance. Both methods work, probably use the if (taskComboBox.SelectedIndex > -1) for this one. Thank you!!!
it_saigeDeveloperCommented:
*NO POINTS*

For explicit string comparisons two better options would be the string methods IsNullOrEmpty and IsNullOrWhitespace

-saige-
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SQL

From novice to tech pro — start learning today.