DataAdapter.UpdateCommand not working c#?

别等时光非礼了梦想. 提交于 2019-12-12 04:58:15

问题


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.


回答1:


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= ?";



回答2:


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();


来源:https://stackoverflow.com/questions/14716103/dataadapter-updatecommand-not-working-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!