troubleshooting Question

If statement inside of foreach loop.

Avatar of ShannonCallahan
ShannonCallahan asked on
C#SQL
7 Comments2 Solutions122 ViewsLast Modified:
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);
            }
}

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();
                    }

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

Thank you,
Shannon
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 2 Answers and 7 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 7 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros