Feature Post

Top

Error: Update requires a valid UpdateCommand when passed DataRow collection with modified rows

Following locs throw an Invalid Operation exception.

OdbcDataAdapter theAdapter = new OdbcDataAdapter(DbCommand);
DataTable dtTable = new DataTable();
int nRecords = theAdapter.Fill(dtTable);
dtTable.Rows[0]["Name"] = "Jackie";
theAdapter.Update(dtTable); //Error!


Error: "Update requires a valid UpdateCommand when passed DataRow collection with modified rows."


Resolution is to add a OdbCommandBuilder, see following locs:

OdbcCommandBuilder theCommand = new OdbcCommandBuilder(theAdapter);
theAdapter.UpdateCommand = theCommand.GetUpdateCommand();



So, the udpated code looks like:

OdbcDataAdapter theAdapter = new OdbcDataAdapter(DbCommand);
DataTable dtTable = new DataTable();
int nRecords = theAdapter.Fill(dtTable);

OdbcCommandBuilder theCommand = new OdbcCommandBuilder(theAdapter);
theAdapter.UpdateCommand = theCommand.GetUpdateCommand();

dtTable.Rows[0]["Name"] = "Jackie";

nRecords = theAdapter.Update(dtAccounts);

No comments