473,399 Members | 3,919 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Insert statement conflicted with foreight key contrait.

{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("@ProjectRespondentCompletionDa te", 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)
Oct 2 '06 #1
0 1962

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Ford Desperado | last post by:
I've been reading the docs and playing around, but I'm still not getting the difference. For instance, create table a(i int check(i>0)) create table a_src(i int) go create unique index ai on...
6
by: postings | last post by:
Hi I'm trying to port some data from one database table to another database table on the same server. This is the query I am using: -----> INSERT into newdatabase.dbo.contactevents ...
2
by: trint | last post by:
I am trying to insert a record, but I have to get an automatically created identity field at the same time to do this. string strSQL2 = "INSERT INTO tblTravelDetail(MemberID, " + "...
7
by: trint | last post by:
This: string strSQL2 = "INSERT INTO tblTravelDetailMember(memberId, " + " TravelDetailUplineId, " + " rankId, " + " TravelDetailId, " + " CreatedDateTime, " + " Operator) " + "VALUES ('" +...
2
by: Tim::.. | last post by:
Can someone please tell me why I keep getting the following error from the code below! Error: INSERT statement conflicted with COLUMN FOREIGN KEY constraint...
0
by: Robert Bravery | last post by:
HI all, VS 2005 C# MSSQL2005 I have a parent child grandchild related tables. Claimheader, claim (deteails), claimfinancialloss When ever I try to add row to claimfinancialloss table, I get...
3
by: weird0 | last post by:
I have two tables accounts and ATM and i am trying to insert a tuple in ATM with accountId as foreign key. But even this simple work,I encounter the following error: The INSERT statement...
3
by: haiminnu | last post by:
I have created Two tables 1]EmployeeTable -------------------------- EmpID EmpName AccessLevelID ...
1
by: waqasahmd | last post by:
Hi i have two tables 1- Person 2- Account In Account table i have key accountID which is a FK in Person table. when i run the insert query as
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.