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 OriginalGriff 324
1 Sergey Alexandrovich Kryukov 182
2 Maciej Los 159
3 Hard_Rockz 143
4 Richard MacCutchan 125
0 OriginalGriff 5,374
1 Sergey Alexandrovich Kryukov 4,713
2 Peter Leow 2,944
3 DamithSL 2,465
4 Maciej Los 2,270


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