Search code examples
c#mysqlsql.netsql-server-ce

On button click INSERT multiple (textbox and label) texts from many controls inside Table


I have created table "TenOperations" which has 4 columns "EUR" type real, "Rate" type real ,"BGN" type real , "Date" type nvarchar

On button click im trying to insert the data from all populated textboxes and labels so that :

First row should be

 textbox1.text , Rate , textbox2.text , Date2.text

second row should be

 textbox3.text , Rate , textbox4.text , Date4.text

and so on

Rate is global double variable and Date2 is Label which get it's text changed when textbox2.text is changed.

The problem is that my this code creates infinite rows which have equal values

private void InsertData_Click(object sender, EventArgs e)
{
            var textboxes = new List<TextBox>() {
                textBox1,
                textBox2,
                textBox3,
                textBox4,
                textBox5,
                textBox6,
                textBox7,
                textBox8,
                textBox9,
                textBox10,
                textBox11,
                textBox12,
                textBox13,
                textBox14,
                textBox15,
                textBox16,
                textBox17,
                textBox18,
                textBox19,
                textBox20
            };
            var labels = new List<Label>() {
                Date2,
                Date4,
                Date6,
                Date8,
                Date10,
                Date12,
                Date14,
                Date16,
                Date18,
                Date20        
            };

            SqlCeConnection connection = new SqlCeConnection(@"Data Source=C:\Users\FluksikartoN\Documents\Visual Studio 2012\Projects\BuroFoki\BuroFoki\MainDB.sdf");
            connection.Open();

            for (int i = 0; i < 10 ; i = i++)
            { 

                using (SqlCeCommand com = new SqlCeCommand("INSERT INTO TenOperations (EUR, Rate, BGN, Date) Values(@EUR, @Rate, @BGN, @Date)", connection))
                {


                    com.Parameters.AddWithValue("@EUR", textboxes[i+1].Text.ToString());
                    com.Parameters.AddWithValue("@Rate", EURbuy);
                    com.Parameters.AddWithValue("@BGN", textboxes[i].Text.ToString());
                    com.Parameters.AddWithValue("@Date", labels[i].Text.ToString());

                    com.ExecuteNonQuery();
                }
            }
            connection.Close();
        }

Solution

  • for (int i = 0; i < 10 ; i = i++)
    

    should be

    for (int i = 0; i < 10 ;i++)