0

When i'm trying to merge Table in dataset this Exception Occurred :- (Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.)

Code:

MyDataset.Tables[0].Merge(dt, false, MissingSchemaAction.Add);
Ash Burlaczenko
  • 24,778
  • 15
  • 68
  • 99
Akash
  • 21
  • 1
  • 4
  • 2
    Do you have a question? The exception is self-explanatory. – Ash Burlaczenko Nov 22 '12 at 13:17
  • trying to add Table in dataset ? Try `MyDataset.Tables.Add(dt);` Hope it would do what you are trying...... :) – Sami Nov 22 '12 at 16:41
  • Have a look at [Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints](http://stackoverflow.com/questions/7026566/failed-to-enable-constraints-one-or-more-rows-contain-values-violating-non-null). – Murat Yıldız Mar 20 '16 at 20:33

1 Answers1

2

I looked at the question in Murat Yıldız comment, but none of the answers worked with my setup. My code failed at the Merge method too. What worked in my case was this snippet, taken from here: http://www.codeproject.com/Tips/405938/Debugging-DataSet-Constraint-Errors

try
{
    dataSet.Merge(anotherDataSet);
}
catch (ConstraintException)
{
    foreach (DataTable table in dataSet.Tables)
    {
        DataRow[] rowErrors = table.GetErrors();

        System.Diagnostics.Debug.WriteLine(table.TableName + " Errors:" + rowErrors.Length);

        for (int i = 0; i < rowErrors.Length; i++)
        {
            System.Diagnostics.Debug.WriteLine(rowErrors[i].RowError);

            foreach (DataColumn col in rowErrors[i].GetColumnsInError())
            {
                System.Diagnostics.Debug.WriteLine(col.ColumnName + ":" + rowErrors[i].GetColumnError(col));
            }
        }
    }
}
DavidC
  • 654
  • 1
  • 14
  • 20
  • Thnx alot. This is the best way to get the actual Error. In my case it says "Failed to enable .... foreign-key constraints .... " but table.GetErrors() shows that the actual reason is maxLength of the column was exeeded. – nuwancy Feb 22 '22 at 07:46