none
Add Child Datarow, but How? RRS feed

  • Question

  • Hi Everybody,


    i started Developing Ado.net 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


    address:

    id  | full name | street


    contact

    id | id_adress | type | number



    Greats from Austria

    Andreas

    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 = data.contact.NewcontactRow

     

    contact.addressID = address.ID

     

    data.address.AddaddressRow(address)

    data.contact.AddcontactRow(contact)

     

    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 ado.net knows that at the update from the DataSet to change the Primary Key to the Key the comes from the Database?


    Is Ado.net 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

    http://msdn.microsoft.com/msdnmag/issues/03/07/DataPoints/

     

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

    http://msdn.microsoft.com/msdnmag/issues/04/05/DataPoints/default.aspx

     

    Here are also another set of articles that cover concurrency.

     

    Tackle Data Concurrency Exceptions Using the DataSet Object

    http://msdn.microsoft.com/msdnmag/issues/03/04/DataConcurrency/

     

    Handling Data Concurrency Using ADO.NET

    http://msdn.microsoft.com/msdnmag/issues/04/09/DataPoints/

     

     

    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