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
4,082 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

752 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