asked on
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);
}
}
ASKER
public partial class Form1 : Form
{
//Declare stopwatches at Form/Class level.
Stopwatch stopwatch1 = new Stopwatch();
Stopwatch stopwatch2 = new Stopwatch();
Stopwatch stopwatch3 = new Stopwatch();
Stopwatch stopwatch4 = new Stopwatch();
Stopwatch stopwatch5 = new Stopwatch();
Stopwatch stopwatch6 = new Stopwatch();
Stopwatch stopwatch7 = new Stopwatch();
Stopwatch stopwatch8 = new Stopwatch();
Stopwatch stopwatch9 = new Stopwatch();
Stopwatch stopwatch10 = new Stopwatch();
Stopwatch stopwatch11 = new Stopwatch();
Stopwatch stopwatch12 = new Stopwatch();
Stopwatch stopwatch13 = new Stopwatch();
Stopwatch stopwatch14 = new Stopwatch();
Stopwatch stopwatch15 = new Stopwatch();
private void timer1_Tick(object sender, EventArgs e)
{
//Set Textbox text equal to elapsed time
TimerTextBox1.Text = stopwatch1.Elapsed.ToString("hh\\:mm\\:ss");
TimerTextBox2.Text = stopwatch2.Elapsed.ToString("hh\\:mm\\:ss");
TimerTextBox3.Text = stopwatch3.Elapsed.ToString("hh\\:mm\\:ss");
TimerTextBox4.Text = stopwatch4.Elapsed.ToString("hh\\:mm\\:ss");
TimerTextBox5.Text = stopwatch5.Elapsed.ToString("hh\\:mm\\:ss");
TimerTextBox6.Text = stopwatch6.Elapsed.ToString("hh\\:mm\\:ss");
TimerTextBox7.Text = stopwatch7.Elapsed.ToString("hh\\:mm\\:ss");
TimerTextBox8.Text = stopwatch8.Elapsed.ToString("hh\\:mm\\:ss");
TimerTextBox9.Text = stopwatch9.Elapsed.ToString("hh\\:mm\\:ss");
TimerTextBox10.Text = stopwatch10.Elapsed.ToString("hh\\:mm\\:ss");
TimerTextBox11.Text = stopwatch11.Elapsed.ToString("hh\\:mm\\:ss");
TimerTextBox12.Text = stopwatch12.Elapsed.ToString("hh\\:mm\\:ss");
TimerTextBox13.Text = stopwatch13.Elapsed.ToString("hh\\:mm\\:ss");
TimerTextBox14.Text = stopwatch14.Elapsed.ToString("hh\\:mm\\:ss");
TimerTextBox15.Text = stopwatch15.Elapsed.ToString("hh\\:mm\\:ss");
}
ASKER
Stopwatch stopwatch = new Stopwatch();
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>();
var StopwatchList = Controls.OfType<Stopwatch>();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
//Insert data into table through loop
foreach (ComboBox taskComboBox in ComboBoxList)
{
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;
}
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);
}
}
ASKER
string comboname = taskComboBox.Name;
string stopwatchname = comboname.Replace("taskComboBox", "stopwatch");
stopwatch = this.Controls[stopwatchname];
C# is an object-oriented programming language created in conjunction with Microsoft’s .NET framework. Compilation is usually done into the Microsoft Intermediate Language (MSIL), which is then JIT-compiled to native code (and cached) during execution in the Common Language Runtime (CLR).
TRUSTED BY
I don't quite understand the setup of your form.
How many comboboxes do you have?
Can you set up a separate stopwatch for each combobox?
Sam