Waqas Ali
Waqas Ali

Reputation: 189

DataAdapter.UpdateCommand not working c#?

I am using this code to update "SOME" columns in a table in my database. But everytime I try to do so an error is given.

No value given for one or more required parameters.

con.Open();
SlipDA = new OleDbDataAdapter();
string sqlUpdate = "Update tbl_Slip SET RaiseBasic=@RaiseBasic, OtherDed=@OtherDed, Arrears=@Arrears, Notes=@Notes WHERE SlipNo=@SlipNo";

SlipDA.UpdateCommand = new OleDbCommand(sqlUpdate, con);
SlipDA.UpdateCommand.Parameters.AddWithValue("@RaiseBasic", Convert.ToInt32(dRow[4].ToString()));
SlipDA.UpdateCommand.Parameters.AddWithValue("@OtherDed", Convert.ToInt32(dRow[5].ToString()));
SlipDA.UpdateCommand.Parameters.AddWithValue("@Arrears", Convert.ToInt32(dRow[7].ToString()));
SlipDA.UpdateCommand.Parameters.AddWithValue("@Notes", dRow[8].ToString());
SlipDA.UpdateCommand.Parameters.AddWithValue("@SlipNo", dRow[0].ToString());

SlipDA.UpdateCommand.ExecuteNonQuery();
con.Close();

The table contains 9 columns but I only want to update a few.

Upvotes: 0

Views: 745

Answers (2)

Тарас Кузь
Тарас Кузь

Reputation: 1

try this

    string sqlUpdate = "Update tbl_Slip SET RaiseBasic=@RaiseBasic, OtherDed=@OtherDed, Arrears=@Arrears, Notes=@Notes WHERE SlipNo=@SlipNo";

    OleDbCommand UpdateCommand = new OleDbCommand(sqlUpdate, con);
    UpdateCommand.Parameters.AddWithValue("@RaiseBasic", Convert.ToInt32(dRow[4].ToString()));
    UpdateCommand.Parameters.AddWithValue("@OtherDed", Convert.ToInt32(dRow[5].ToString()));
    UpdateCommand.Parameters.AddWithValue("@Arrears", Convert.ToInt32(dRow[7].ToString()));
    UpdateCommand.Parameters.AddWithValue("@Notes", dRow[8].ToString());
    UpdateCommand.Parameters.AddWithValue("@SlipNo", dRow[0].ToString());

    con.Open();
    UpdateCommand.ExecuteNonQuery();
    con.Close();

Upvotes: 0

Abdusalam Ben Haj
Abdusalam Ben Haj

Reputation: 5423

This Could be the problem :

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. For example: SELECT * FROM Customers WHERE CustomerID = ?

source : this

so basically your query should be like this :

string sqlUpdate = "Update tbl_Slip SET RaiseBasic= ?, OtherDed= ?, Arrears= ?, Notes= ? WHERE SlipNo= ?";

Upvotes: 2

Related Questions