Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NETSQL-Server
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 10-Nov-11 17:50pm
Edited 10-Nov-11 17:57pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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[^].
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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....
INSERT INTO "FIRSTTABLE" (YOUR COLUMN LIST) VALUES (VALUES TO BE INSERTED)
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...
--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....
  Permalink  
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

The error message is clear. You have specified key values that doesn't exist in the parent table.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

thanks for the help guy i managed to resolve the problem Smile | :)
  Permalink  
Comments
Pete BSC at 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 at 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 at 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..
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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[^]
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 382
1 Damith Weerasinghe 370
2 thatraja 350
3 Sergey Alexandrovich Kryukov 259
4 Ajith K Gatty 200
0 Sergey Alexandrovich Kryukov 10,639
1 OriginalGriff 6,658
2 Peter Leow 4,735
3 Abhinav S 3,813
4 Maciej Los 3,565


Advertise | Privacy | Mobile
Web02 | 2.8.140421.2 | Last Updated 3 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid