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,970 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

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

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

Suggested Solutions

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

680 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