-1

What is the problem in this code?

for (int i = 1; i <= kalanum; i++)
{
    foreach (Control ctr in panel1.Controls)
    {                       
        if (ctr is TextBox && ctr.Name == i.ToString())
        {
            int kalaid = int.Parse(((TextBox)ctr).Text);
            oleDbCommand1.Parameters.AddWithValue("@k", kalaid);
        }
        else if (ctr is TextBox && ctr.Name == "tbxfee_" + (i.ToString()))
        {
            int fee = int.Parse(((TextBox)ctr).Text);
            oleDbCommand1.Parameters.AddWithValue("@fe", fee);
        }
        else if (ctr is TextBox && ctr.Name == "tbxnumber_" + (i.ToString()))
        {
            int number = int.Parse(((TextBox)ctr).Text);
            oleDbCommand1.Parameters.AddWithValue("@n", number);
        }            
    }

    oleDbCommand1.CommandText = "INSERT INTO fackala(factornum,kalaid,fee,number) values(@f,@k,@fe,@n)";
    oleDbConnection1.Open();
    oleDbCommand1.ExecuteNonQuery();
    oleDbConnection1.Close();            
}

All columns in my access file are numbers. When I insert all dates in the form and send the form, it shows:

Syntax error in INSERT INTO statement.

krlzlx
  • 5,752
  • 14
  • 47
  • 55

1 Answers1

0

There are three problems here:

  • You're using four parameters, but only specifying 3 of them. (Comments suggest that you're specifying the other one outside the loop)
  • On any one iteration you're only populating at most one of the parameters - what do you expect the values of the others to be?
  • You're trying use named parameters with OleDbConnection. From the OleDbConnection.Parameters documentation:

    The OLE DB .NET Provider does not support named parameters for passing parameters to an SQL statement or a stored procedure called by an OleDbCommand when CommandType is set to Text. In this case, the question mark (?) placeholder must be used.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194