Add Child Datarow, but How? RRS feed

  • Question

  • Hi Everybody,

    i started Developing a few days, but one Problem i have.

    I Create in a Dataset a new Row. Ok works fine. Now I want create a DataRow with a Relation to the Data. So there is the Problem. What value I insert (auto Key) in the Field where the Relation is.

    So my structure is like this


    id  | full name | street


    id | id_adress | type | number

    Greats from Austria


    Tuesday, June 19, 2007 8:05 AM

All replies

  • Hi,


    When you create a new row in the Address table that rows ID (if the column is auto increment) should be populated. You use this ID to set the id_address field of the contacts table. Here's an example.


    Code Snippet

    Dim data As New DataExample


    Dim address As DataExample.addressRow = data.address.NewaddressRow

    Dim contact As DataExample.contactRow =


    contact.addressID = address.ID




    You need to make sure you add the parent row (the address) first before you add the child row (the contact). If you add the contact row first it checks the address table for the addressIID, if the address ID isn't in that table then you will get an exception.

    Tuesday, June 19, 2007 11:11 AM
  • Oh thanks.

    Why knows that at the update from the DataSet to change the Primary Key to the Key the comes from the Database?

    Is this check with the Relation in the DataSet?

    Greats from Austria

    Andreas Hoffmann

    Wednesday, June 20, 2007 5:46 AM
  • Hi,


    These articles here covers how to deal with this situation better than I ever could write in a post.


    Managing Hierarchical Inserts in ASP.NET and ADO.NET


    Saving Parent-child Data in a Multitiered App Using ADO.NET


    Here are also another set of articles that cover concurrency.


    Tackle Data Concurrency Exceptions Using the DataSet Object


    Handling Data Concurrency Using ADO.NET



    I also have a sample chapter on concurrency I'll upload tonight and post the link so check back tomorrow for it.

    Hopefully the information in the above articles will be useful to you.

    Wednesday, June 20, 2007 7:24 AM