“Failed to enable constraints. One or more rows contain values violating…”

November 2, 2006

Using DataSets with TableAdapters I was trying to retrieve the different years listed in a database table since I wanted the unique years to show up on a drop-down list. My SQL-query looked like this:

SELECT DISTINCT DATEPART(yyyy, EntryDate) AS Year
FROM nPressClips
ORDER BY Year DESC

 What happened was that the dataset couldn’t fill due to this error:

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

For some reason unknown to me, the query above also returned a null value PressClipID-column that I didn’t want. A quick solution was to simply return a constant column value to avoid it being null. This is my solution, quick and very dirty:

SELECT DISTINCT DATEPART(yyyy, EntryDate) AS Year, 1 AS PressClipID
FROM nPressClips
ORDER BY Year DESC

If anyone can come up with a better solution than this, do not hesitate to contact me. Until then, this works just fine.

About these ads

5 Responses to ““Failed to enable constraints. One or more rows contain values violating…””

  1. Mike.Ke Says:

    you could set the dataset constraints to false as below.

    variable = New
    variable.EnforceConstraints = False

    This way all the fields will be loaded into the dataset.

    hope this helps.

  2. Sivakumar Says:

    Really superb..
    Now I run my application without any compilation error
    so Thank You…..

  3. Michael Says:

    Thanks, the clue to make EnforceConstraints=false helped me resolve out my problem too. Thanks for making it avialable.

  4. Tania Says:

    Thank so much Mike.Ke. Your help were very important to me..


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: