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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
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.

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now