Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2010-09-22
6
1,392 Views
Last Modified: 2012-05-10
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.

0
Comment
Question by:EricTViking
  • 4
  • 2
6 Comments
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33739155
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....
0
 
LVL 11

Author Comment

by:EricTViking
ID: 33739178
Thanks for the reply but it doesn't make much sense. I mentioned I was using a dataset in the second sentence.
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33739213
When you add data in the child can you see the FK number?
0
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33739342
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
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33739460
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
0
 
LVL 11

Accepted Solution

by:
EricTViking earned 0 total points
ID: 33740035
Like I said I can't enable the constraint because I allow NULLs in the child FK.

In the end I coded a manual solution that solved the problem.

Pseudo code would be:
Add row to master table

Add row to detail table

Insert temporary PK of new master row as FK in details table

Create list of indices in dataset of added rows in master along with temporary PKs

Update master

Iterate added rows list and replace details FK with real PK

Open in new window

0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question