Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I keep getting this error after inserting text with over 200 characters into my database. I ve tried everything. Can anyone help me please. i have two tables: roles and departments. the roles table has the departments primary key as a foreign key and i have set the relationship to allow for this. I have tried not to enforce foreign key constraints but the problem still persists. The thing is that when i enter about a line or two of text it works fine but with large text it pops up the error below. Both the primary keys of the two tables are int.

System.Data.ConstraintExceptoin: Failed to enable constraints. One or more rows contain values violating non-null, unique or foreign-key constraints
Posted
Updated 13-Apr-11 8:01am
v3
Comments
Kschuler 13-Apr-11 13:45pm    
You need to post more information if you want any help on this. How is your table setup? What columns are keys? You said you've tried everything, well what have you tried and how/why didn't it work? What does the data look like that you are trying to add?
[no name] 13-Apr-11 14:01pm    
i ve updated
pankajupadhyay29 13-Apr-11 13:49pm    
[Edit]added tag for sql server[/Edit]

IMO, if you are getting this error, something is wrong with your data.
You can handle the error in your code, and step out of the code gracefully.

However, you should be looking at ways to correct this erroneous data rather than fix the error.
The entire concept behind the foreign key concept is to prevent wrong data from going into the database and corrupting it.
 
Share this answer
 
Comments
Manfred Rudolf Bihy 13-Apr-11 18:19pm    
Good answer! 5+
Abhinav S 14-Apr-11 1:00am    
Thank you.
I'm going out on a limb here:

Perhaps that column that is a long string is getting truncated by the database column. For example

These 2 unique strings:

aaaaaaaaaaaaaaaaaaaabbbbbb

and

aaaaaaaaaaaaaaaaaaaacccccc

would be non unique if stored into a char(10). Thus breaking a key constraint if one is dependent on this column.
 
Share this answer
 
v2
Comments
Manfred Rudolf Bihy 13-Apr-11 18:20pm    
Interesting idea! Deserves a 5+
if u r geting this error then two case may arise :
1st is that the data that u r going to store is wrong.
second the value or data that u r going to store didn't present in the primary key column of another table which u r refering,


so, at first try to insert data at first in primary key then in foreign key.....


or

before inserting use trim() method.... to trim the unwanted space...
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900