a database error :“No value given for one or more required parameters.”

后端 未结 2 1681
清酒与你
清酒与你 2021-01-25 18:40

I have a data grid that I should insert its columns values to an access database but I have problem with command.ExecuteNonQuery();

My project is not finish

2条回答
  •  心在旅途
    2021-01-25 19:29

    As suggested in one of the comments above, you should start by changing your code to use a parameterized query. That will relieve you of the need to delimit values, and will also make your code safer. In addition, you should take advantage of the using statement to let .NET manage resources better.

    After making those changes your code would look more like this:

    string query =
        @"INSERT INTO tbl_RentFactor([ID],DateNow,customerName, objectName, 
          objectNumber,unitCost,objectCost,paidMoney,restOfMonyy,customerID,DateBack)
          VALUES (?,?,?,?,?,?,?,?,?,?,?)";
    con.Open();
    for (int i = 0; i < (dataGridFactorRent.Rows.Count) - 1; i++)
    {
        using (var command = new OleDbCommand(query, con));
        {
            command.Parameters.AddWithValue("?", ID);
            command.Parameters.AddWithValue("?", lbldate.Text);
            command.Parameters.AddWithValue("?", cmdCustomName.Text);
            command.Parameters.AddWithValue("?", dataGridFactorRent.Rows[i].Cells[1].Value);
            command.Parameters.AddWithValue("?", dataGridFactorRent.Rows[i].Cells[3].Value);
            command.Parameters.AddWithValue("?", dataGridFactorRent.Rows[i].Cells[4].Value);
            command.Parameters.AddWithValue("?", dataGridFactorRent.Rows[i].Cells[5].Value);
            command.Parameters.AddWithValue("?", txtPaid.Text);
            command.Parameters.AddWithValue("?", lblRemained.Text);
            command.Parameters.AddWithValue("?", customerID);
            command.Parameters.AddWithValue("?", lbldate.Text);
    
            command.ExecuteNonQuery();
        }
    }
    con.Close();
    

    If you still receive an error after making those revisions then double-check the field names in your INSERT statement.

提交回复
热议问题