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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

856 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