Georgi Antonov
Georgi Antonov

Reputation: 1641

INSERT more than one row in table on button click

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();

     }

Upvotes: 0

Views: 1014

Answers (2)

Dinesh Saxena
Dinesh Saxena

Reputation: 71

Try if this works.

using (SqlCeCommand com = new SqlCeCommand("INSERT INTO TenOperations (EUR, Rate, BGN, Date) Values(@EUR1, @Rate1, @BGN1, @Date1),(@EUR2, @Rate2, @BGN2, @Date2)", connection))
     {
        com.Parameters.AddWithValue("@EUR1", textBox2.Text.ToString());
        com.Parameters.AddWithValue("@Rate1", EURbuy);
        com.Parameters.AddWithValue("@BGN1", textBox1.Text.ToString());
        com.Parameters.AddWithValue("@Date1", Date2.Text.ToString());

        com.Parameters.AddWithValue("@EUR2", textBox4.Text.ToString());
        com.Parameters.AddWithValue("@Rate2", EURbuy);
        com.Parameters.AddWithValue("@BGN2", textBox3.Text.ToString());
        com.Parameters.AddWithValue("@Date2", Date4.Text.ToString());

        com.ExecuteNonQuery();
     }

Upvotes: 0

Kryptonian
Kryptonian

Reputation: 870

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.

Upvotes: 1

Related Questions