Quick fix for “Failed to enable constraints” error

If you use strongly-typed datasets in .NET, you’ve encountered the dreaded “Failed to enable constraints …” message.  I most recently encountered it this morning, while unit testing some new code.  There were far fewer search results for the phrase than I expected, so I’ll add my experience to the lot.

The XSD I’m working with has one table and one stored procedure with four parameters.  A call of this stored procedure (via a method in a business logic class) returns a one-column one-row result set.  My code threw a ConstraintException each time the result set value was zero (0).  To eliminate this problem, I changed the value of AllowDBNull attribute of each column in the XSD table from False to True (if the value wasn’t True already).  When I ran the unit tests again, they were successful.

I’ll have to research this further at some point, but I think part of the reason for ConstraintException being thrown in my case was the difference between the stored procedure’s result set columns and the table definition of the associated table adapter.

In any case, setting AllowDBNull to True is one way to eliminate that pesky error.