473,396 Members | 1,864 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,396 software developers and data experts.

Insert atatement conflict with foreign key

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 this error, if and only if the claim detail table is empty:
Obviously as the foreign key is 0.
System.Data.SqlClient.SqlException was unhandled
Message="The INSERT statement conflicted with the FOREIGN KEY constraint \"FK_Claim_claimheader\". The conflict occurred in database \"RASRMIS\", table \"dbo.claimheader\", column 'headerid'.\r\nThe 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.ToDateTime(rows["DOL"].ToString());

DateTime drep = Convert.ToDateTime(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.NewClaimRow();

claimrow["headerid"] = clhprimarykey;

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

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

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

DataRow[] childrowperil = rows.GetChildRows(ds.Relations["dbfimport_Peril"].RelationName);

if (childrowperil.Length>0)

{

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

}

DataRow[] childrowcause = rows.GetChildRows("dbfimport_Cause");

if (childrowcause.Length>0)

{

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

}

DataRow[] childrowfault = rows.GetChildRows("dbfimport_Fault");

if (childrowfault.Length>0)

{

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

}

DataRow[] childrowresc = rows.GetChildRows("dbfimport_Resc");

if (childrowresc.Length>0)

{

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

}

claimrow["Assid"] = 5;

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

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

ds.Claim.Rows.Add(claimrow);

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

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

DataRow cflRow = ds.ClaimFinancialLoss.NewClaimFinancialLossRow();

//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.ClaimFinancialLoss.Rows.Add(cflRow);

}

ch.Update(ds);

claimdetail.Update(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 2636

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

Similar topics

2
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....
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: 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...
3
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...
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...
2
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.