Prizes & Awards
My Profile
Active Members
TodayLast 7 Days
more...
|
New Feature: Community Sites:
Create your own .NET community website and start earning from Google AdSense !
It's Free !
|
Insert statement conflicted with foreight key contrait.
Posted Date:
02 Oct 2006
Total Responses:
2
|
Posted By: Mathieu Member Level: Bronze Points: 2
|
{System.Data.SqlClient.SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_ProjectResponse__ProjectQuestionId". The conflict occurred in database "RG_ProjectData", table "dbo.ProjectQuestion", column 'ProjectQuestionId'. The statement has been terminated.
public void ProjectResponseandRespondentToDB(DataRow dr, string contents, int iProjectQuestionID, int iAnswerNumber)
{
// Create the command object and set the SQL statement
SqlCommand cmd = new SqlCommand("prAddProjectResponseandRespondent", conn);
cmd.CommandType = CommandType.StoredProcedure;
// Values used in the first insert
cmd.Parameters.Add("@ProjectRespondentVcId", SqlDbType.NVarChar);
cmd.Parameters["@ProjectRespondentVcId"].Value = dr[0];
// Values used in the first insert
cmd.Parameters.Add("@ProjectRespondentCompletionDate", SqlDbType.DateTime);
cmd.Parameters["@ProjectRespondentCompletionDate"].Value = DateTime.ParseExact(dr[1].ToString(), "yyyyMMdd", new System.Globalization.CultureInfo("en-US"));
cmd.Parameters.Add("@ProjectResponseMention", SqlDbType.Int);
cmd.Parameters["@ProjectResponseMention"].Value = 1;
// Values used in the second insert
cmd.Parameters.Add("@ProjectQuestionId", SqlDbType.Int);
cmd.Parameters["@ProjectQuestionId"].Value = iProjectQuestionID;
cmd.Parameters.Add("@ProjectResponseContent", SqlDbType.NVarChar);
cmd.Parameters["@ProjectResponseContent"].Value = contents;
cmd.Parameters.Add("@ProjectResponseOpenEndContent", SqlDbType.NText);
cmd.Parameters["@ProjectResponseOpenEndContent"].Value = DBNull.Value;
try
{
conn.Open();
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
}
finally
{
conn.Close();
}
}
And here is my stored proc:
DECLARE @SQL4 NVARCHAR(4000)
SET @SQL4 = N'EXECUTE ' + @nvcDatabaseName + N'.dbo.sp_executesql N''
CREATE Procedure [dbo].[prAddProjectResponseandRespondent]
@ProjectRespondentVcId nvarchar(255),
@ProjectRespondentCompletionDate datetime,
@ProjectQuestionId int,
@ProjectResponseMention int ,
@ProjectResponseContent nvarchar(255) ,
@ProjectResponseOpenEndContent ntext
AS
declare @ProjectRespondentId int
SET NOCOUNT ON
INSERT INTO [ProjectRespondent] (
ProjectRespondentVcId,
ProjectRespondentCompletionDate)
VALUES (
@ProjectRespondentVcId,
@ProjectRespondentCompletionDate)
SELECT @ProjectRespondentId = SCOPE_IDENTITY()
INSERT INTO [RG_ProjectData].[dbo].[ProjectResponse]
([ProjectRespondentId]
,[ProjectQuestionId]
,[ProjectResponseMention]
,[ProjectResponseContent]
,[ProjectResponseOpenEndContent])
VALUES
(@ProjectRespondentId,
@ProjectQuestionId,
@ProjectResponseMention,
@ProjectResponseContent,
@ProjectResponseOpenEndContent)'''
EXECUTE (@SQL4)
|
Responses
|
| Author: Vadivel Mohanakrishnan 02 Oct 2006 | Member Level: Diamond | Rating: Points: 2 | oops seems to be a lengthy question. Do you mind explaining what exactly you are trying to do and when are you facing this error exactly.
After that if we go through ur code [which u have attached here] it would save some time for both of us!
| | Author: Vadivel Mohanakrishnan 02 Oct 2006 | Member Level: Diamond | Rating: Points: 2 | Just a wild guess .. I understand that ProjectQuestion.QuestionID [Master Table] has a foreignkey relationship with ProjectResponse.QuestionID [Child Table]
That being the case, check whether you are trying to insert a "QuestionID" into "ProjectResponse" table which isn't actually existing in "ProjectQuestion" table.
|
| Post Reply |
|
|
|
You must Sign In to post a response.
|
|
|
|