473,799 Members | 2,822 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Insert atatement conflict with foreign key

HI all,

VS 2005 C# MSSQL2005

I have a parent child grandchild related tables. Claimheader, claim (deteails), claimfinanciall oss
When ever I try to add row to claimfinanciall oss table, I get this error, if and only if the claim detail table is empty:
Obviously as the foreign key is 0.
System.Data.Sql Client.SqlExcep tion was unhandled
Message="The INSERT statement conflicted with the FOREIGN KEY constraint \"FK_Claim_clai mheader\". The conflict occurred in database \"RASRMIS\", table \"dbo.claimhead er\", column 'headerid'.\r\n The statement has been terminated."

IF the claim detail has atleast one row in it, then the data is added. The the insert works
Briefly here is what I do:
DataRow chrow = ds.claimheader. NewRow();

DateTime dt = Convert.ToDateT ime(rows["DOL"].ToString());

DateTime drep = Convert.ToDateT ime(rows["DREP"].ToString());

chrow["dhid"] = rows["dhid"];

chrow["DOL"] = dt.AddYears(4);

chrow["DREP"] = drep.AddYears(4 );

chrow["DFIN"] = rows["DFIN"];

chrow["brkref"] = rows["BRK"];

chrow["clmperiod"] = rows["clmperiod"];

ds.claimheader. Rows.Add(chrow) ;

//update the claims details

clhprimarykey = Convert.ToInt32 ( chrow["headerid"].ToString());

DataRow claimrow = ds.Claim.NewCla imRow();

claimrow["headerid"] = clhprimarykey;

claimrow["ctkey"] = rows["ctkey"];

claimrow["brkref"] = rows["BRK"];

claimrow["DET"] = rows["DET"];

DataRow[] childrowperil = rows.GetChildRo ws(ds.Relations["dbfimport_Peri l"].RelationName);

if (childrowperil. Length>0)

{

claimrow["peril"] = childrowperil[0]["perilid"];

}

DataRow[] childrowcause = rows.GetChildRo ws("dbfimport_C ause");

if (childrowcause. Length>0)

{

claimrow["cause"] = childrowcause[0]["causeid"];

}

DataRow[] childrowfault = rows.GetChildRo ws("dbfimport_F ault");

if (childrowfault. Length>0)

{

claimrow["fault"] = childrowfault[0]["faultid"];

}

DataRow[] childrowresc = rows.GetChildRo ws("dbfimport_R esc");

if (childrowresc.L ength>0)

{

claimrow["resc"] = childrowresc[0]["rescid"];

}

claimrow["Assid"] = 5;

claimrow["polsecdiv"] = rows["PSD_Key"];

claimrow["companyid"] = rows["Companyid"];

ds.Claim.Rows.A dd(claimrow);

//Update and add claim financial loss details
//get the newly added claimid

int claimid = Convert.ToInt32 (claimrow["ClaimID"].ToString());

DataRow cflRow = ds.ClaimFinanci alLoss.NewClaim FinancialLossRo w();

//Add MD

cflRow["Ammount"] = rows["MD"] == DBNull.Value ? 0.00 : rows["MD"];

cflRow["Clacorder"] = 1;

cflRow["ClaimId"] = claimid;

cflRow["calcxs"] = true;

cflRow["losstype"] = 1; //MD

cflRow["inclgross"] = true;

ds.ClaimFinanci alLoss.Rows.Add (cflRow);

}

ch.Update(ds);

claimdetail.Upd ate(ds);

cfl.Update(ds);

It seems that when the table is empty the claimid after inserting a rows is still 0, or is the same as the claimheaderid, which also seems to be the same as the row added, so if it is two rows added then claimheaderid and claim id will be 2,even tho select ident_current() shows the correct last values, as 5777 from claimheaderid, and 5026 from claimid.

But if the claim table has a row in it, then the claimid has a or next correct value.

What am I missing here

Thanks

Robert


Mar 25 '07 #1
0 2651

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
5100
by: Victor M. | last post by:
Hello everyone. I am a newbie to Oracle 8 so please be patient and thanks for your time. The problem is the above mentioned error. I have two databases successully created using SQL*Plus 8.03. They are as follows: Create Table EMPLOYEES ( FName VARCHAR(15) NOT NULL, MINIT CHAR,
6
2255
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 (EventTypeID, UserID, ContactID, DateEntered, EventDate, Description)
2
1646
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, " + " TravelEventID, " + " CreatedDateTime, " + " Operator) " + "VALUES ('" + TxtMLV.Text.Trim() + "', " + " '" + TxtEventID+ "', " + " '" + TxtDATE.Text.Trim() + "', " +
7
1945
by: trint | last post by:
This: string strSQL2 = "INSERT INTO tblTravelDetailMember(memberId, " + " TravelDetailUplineId, " + " rankId, " + " TravelDetailId, " + " CreatedDateTime, " + " Operator) " + "VALUES ('" + insertString2 + "', " + " '" + insertString3 + "', " +
2
12963
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 'FK_tblOfficePageContent_tblPageContent'. The conflict occurred in database 'CPNCMS', table 'tblPageContent', column 'pageID'. The statement has been terminated. I cant seem to see why I keep getting an error when I do the insert!
0
1987
by: macupryk | last post by:
{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...
3
3894
by: Dave | last post by:
I have an old web app that ues an Access database and ASP 3.0. I need to build an INSERT statement based on the contents of a form. What is the best way to handle blank text boxes that are submitted with the form? For example, I collect all my name/value pairs that are submitted with the form like this...
3
45329
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 conflicted with the FOREIGN KEY constraint "FK_ATM_Accounts". The conflict occurred in database "G:\DOCUMENTS AND SETTINGS\ADMINISTRATOR\DESKTOP\VERSION2\APP_DATA\BANKINGDB.MDF", table "dbo.Accounts", column 'acc_Id'. please sugggest how can i fix...
2
7340
by: ksenthilbabu | last post by:
Hey All, I am using MSSQL -2005 with VB6. I have created a master table tblCompany and detail Table tblDetail having foreign key relationship. When i try to insert a value within a TRANSACTION I am getting Error No. -2147217873 at Line No. 0 (The INSERT statement conflicted with the FOREIGN KEY constraint "FK_tblDetail_tblCompany". The conflict occurred in database "DBTest", table "dbo.tblCompany", column RefID.) . please help me to solve...
0
9550
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10495
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10269
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10248
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9085
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7573
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5597
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2942
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.