{System.Data.Sq lClient.SqlExce ption: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_ProjectResp onse__ProjectQu estionId". The conflict occurred in database "RG_ProjectData ", table "dbo.ProjectQue stion", column 'ProjectQuestio nId'.
The statement has been terminated.
public void ProjectResponse andRespondentTo DB(DataRow dr, string contents, int iProjectQuestio nID, int iAnswerNumber)
{
// Create the command object and set the SQL statement
SqlCommand cmd = new SqlCommand("prA ddProjectRespon seandRespondent ", conn);
cmd.CommandType = CommandType.Sto redProcedure;
// Values used in the first insert
cmd.Parameters. Add("@ProjectRe spondentVcId", SqlDbType.NVarC har);
cmd.Parameters["@ProjectRespon dentVcId"].Value = dr[0];
// Values used in the first insert
cmd.Parameters. Add("@ProjectRe spondentComplet ionDate", SqlDbType.DateT ime);
cmd.Parameters["@ProjectRespon dentCompletionD ate"].Value = DateTime.ParseE xact(dr[1].ToString(), "yyyyMMdd", new System.Globaliz ation.CultureIn fo("en-US"));
cmd.Parameters. Add("@ProjectRe sponseMention", SqlDbType.Int);
cmd.Parameters["@ProjectRespon seMention"].Value = 1;
// Values used in the second insert
cmd.Parameters. Add("@ProjectQu estionId", SqlDbType.Int);
cmd.Parameters["@ProjectQuesti onId"].Value = iProjectQuestio nID;
cmd.Parameters. Add("@ProjectRe sponseContent", SqlDbType.NVarC har);
cmd.Parameters["@ProjectRespon seContent"].Value = contents;
cmd.Parameters. Add("@ProjectRe sponseOpenEndCo ntent", SqlDbType.NText );
cmd.Parameters["@ProjectRespon seOpenEndConten t"].Value = DBNull.Value;
try
{
conn.Open();
cmd.ExecuteNonQ uery();
}
catch (Exception ex)
{
throw ex;
}
finally
{
conn.Close();
}
}
And here is my stored proc:
DECLARE @SQL4 NVARCHAR(4000)
SET @SQL4 = N'EXECUTE ' + @nvcDatabaseNam e + N'.dbo.sp_execu tesql N''
CREATE Procedure [dbo].[prAddProjectRes ponseandRespond ent]
@ProjectRespond entVcId nvarchar(255),
@ProjectRespond entCompletionDa te datetime,
@ProjectQuestio nId int,
@ProjectRespons eMention int ,
@ProjectRespons eContent nvarchar(255) ,
@ProjectRespons eOpenEndContent ntext
AS
declare @ProjectRespond entId int
SET NOCOUNT ON
INSERT INTO [ProjectResponde nt] (
ProjectResponde ntVcId,
ProjectResponde ntCompletionDat e)
VALUES (
@ProjectRespond entVcId,
@ProjectRespond entCompletionDa te)
SELECT @ProjectRespond entId = SCOPE_IDENTITY( )
INSERT INTO [RG_ProjectData].[dbo].[ProjectResponse]
([ProjectResponde ntId]
,[ProjectQuestion Id]
,[ProjectResponse Mention]
,[ProjectResponse Content]
,[ProjectResponse OpenEndContent])
VALUES
(@ProjectRespon dentId,
@ProjectQuestio nId,
@ProjectRespons eMention,
@ProjectRespons eContent,
@ProjectRespons eOpenEndContent )'''
EXECUTE (@SQL4)
The statement has been terminated.
public void ProjectResponse andRespondentTo DB(DataRow dr, string contents, int iProjectQuestio nID, int iAnswerNumber)
{
// Create the command object and set the SQL statement
SqlCommand cmd = new SqlCommand("prA ddProjectRespon seandRespondent ", conn);
cmd.CommandType = CommandType.Sto redProcedure;
// Values used in the first insert
cmd.Parameters. Add("@ProjectRe spondentVcId", SqlDbType.NVarC har);
cmd.Parameters["@ProjectRespon dentVcId"].Value = dr[0];
// Values used in the first insert
cmd.Parameters. Add("@ProjectRe spondentComplet ionDate", SqlDbType.DateT ime);
cmd.Parameters["@ProjectRespon dentCompletionD ate"].Value = DateTime.ParseE xact(dr[1].ToString(), "yyyyMMdd", new System.Globaliz ation.CultureIn fo("en-US"));
cmd.Parameters. Add("@ProjectRe sponseMention", SqlDbType.Int);
cmd.Parameters["@ProjectRespon seMention"].Value = 1;
// Values used in the second insert
cmd.Parameters. Add("@ProjectQu estionId", SqlDbType.Int);
cmd.Parameters["@ProjectQuesti onId"].Value = iProjectQuestio nID;
cmd.Parameters. Add("@ProjectRe sponseContent", SqlDbType.NVarC har);
cmd.Parameters["@ProjectRespon seContent"].Value = contents;
cmd.Parameters. Add("@ProjectRe sponseOpenEndCo ntent", SqlDbType.NText );
cmd.Parameters["@ProjectRespon seOpenEndConten t"].Value = DBNull.Value;
try
{
conn.Open();
cmd.ExecuteNonQ uery();
}
catch (Exception ex)
{
throw ex;
}
finally
{
conn.Close();
}
}
And here is my stored proc:
DECLARE @SQL4 NVARCHAR(4000)
SET @SQL4 = N'EXECUTE ' + @nvcDatabaseNam e + N'.dbo.sp_execu tesql N''
CREATE Procedure [dbo].[prAddProjectRes ponseandRespond ent]
@ProjectRespond entVcId nvarchar(255),
@ProjectRespond entCompletionDa te datetime,
@ProjectQuestio nId int,
@ProjectRespons eMention int ,
@ProjectRespons eContent nvarchar(255) ,
@ProjectRespons eOpenEndContent ntext
AS
declare @ProjectRespond entId int
SET NOCOUNT ON
INSERT INTO [ProjectResponde nt] (
ProjectResponde ntVcId,
ProjectResponde ntCompletionDat e)
VALUES (
@ProjectRespond entVcId,
@ProjectRespond entCompletionDa te)
SELECT @ProjectRespond entId = SCOPE_IDENTITY( )
INSERT INTO [RG_ProjectData].[dbo].[ProjectResponse]
([ProjectResponde ntId]
,[ProjectQuestion Id]
,[ProjectResponse Mention]
,[ProjectResponse Content]
,[ProjectResponse OpenEndContent])
VALUES
(@ProjectRespon dentId,
@ProjectQuestio nId,
@ProjectRespons eMention,
@ProjectRespons eContent,
@ProjectRespons eOpenEndContent )'''
EXECUTE (@SQL4)