locked
Two DataRows from two DataTables - FK on auto-incremented field RRS feed

  • Question

  • Hi all,

    I have two new DataRows I want to update into my database. Each one is from a different table, say Customer and Order, and the Order has a foreign key to Customer. The Customer PK is an auto-incremented field. When I create programmatically the Customer DataRow I want to have updated, I fill in everything BUT the id, which is normally generated by the DB when the Update occurs.

    The problem is that the Order DataRow (also programmatically created, but instead of setting the Customer_Id column I haveset the Customer_IdRow as the previously created Customer DataRow) does not know what the generated customer_id is. So, I get a FK violatopm when the Order Update occurs. Is there any way to make my ClientDataRow aware of the ID it was given in the DB?

    Cheers,

    markos

    Thursday, June 4, 2009 5:31 PM

Answers

All replies

  • You need to retrieve the newly inserted Identity value, then use it to populate the column in the child table.  See this MSDN article for info:

    http://msdn.microsoft.com/en-us/library/ks9f57t0.aspx

    Michael Asher
    • Proposed as answer by wheaties Thursday, June 4, 2009 6:39 PM
    • Marked as answer by deadlock_gr Thursday, June 4, 2009 6:57 PM
    Thursday, June 4, 2009 5:56 PM
  • From my experience the stored procedure method should be your preferred way of handling it.  That is a good link.
    Thursday, June 4, 2009 6:40 PM