Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
Two datatables (dt1, dt2) are populated with data.
Each datatable has several fields.
Field ID is common in both datatables.

I would like to have one datatable which has all the fields and data of both datatables. So I try to merge as follows but the error is:
C#
DataTable dtAll = dt1.Copy();
dtAll.Merge(dt2);

error is:
HTML
"Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints."

Can you explain what I am doing wrong please?
Thanks
Posted
Updated 26-Sep-12 1:59am
v2
Comments
arkiboys 26-Sep-12 10:14am    
Thank you

1 solution

If you've bound a datatable to an SQL table or some other ADO provider then I'm not sure you can merge in this way. As it tries binding the data in the table back to the ADO provider. And as the error message suggests, your merged data table fails the constraints imposed on the table.

When using DataTable.Merge you need to make sure the schemas for the data tables are very similar.

See this:

http://msdn.microsoft.com/en-us/library/fk68ew7b.aspx[^]

So you need to make sure the data table schemas are compatible. You then need to make sure that the data you're trying to merge is not going to break any of the constraints imposed by the database.
 
Share this answer
 
Comments
arkiboys 26-Sep-12 10:14am    
Thank you

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