Click here to Skip to main content
15,665,229 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
ok so i have two web forms in which data is being added into two tables. one table has a FK which refers to the other table primary key. in the second web form i insert values in to the form and want to store it in the db...but i get an error

"The INSERT statement conflicted with the FOREIGN KEY constraint "FK__StuDetail__eduid__5AA469F6". The conflict occurred in database "Dunstan_DB", table "dbo.EduDetails", column 'eduid'.
The statement has been terminated.


wat is the problem and how can it be resolved ?
Posted
Updated 10-Nov-11 17:57pm
v2

as you say you have two tables....
so you have to fire insert query in primary key table first and get a identity value means newly added records primary key and then after completion of that process you have to fire second insert query with inserting a value in the FOREIGN key that is the out putted primary key from the first insert query...

IF YOU USE A STORE PROCEDURE THEN USE QUERY LIKE THIS....
SQL
INSERT INTO "FIRSTTABLE" (YOUR COLUMN LIST) VALUES (VALUES TO BE INSERTED)

SQL
DECLARE @IDENTITY INT 
INSERT INTO TABLE1(USERNAME,PASSWORD) VALUES ('test','test')
SELECT @IDENTITY =  @@IDENTITY FROM TABLE1
INSERT INTO TABLE2(USERID,FIRSTNAME,LASTNAME,MOBILENO) VALUES (@IDENTITY,'test','test','xxxx')
SELECT * FROM TABLE1
SELECT * FROM TABLE2

OR IF YOU NOT USE A STORE PROCEDURE AND USE ONLY TEXT QUERY THEN USE THIS...
SQL
--first insert data to table 1 contains primary key
INSERT INTO USERDETAIL(USERNAME,PASSWORD) VALUES ('test','test')
--select IDENTITY value of last inserted record means primary key
-- you can use any of this select query for that
SELECT TOP 1 ID FROM USERDETAIL ORDER BY ID DESC
--OR 
SELECT MAX(ID) FROM USERDETAIL
-- fill you resulted data in you container for use..
--fire insert query on second table containing FK
-- here use the data returnd from your select query..
INSERT INTO USERPERSONALDETAIL(USERID,FIRSTNAME,LASTNAME,MOBILENO) VALUES ('your select query data','test','test','xxxx')


you have to tack care of relation ship while using parent child relation in database...
you got that error because of that reason....
 
Share this answer
 
v3
Based on the error, you have a relationship between your tables and the FK value (column: eduid) you are inserting is not in the "other table".

Here is a good explanation of relationships[^].
 
Share this answer
 
The error message is clear. You have specified key values that doesn't exist in the parent table.
 
Share this answer
 
thanks for the help guy i managed to resolve the problem :)
 
Share this answer
 
Comments
Pete BSC 22-Nov-11 15:16pm    
IF any of the above items helped you, mark them as the solution or make this your solution and explain how you fixed it (for future readers).
Briga85 3-Oct-12 9:12am    
I have the same problem...I have to insert in a second table an item that not have to appear in the first one...how you solved your problem?
srinivasadari 9-Jan-13 4:40am    
I'm facing the same problem.I have to insert primary key and foreign key values in both tables from asp.net application.The newly coming value is primary key and foreign key at a time.If any help it would be appreciated..
Check the data from web forms such that you are entering a column value which is not present in the primary key column.

http://www.w3schools.com/sql/sql_foreignkey.asp[^]
 
Share this answer
 

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