DataAdapter.Update() returns error.

M

Manohar

Hi,

I am populating a dataset from a stored procedure, which returns
multiple resultsets. One of the resultset is formed with a join
condition from two tables. I am updating the fields of the respective
datatable. Then update the first table in the database using
DataAdapter.Update(). This succeeds. Subsequently, I update the
fields of the second table in the datatable and update the
respective(second) table in the database using DataAdapter.Update().
This throws the error, "Concurrency violation: the UpdateCommand
affected 0 of the expected 1 records.". Dataset.HasChanges() returns
true before each update. I give below the code.

// First Table

DataRow dr = dtOrderInfo.Rows[iSelectedRow];
dr.BeginEdit();
// Data Row changes.
....
....
dr.EndEdit();

// connect to database and execute as follows.

cnDatabaseOpen(strdbconnect);
SqlDataAdapter da = new SqlDataAdapter("Select top 0 * from
OrderDetail", cnDatabase);
SqlCommandBuilder scb = new SqlCommandBuilder(da);
da.Update(dtOrderInfo);
da.Dispose();
cnDatabaseClose();

// Second Table

dr = null;
dr = dtOrderInfo.Rows[iSelectedRow];
dr.BeginEdit();
// Data Row changes.
....
....
dr.EndEdit();

// connect to database and execute as follows.

cnDatabaseOpen(strdbconnect);
SqlDataAdapter da = new SqlDataAdapter("Select top 0 * from Orders",
cnDatabase);
SqlCommandBuilder scb = new SqlCommandBuilder(da);
// Following line throws error.
da.Update(dtOrderInfo);
da.Dispose();
cnDatabaseClose();

I get the error in da.Update() for the second table.
Any help would be greatly appreciated.

Thanks
Manohar
 
M

Manohar

Hi Peter,

I have primary keys in each of the two tables, but there are no
foreign keys.
What could be the problem?

Thanks
Manohar
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top