Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
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:
DataTable dtAll = dt1.Copy();
dtAll.Merge(dt2);
error is:
"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 26-Sep-12 1:44am
arkiboys2.6K
Edited 26-Sep-12 1:59am
v2
Comments
arkiboys at 26-Sep-12 10:14am
   
Thank you

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
arkiboys at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 432
1 OriginalGriff 250
2 CPallini 240
3 Abdul Samad KP 145
4 George Jonsson 119
0 OriginalGriff 6,329
1 Sergey Alexandrovich Kryukov 5,700
2 CPallini 4,940
3 George Jonsson 3,469
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 26 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100