.NET     Console.WriteLine( "All Things .NET" );
.NET Nerd Blog Home
12.12.2006

 
DataSet.Merge - Failed to Enable Constraints
I've been working on an integration project for our client where we are wrapping some legacy data with a web service layer, and pointing an existing WinForms app at it. There is a bunch of client side code that calls DataSet.Merge and fails with the error Failed to enable constraints.

Many times, the problem is what the error is trying to tell you - you have a relationship defined in the dataset that is not satisfied when trying to merge the data - a child row in a relationship perhaps doesn't have a corresponding foreign key in the parent table...

Tonight I was getting this error, and double checked all relationships (spit out the relationships to the debug window), checked for null values and duplicate primary keys (again spewing to the debug window) -- where is my uber DataSet visualizer that I've been wanting to write? (more on that in another post)

Finally I google'd and found this thread. Sanjay explains the best way to find what is causing the error:


foreach ( DataTable dt in mergingSet.Tables )
{
if ( dt.HasErrors )
{
foreach ( DataRow dr in dt.GetErrors() )
{
Debug.WriteLine( dr.RowError );
}
}
}

That'll do it - so simple, huh? The big question - why couldn't the MS devs have just put this info in the exception text itself?

Additional note: HasErrors and GetErrors may not be available/set until AFTER you have attempted the Merge operation (depends on if it's a FK, PK, or unique constraint issue)


Comments: Post a Comment

Powered by Blogger