Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove constraints from datatable in .NET?

I cannot set any constraints in my datatable if i merge two datatables dt1.Merge(dt2); It shows error like this Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints. By default i view in visualizer it shows constraints count as 2. How to remove constraints from datatable and solve this error?

like image 503
Ravi Avatar asked Jan 11 '12 05:01

Ravi


2 Answers

To remove constraints from a DataTable you can use the Clear method of the Constrains property:

myDataTable.Constraints.Clear();
like image 106
Peter Meinl Avatar answered Oct 17 '22 06:10

Peter Meinl


try iterating the constraints of datatable and remove them using

private void RemoveConstraint(DataTable table, 
    Constraint constraint)
{
    if(table.Constraints.Contains(constraint.ConstraintName))
        if(table.Constraints.CanRemove(constraint))
            table.Constraints.Remove(constraint);
}
like image 45
Shoaib Shaikh Avatar answered Oct 17 '22 05:10

Shoaib Shaikh