Click here to Skip to main content
15,889,877 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends
The following exception occur when inserting data to table


SQL
<pre>The INSERT statement conflicted with the FOREIGN KEY constraint "FK_PermenentAddress_details_StudentAdmission_details". The conflict occurred in database "School", table "dbo.StudentAdmission_details", column 'AdmissionNumber'.


and my stored procedure is

SQL
create procedure [dbo].[sp_studadmissioninsert](@admissionnumber int,@admissiondate datetime,@fromyear smallint,@toyear smallint,@classname varchar(30),@studname varchar(50),@dob datetime,@gender char(1),@age smallint,@fathername varchar(50),@fatheroccupation varchar(50),@mothername varchar(50),@motheroccupation varchar(50),@medium varchar(25),@studenttype varchar(25),@identimark1 varchar(300),@identimark2 varchar(300),@religion varchar(15),@cast varchar(50),@castcategory varchar(10),@phonenumber varchar(20),@email varchar(80),@previousschool varchar(max),@needtransport char(1),   @street varchar(200),@city varchar(50),@state varchar(50),@country varchar(50),@zipcode varchar(50),@street1 varchar(200),@city1 varchar(50),@state1 varchar(50),@country1 varchar(50),@zipcode1 varchar(15),@datetime datetime,@adminid int,@applicationnumber int )
as
begin
declare @cassid int,@academicid int
select @academicid=Acadamic_year_id from Acadamic_year where Acadamic_year_from=@fromyear and Acadamic_year_to=@toyear
select @cassid=Standered_id from Standered_details where Acadamic_year_id=(select Acadamic_year_id from Acadamic_year where Acadamic_year_from=@fromyear and Acadamic_year_to=@toyear)and Standered_name=@classname

if exists (select  StudentName from StudentApplication_details where ApplicationNumber=@applicationnumber)
begin
update StudentAdmission_details set Admissiondate=@admissiondate,Academic_year_id=@academicid,ClassId=@cassid,StudentName=@studname,Age=@age,FatherOccupation=@fatheroccupation,MotherName=@mothername,MotherOccupation=@motheroccupation,Medium=@medium,StudentType=@studenttype,IdentificationMark1=@identimark1,IdentificationMark2=@identimark2,Religion=@religion,Caste=@cast,CastCategory=@castcategory,Phone_number=@phonenumber,EmailId=@email,Previous_School=@previousschool,Transport_status=@needtransport,Updateddatetime=@datetime,adminid=@adminid where AdmissionNumber=@admissionnumber and ApplicationNumber=@applicationnumber
end
else
begin
--declare @cassid int,@academicid int
--select @academicid=Acadamic_year_id from Acadamic_year where Acadamic_year_from=@fromyear and Acadamic_year_to=@toyear
--select @cassid=Standered_id from Standered_details where Acadamic_year_id=(select Acadamic_year_id from Acadamic_year where Acadamic_year_from=@fromyear and Acadamic_year_to=@toyear)and Standered_name=@classname
insert into StudentAdmission_details(AdmissionNumber ,ApplicationNumber ,Admissiondate,Academic_year_id,ClassId,StudentName,DOB,Gender,Age,FatherName,FatherOccupation,MotherName,MotherOccupation,Medium,StudentType,IdentificationMark1,IdentificationMark2,Religion,Caste,CastCategory,Phone_number,EmailId,Previous_School,Transport_status,Createddatetime,adminid)values(@admissionnumber, @applicationnumber,@admissiondate,@academicid,@cassid,@studname,@dob,@gender,@age,@fathername,@fatheroccupation,@mothername,@motheroccupation,@medium,@studenttype,@identimark1,@identimark2,@religion,@cast,@castcategory,@phonenumber,@email,@previousschool,@needtransport,@datetime,@adminid)
end
if exists(select Street,City,State,Country,Zipcode from TemporaryAddress_details where AdmissionNumber=@admissionnumber and ApplicationNumber=@applicationnumber)
begin
update TemporaryAddress_details set Street=@street,City=@city,State=@state,Country=@country,Zipcode=@zipcode,Updateddatetime=@datetime,adminid=@adminid where AdmissionNumber=@admissionnumber and ApplicationNumber=@applicationnumber
end
else
begin
insert into TemporaryAddress_details(Street,City,State,Country,Zipcode,Createddatetime,adminid,ApplicationNumber,AdmissionNumber)values(@street,@city,@state,@country,@zipcode,@datetime,@adminid,@applicationnumber,@admissionnumber)
end
if exists(select Street,City,State,Country,Zipcode from PermenentAddress_details where StudentApplicationNumber=@applicationnumber and StudentAdmissionNumber=@admissionnumber )
begin
update PermenentAddress_details set Street=@street,City=@city,State=@state,Country=@country,Zipcode=@zipcode,Updateddatetime=@datetime,adminid=@adminid where StudentApplicationNumber=@applicationnumber and StudentAdmissionNumber=@admissionnumber
end
else
begin
insert into PermenentAddress_details(Street,City,State,Country,Zipcode,StudentApplicationNumber,StudentAdmissionNumber,Createddatetime,adminid)values(@street,@city,@state,@country,@zipcode,@applicationnumber,@admissionnumber,@datetime,@adminid)
end
end


In studentAdmission_details "AdmissionNumber is primarykey and Application Number,Acadamic_year_id is foreign key.procedure successfully compiled. but insertion is failed. what could be the problem on my table.
Posted
Updated 28-Oct-12 18:41pm
v2

1 solution

It means exactly what it says. You're trying to insert a value into a column that has a FK constraint on it that doesn't match any values in the lookup table.

In your table dbo.StudentAdmission_details, it has a foreign key reference to another table. The way a FK works is it cannot have a value in that column that is not also in the primary key column of the referenced table.

--Amit
 
Share this answer
 
v2
Comments
baskaran chellasamy 29-Oct-12 1:23am    
Thank you for your comment.i got the solution. Actually I modified the table without delete the foreign key reference that i dont want to go through for my process.now i find solution. Thank you for your valuable solution.
_Amy 29-Oct-12 1:25am    
Welcome. :)

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