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
Your Filters
Interested
Ignored
     
0 Guruprasad.K.Basavaraju 490
1 Sergey Alexandrovich Kryukov 316
2 Shai Vashdi 314
3 OriginalGriff 190
4 praveen_07 115
0 Sergey Alexandrovich Kryukov 9,185
1 OriginalGriff 5,315
2 Peter Leow 4,040
3 Maciej Los 3,535
4 Abhinav S 3,308


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