Click here to Skip to main content
15,898,747 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two tables, the first is the main one with a primary key called MaGV, the second has a field references to MaGV also called MaGV.

I create two datatables to store the corresponding data from the two tables in database.

After modifying the data in these two datatables, I start updating back to the two tables using adapter but there are two cases:

1.If using DataTable.GetChanges() like this:
C#
adapter.Update(FirstDataTable.GetChanges();
adapter.Update(SecondDataTable.GetChanges());

A conflicted constraint exception will be thrown and I can understand it, just for the first Update couldn't do the update to the main Table and particularly the update should have added a new row to the table with MaGV = 1 (for example) but it didn't and there isn't any row with MaGV = 1 in the main Table.

Then the second Update, in my case, will insert a new row with MaGV = 1 into the second table and that of course will throw the exception.

2. If not using DataTable.GetChanges(), all the Updates will be done successfully, like this:
C#
adapter.Update(FirstDataTable);
adapter.Update(SecondDataTable);

I can't understand why updating with GetChanges() can't be done well, it seems that GetChanges() returns null?

Could you please analyze the problem and give me any ideas?

Any of your helps are highly appreciated!

Thank you very much!
Posted
Updated 2-Nov-11 4:11am
v3

Can you try calling AcceptChanges before calling GetChanges? AcceptChanges ends edits on the table

http://msdn.microsoft.com/en-us/library/system.data.datatable.acceptchanges.aspx[^]

Forgive me if it seems silly and you have already done that, it really is a wild guess based on what I've read.
 
Share this answer
 
v2
It's not easy to point exact cause of your problem.

However you may find the Rows of your DataTable which are having Error/Problem by using below code.
C#
ChangedDataTable = FirstDataTable.GetChanges();

if (ChangedDataTable.HasErrors)
{
  DataRow[] drErrors = ChangedDataTable.GetErrors();
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900