Showing posts sorted by relevance for query enable constraints. Sort by date Show all posts
Showing posts sorted by relevance for query enable constraints. Sort by date Show all posts

Thursday, February 28, 2008

VS 2005 ASP.NET GridView Give "Failed to enable constraints. One or more rows contain values violating non-null, unique,.." Error

Note: There is a later post on this: http://devnote.stokemaster.com/search?q=enable+constraints Please read that first.

Symptom:


You created a web page with a GridView using a DataSet designer. You had a simple or a bit more complex joins in the table. Then one day, it stops working with an exception of:

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

You do know the target schema changed so you tried to re-configure the query but you still get the same error after re-compiling the dataset and associated DataAdapter.


My Root Cause:

This has happened in my case where the database schema changed drastically. After investigating the automatically generated code the failure occurs when filling the DataTable.

It also happend again much later when someone has changed the Field Size of a column too.

This meant that the the Query worked OK, but the DataTable did not get the necessary schema change.

What Fixed It:

Unfortunately I had to get rid of the entire DataAdapter associated with the query, rebuild the DataSet and then re-bind (re-cofnigure) the ObjectDataSource that binds to the GridView. The key here is that just configuring the query in the DataSet is not good enough, because the associated DataTable does not get the change of the schema and it will exception when the returned row set gets filled in.

Time Wasted:

I was at it for about two hours.

Wednesday, August 19, 2009

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

I occasionally have a problem when trying to use TableAdapter

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

In my case this happened because we did not include any of the primary keys in table joins.

My Solution:

You may want to try this using your DataSet Designer.
  1. Open the dataset designer
  2. Go to the TableAdapter in question
  3. Find where the primary key is set
  4. Right click over and select Primary Key menu
  5. Change the key to something else that make more sense