The current program I am building is used to save invoices and I want to save data into a database. However instead of repeating this code shown below 20 times for each possible entry i would like to create a function with the text box name changing in the function.
All the text boxes are named with a number at the end from 1 to 20. I was wondering if there is a way to have a function that would change the number at the end and if its even worth doing compared to repeating this 20 times.
if (txtProductID1.Text.Length > 0)
{
OleDbConnection oledbconnection1 = new OleDbConnection();
oledbconnection1.ConnectionString = Con;
OleDbCommand cmd;
String strInsert = "";
//Generate SQL Statement
strInsert = "Insert into [InvoiceOrder] Values (";
strInsert += "'1', ";
strInsert += "'" + txtInvoiceNo.Text + "', ";
strInsert += "'" + txtProductDescription1.Text + "', ";
strInsert += "'" + txtOrderNo1.Text + "', ";
strInsert += "'" + cboUnit1.Text + "', ";
strInsert += "'" + txtAmount1.Text + "', ";
strInsert += "'" + txtPrice1.Text + "', ";
strInsert += "'" + txtSum1.Text + "', ";
strInsert += "'" + txtDiscount1.Text + "' ";
strInsert += ")";
try
{
oledbconnection1.Open();
cmd = new OleDbCommand();
cmd.CommandText = strInsert;
cmd.Connection = oledbconnection1;
cmd.ExecuteNonQuery();
//MessageBox.Show("Record saved");
}
catch (Exception ex)
{
MessageBox.Show("Error : " + ex.ToString());
}
finally
{
oledbconnection1.Close();
}
}