I’m building an ASP.NET application this weekend and ran into the following error.
Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.
The error message itself is somewhat vague and it took me a little while to troubleshoot it so I thought I’d post my solution here. There are many reasons you could get this error, the most obvious being that you may have data in your database that contains a null value or that violates a foreign-key constraint.
I spent a while checking my database and everything seemed to be in order. There were no null values where there shouldn’t be, my relationships were setup properly, and I’d double-checked that I wasn’t enforcing foreign-key constraints.
After wracking my brain for a while, I started Googling and found some suggestions in a few different .NET forums which helped fix the problem. It turns out that during my data-importing I’d decided to change a field in one of my tables from VARCHAR(50) to VARCHAR(100) because some of the data was larger than I’d originally expected. However, I’d neglected to update the MaxLength property of the same field in my DataSet file. This disparity is what caused the problem. Updating the MaxLength property to 100 instantly fixed the issue.
So this error message can be somewhat misleading. If you find yourself faced with this error and you’ve checked all the things that the error message suggests and you’re still having problems, check to see that the sizes of your database fields match the sizes in your dataset.

