I found this error on my project. Well i spent around one hour to figure out this problem. People might think that this is some silly error message.

The error message i got is “Failed to enable constraints, one or more rows contain values violating non null, unique or foreign-key constraints”.

this is caused by my stored procedure which is

SELECT e.eventid,e.event,e.eventdate,i.email,u.username
,u.firstname,u.surname,i.senttime,i.readtime,i.respond
FROM invitefriends i
inner join users u ON i.franchiseeid=u.userid
inner join events e ON i.eventid = e.eventid
WHERE i.franchiseeid is not null
    and ( (@EventID IS NULL) or (e.eventid=@EventID) )
ORDER BY e.eventdate DESC
GO

Since the query is returning multiple rows with the same eventid and the primary key in my datatable is eventid then it caused the error.

There are two workaround to this problem:

  • by using identity from your own table or you can generate it from your query and you need to regenerate your datatable and make sure check the primary key in datatable since it’s not automatically changed for you.
  • You can also relax the constraint by removing the Primary key on the DataTable