Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
There are 2 data tables, T1 and T2, which have 2 relations between them. T1 contains 3 columns T1ID, T2ID, T1Name. The T1ID is PK column for T1 and is auto-number identity. T2ID col in T1 can contain nulls but unique and FK col for T2. T2 contains 3 columns T2ID, T1ID and T2Name. T2ID is the PK col for T2 and is also auto-number. T1ID is not null-able col in T2 and is the FK col for T1. Here these tables are related in 2 ways. 1 to many and 0 or 1 to 1. I can insert or update data in ADO level but when this modified dataset is sending for updating in the sql database , the foreign key constraint exception is thrown by the sql server.

//ADO Level
DataRow t1NewRow = ds.Tables["T1"].NewRow();
t1NewRow.BeginEdit();
t1NewRow["T1Name"] = somevalue;
t1NewRow.EndEdit();
DataRow t2NewRow = ds.Tables["T2"].NewRow();
t2NewRow.BegigEdit();
t2NewRow["T1ID"] = t1NewRow["T1ID"];
t2NewRow["T2Name"] = somevalue;
t2NewRow.EndEdit();
ds.Tables["T1"].Rows.Add(t1NewRow);
ds.Tables["T2"].Rows.Add(t2NewRow);
t1NewRow.BeginEdit();
t1NewRow["T2ID"] = t2NewRow["T2ID"];
t1NewRow.EndEdit();


[Modified: just fixed the quasi-dangling modifier]
Posted
Updated 23-Jun-10 12:16pm
v2

1 solution

You'll likely need to refresh your rows after you commit changes to make sure that the key generated by ADO is correct (it should be if there are no possibilities of concurrency problems) or is generated at all (are your constraints defined somewhere in code, or reflected somehow?).

Here's a walk through[^] on how to get your keys sync'd up and refreshed after the update.

Cheers.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900