简体   繁体   中英

Problem with releated datatable when adding new rows to each datatable

I have 2 datatable In dataset object

TransactionReasons and TransactionSubReasons.

TransactionSubReasons datatable have foreignkey IDTransactionReason from TransactionReasons.

When I add new row to TransactionReasons(for example IDTransactionReason=1) and then adding new row to TransactionSubReasons. New added row's IDTransactionReason field equals to new added row's ID(in this example 1).Until this is every thing is good.

But when I want to update my database the problem is occured.

For updating I used tableAdapterManager.UpdateAll(MYDATSET);

How can i solve this problem?

Thank for your attention!!!

So, what is this problem / exception exacly?

EDIT:

This exception suggest that you have an ADO.NET constraint associated with your relation (in addition to the DB constraint), but during update your child table rows don't receive the correct parent id. Maybe the tableAdapterManager tries to update the child table first?

I suggest you try to split this into 2 operations. That is:

  1. Save the parent table (ensure in a separate program like SQL Management Studio that you got back the correct id's if they are auto generated

  2. Save the child table (ensure that the parent id is set properly on newly added rows

You can use a debugger to see row fields after save.

If you wanted this two operations in a transaction, have a lookhere

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM