Click here to Skip to main content
13,000,569 members (61,502 online)
Rate this:
Please Sign up or sign in to vote.
See more:
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();

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?
Posted 26-Sep-12 1:44am
Updated 26-Sep-12 1:59am
arkiboys 26-Sep-12 10:14am
Thank you

1 solution

Rate this: bad
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:[^]

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.
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170624.1 | Last Updated 26 Sep 2012
Copyright © CodeProject, 1999-2017
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