We help IT Professionals succeed at work.
Get Started

Multiple variables in for each loop.

Last Modified: 2018-11-21
OK. I am working on getting my SQL Insert command working correctly. Currently I have:

private void testButton_Click(object sender, EventArgs e)
                //Create OleDB connection
                OleDbConnection connection = new OleDbConnection();
                connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0; Data Source=I:\PSC SHARED\accessdb.accdb;
Persist Security Info=False;";
                //connect to DB

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

                //Insert data into table through loop
                foreach(ComboBox taskComboBox in ComboBoxList)
                    string query = "INSERT INTO MasterTable(Task, TimeWorked, RefDate, Analyst, HoursWorked) values('" + taskComboBox.Text + "', '" + stopwatch1.Elapsed.ToString("hh\\:mm\\:ss") + "', '" + ReferenceDate + "', '" + AnalystName + "', '" + (stopwatch1.Elapsed.TotalMinutes / 60d).ToString("N2") + "')";
                    command.CommandText = query;
                //close connection to db
            //handle errors
            catch (Exception ex)
                MessageBox.Show("Error: " + ex);

Open in new window

To my surprise, I almost got this working completely. The "foreach" works, executing the command for each combobox on the form. All work except for the 2, TimeWorked and HoursWorked. It repeats the values of stopwatch1. This makes sense because I specified which stop watch to reference.

What I need to know:
How to do get the stop watch information to "marry-up" with the appropriate ComboBox in the loop?

Please let me know if you need additional information and thank you for your time!!!

Watch Question
Citrix Technology Professional / Director of TechDev Services, IPM
Most Valuable Expert 2020
Distinguished Expert 2020
This problem has been solved!
Unlock 1 Answer and 10 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE