Solved

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_tbl_UserPMessage_tbl_Profile". The conflict occurred in database "omegalove", table "dbo.tbl_Profile", column 'ProfileID'.

Posted on 2010-09-11
5
3,813 Views
Last Modified: 2012-05-10
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_tbl_UserPMessage_tbl_Profile". The conflict occurred in database "omegalove", table "dbo.tbl_Profile", column 'ProfileID'.
The statement has been terminated.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_tbl_UserPMessage_tbl_Profile". The conflict occurred in database "omegalove", table "dbo.tbl_Profile", column 'ProfileID'.
The statement has been terminated.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_tbl_UserPMessage_tbl_Profile". The conflict occurred in database "omegalove", table "dbo.tbl_Profile", column 'ProfileID'.
The statement has been terminated.
conflicted with the FOREIGN KEY constraint "FK_tbl_UserPMessage_tbl_Profile". The conflict occurred in database "omegalove", table "dbo.tbl_Profile", column 'ProfileID'.
The statement has been terminated.

USE [omegalove]
GO
/****** Object:  StoredProcedure [dbo].[prc_PMessage_Save]    Script Date: 09/11/2010 23:59:02 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[prc_PMessage_Save](
	@FromProfileID	int,
	@ToProfileID	int,
	@Subject	nvarchar(100),
	@Body		ntext
) as
begin
	declare @PMessageID int
	declare @ProfileID int

	insert into tbl_PMessage(FromProfileID,Created,Subject,Body)
	values(@FromProfileID,getdate(),@Subject,@Body)

	
	set @PMessageID = SCOPE_IDENTITY();

	if (@ToProfileID = 0)
	begin
	    insert into tbl_UserPMessage(ProfileID,PMessageID,IsRead) values(@ToProfileID,@PMessageID,1)
	end
	else
	begin
		insert into tbl_UserPMessage(ProfileID,PMessageID,IsRead) values(@ToProfileID,@PMessageID,0)
	end
end

Open in new window

9-12-2010-12-51-59-AM.png
0
Comment
Question by:omegalove
5 Comments
 
LVL 11

Accepted Solution

by:
JoeNuvo earned 250 total points
ID: 33655950
check your @FromProfileID versus table tbl_Profile's ProfileID

I think enter value of @FromProfileID is not exists in the table
0
 
LVL 4

Assisted Solution

by:MichaelMH
MichaelMH earned 125 total points
ID: 33656089
You have to reverse your insert statements order. When you have a Parent - Child relation you must be sure that first you are going to insert the values of the parent record and only after the child record.
0
 
LVL 4

Assisted Solution

by:tabish
tabish earned 125 total points
ID: 33656132
Please check your @ToProfileID. The message shows the @ToProfileID that is being passed and entered into  tbl_UserPMessage does not exists in the column "ProfileID" of table "dbo.tbl_Profile"

in the follwoing statement.'

if (@ToProfileID = 0)
      begin
          insert into tbl_UserPMessage(ProfileID,PMessageID,IsRead) values(@ToProfileID,@PMessageID,1)
      end
      else
      begin
            insert into tbl_UserPMessage(ProfileID,PMessageID,IsRead) values(@ToProfileID,@PMessageID,0)
      end

I'm particulary thinking passing a 0 might be a problem as you if statement suggests.

Cheers!
0
 
LVL 11

Assisted Solution

by:JoeNuvo
JoeNuvo earned 250 total points
ID: 33656296
Look in tbl_Profile table, there might not have record with ProfileID = 0

if you really want to insert data into table tbl_UserPMessage with ProfileID = 0
either insert data with ProfileID = 0 into tbl_Profile
or
remove the relationship constraint FK_tbl_UserPMessage_tbl_Profile
0
 

Author Closing Comment

by:omegalove
ID: 33657405
k
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question