Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parent Child Dataset. How to get foreign key populated

Status
Not open for further replies.

MikeDamone

Programmer
Oct 21, 2003
106
0
0
US
I have a dataset with a parent table and a child table. I have defined the relationship in the dataset designer. I am using a database generated sequence as the ID of the parent table which will be a foreign key in the child table

On the windows form when I click addnew, I create a header record and a child record. At this point the ID of the header record is 0. When I click save, the database generates the ID of the header record and returns it to the dataset which is reflected on the screen. The problem I have is that when I click save, the header ID doesn't exist until the header record is created in the database. That means the child table gets sent down to the database with a 0 as the foreign key ID. Since there is a foreign key defined on the database of the child table, the database returns an error because 0 does not exist as an ID in the child table.

Does anyone have any experience in using the database to generate the ID of the parent table in the database and having that ID populate the child table in the dataset. I see a few options:

1) If i could somehow set the default value property of the ID column of the parent datatable to be go to the database and select the sequence.

2) Instead of using the UpdateAll, save the header record first and then repopulate the chidl datatables with the ID that came back from the database and then save the child tables.

Problem is I don't know how to do either of those. Any ideas? Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top