Failed to Enable Constraints on DataTableAdapter ASP.NET
Written by Ben McInturff   
Thursday, 14 May 2009 04:49

If you've ever been frustrated by the exception message "Failed to enable constraints..." while working with a data table or data table adapter, you understand both the benefits and drawbacks to using the powerful data design tools from Microsoft.

 When I encounter this error, I usually take a few moments to try to find the non-nullable field in the datatable that for whatever reason is not being returned in the data table. If I am unsuccessful in that, I was passed this awesome way of finding the actual exception by my awesome team lead: 

  1. Find the ‘fill’ command that’s erroring (this can usually be found by tracing the stack trace of a debug build).
  2. If this is a generated method (from the TableAdapters) then there’ll be an attribute at the beginning of the method: [System.Diagnostics.DebuggerNonUserCodeAttribute()]
  3. Comment out the attribute and set a breakpoint at the Fill command
  4. Run to the breakpoint and cause the error to happen
  5. In the watch window add a watch on dataTable.GetErrors()
This method can get right to the problem, and save a ton of checking and previewing the data, and the problems with staged and production data differences. I have also found (also through the wisdom of my team lead) that sometimes if a DataTableAdapter starts behaving weirdly, it can be beneficial to copy all the methods out of the DataTableAdapter, delete it, and then recreate it from scratch.
 

Valid XHTML and CSS.