Recently I constantly encountered ‘Failed to Enable constraints…’ error when working with DataTables. .Net doesn’t provide any detailed information for this error.
This error might be due to
a) Query columns and Fill method columns mismatch
b) Column maxlength exceeded
c) Duplicate rows(when some join)
d) some other when loading data into the DataTable.
I have had used step through debug to know what caused this error and it was time consuming. But .Net provides a workaround solution for this in the form of DataTable GetErrors method. To know what exactly causing this error(column data), I have used below method to get all the DataTable errors that saved my debugging time.
public string GetErrors(DataTable table)
StringBuilder s = new StringBuilder();
// Test if the table has errors. If not, skip it.
// Print the error of each column in each row.
foreach (DataRow row in table.GetErrors())
foreach (DataColumn column in table.Columns)
s.Append(column.ColumnName + "-" + row.GetColumnError(column));
// Clear the row errors