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.
There are three problems here:
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.
See more on this question at Stackoverflow