Link to home
Start Free TrialLog in
Avatar of EricTViking
EricTVikingFlag for United Kingdom of Great Britain and Northern Ireland

asked on

ADO.Net how to get auto-increment primary key from master into detail table

Using Visual Studio 2008 I have written an app in VB.Net that runs under .Net Framework 2.0.

I am working with disconnected datasets and in this example I have 2 tables in a dataset.

One table can be regarded as the master and the other as the detail.

The Master table has an autoincrement Primary Key value, the detail table has a foreign key into this primary key.

The detail tables' foreign key allows NULLs.


When I add a new row to the master (disconnected from the database) it gets a -ve primary key value i.e. -1 - (because the autoincrement PK can't be properly known until the data is inserted into the database).

When I call an Update() on the Master table, the -ve PK is then replaced with a real PK from the database.


I then need to update the foreign key value in the detail table to match the real PK, but I can't work out how to get the real PK from the Master to the Detail table.


Tried creating a constraint in the dataset bewteen the tables with cascaded updates, this worked fine except that the constaint will not allow the foreign key in the detail table to be NULL. I need to allow NULLs in the detail table - so that won't work.


I can call Update() on the master and get it to generate the proper PKs, but can't find a way of tying up a master PK with which detail row it corresponds to.

Any suggestions welcome.

Avatar of John (Yiannis) Toutountzoglou
John (Yiannis) Toutountzoglou
Flag of Greece image

are you using a dataset?
If socheck the tables in your dataset (dataset Tab)
expand The parent table and you will se also the child...after the last field of your parent table ..this table is that youhave to use....
Avatar of EricTViking

ASKER

Thanks for the reply but it doesn't make much sense. I mentioned I was using a dataset in the second sentence.
When you add data in the child can you see the FK number?
maybe i have to improove my english!!!! ok ...Check this video is exactly what you want
http://msdn.microsoft.com/en-us/vbasic/cc138241.aspx
And also this Provides a function for updatets deletes from parents child relaiton ....
http://msdn.microsoft.com/en-us/vbasic/bb725826.aspx
Hope it helps
ASKER CERTIFIED SOLUTION
Avatar of EricTViking
EricTViking
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial