troubleshooting Question

Multiple variables in for each loop.

Avatar of ShannonCallahan
ShannonCallahan asked on
C#SQL* foreach
10 Comments1 Solution81 ViewsLast Modified:
OK. I am working on getting my SQL Insert command working correctly. Currently I have:

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=I:\PSC SHARED\accessdb.accdb;
Persist Security Info=False;";
                
                //connect to DB
                connection.Open();

                //Setup 
                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;
                    command.ExecuteNonQuery();
                }
                
                //close connection to db
                connection.Close();
            }
            //handle errors
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex);
            }
        }

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!!!

Shannon
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 10 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 1 Answer and 10 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