This code works fine but its INSERTING values only from textbox1 textbox2 and Date2 what if i have more textboxes and labels how to INSERT multiple rows from different controls in one click.
To be precise i have 10 pair textboxes textbox 1 and textbox 2 then textbox3 and textbox 4 and 10 Labels Date 2 / 4 /6 / 8
So on each row i want values from textbox[i] textbox[i+1] Date[i+1] and global varialbe buyEUR
private void InsertData_Click(object sender, EventArgs e)
{
SqlCeConnection connection = new SqlCeConnection(@"Data Source=C:\Users\FluksikartoN\Documents\Visual Studio 2012\Projects\BuroFoki\BuroFoki\MainDB.sdf");
connection.Open();
using (SqlCeCommand com = new SqlCeCommand("INSERT INTO TenOperations (EUR, Rate, BGN, Date) Values(@EUR, @Rate, @BGN, @Date)", connection))
{
/* for (int i = 2; i <= 20; i = i+2)
{
TextBox txtBox1 = this.Controls["TextBox" + (i - 1).ToString()] as TextBox;
TextBox txtBox2 = this.Controls["TextBox" + i.ToString()] as TextBox;
Label Date = this.Controls["Date" + i.ToString()] as Label;*/
com.Parameters.AddWithValue("@EUR", textBox2.Text.ToString());
com.Parameters.AddWithValue("@Rate", EURbuy);
com.Parameters.AddWithValue("@BGN", textBox1.Text.ToString());
com.Parameters.AddWithValue("@Date", Date2.Text.ToString());
/*
com.Parameters.AddWithValue("@EUR", textBox4.Text.ToString());
com.Parameters.AddWithValue("@Rate", EURbuy);
com.Parameters.AddWithValue("@BGN", textBox3.Text.ToString());
com.Parameters.AddWithValue("@Date", Date4.Text.ToString());
*/
com.ExecuteNonQuery();
}
connection.Close();
}
you can create a control array See here (although there is no inherent support you can duplicate the functionality) then you can get the values to add as parameters using a loop and insert the value in database.