If you like using .NET TableAdapters then I'd bet you've seen this message more than once:

Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.

This message is (like always) not very helpful in tracking down the problem, but going through the list below should help track down the cause.

Causes:

  • Field size has changed.
  • Bad query.
  • .NET doesnt like you.

Fixes:

  • Field size has changed. This has been the cause a couple of times for me. I'm not the only one modifying the database, so things can change without notice occasionally. Check to make sure the MaxLength property for each field is set correctly in the TableAdapter. If a value is pulled from the database that is too long it can throw the error.
    Note: this can also occur without any changes to the database if your TableAdapter references a Stored Procedure. Stored Procedures do not always communicate the correct MaxLength of fields when building the TableAdapter, so always ensure the MaxLength of each field is correct when using Stored Procedures.
  • Bad query. Run the query in Query Analyser and check to make sure you are not pulling duplicate keys or null values in fields that should not be null.
  • .NET doesnt like you. If all else fails delete the TableAdapter and recreate it. This can be very annoying, but sometimes it is easier than checking each field individually.