Typed dataset with cascade delete using TableAdapter
-
Hi, We have implemented a Windows c# application using a typed dataset (ADO.NET) I've modified the relation so the childs get deleted when the parent is deleted. Delete action: cascade. In the function where I process all the changes in the dataset (Dataset.GetChanges()), I create for every table 1 adapter & call the .Update() function. When the first adapter (for parent) is created & called the .Update() function, the parent and all its childs are deleted due to the relationship (Delete action: cascade) A few lines further on, the adapter of the childs are called and the .Update() function throws an exception because the child is already deleted on the DB! This is very ennoying and I really don't know how to solve this problem. Any idea how to solve this? I tested it out with my own SqlCommand object and the deletion of the parent & childs did not cause any problems. I'd really love to keep the relationship so the childs get deleted automatically. This allows me to write less code :) Best regards, Jens
-
Hi, We have implemented a Windows c# application using a typed dataset (ADO.NET) I've modified the relation so the childs get deleted when the parent is deleted. Delete action: cascade. In the function where I process all the changes in the dataset (Dataset.GetChanges()), I create for every table 1 adapter & call the .Update() function. When the first adapter (for parent) is created & called the .Update() function, the parent and all its childs are deleted due to the relationship (Delete action: cascade) A few lines further on, the adapter of the childs are called and the .Update() function throws an exception because the child is already deleted on the DB! This is very ennoying and I really don't know how to solve this problem. Any idea how to solve this? I tested it out with my own SqlCommand object and the deletion of the parent & childs did not cause any problems. I'd really love to keep the relationship so the childs get deleted automatically. This allows me to write less code :) Best regards, Jens
I think your problem is with the order you are performing updates. You should make sure you perform updates in the correct order. Check out this MSDN walkthrough: http://msdn2.microsoft.com/en-us/library/4esb49b4.aspx[^] Hope this helps.
-
I think your problem is with the order you are performing updates. You should make sure you perform updates in the correct order. Check out this MSDN walkthrough: http://msdn2.microsoft.com/en-us/library/4esb49b4.aspx[^] Hope this helps.
Hi, Performing the updates in the correct order is not a good solution aswell. I have put the table adapter update of parent at the end, but this comes with another problem. Let's say you have to create the parent, the table adapter needs to first do the inserts of the parents, otherwise the children won't be added because of the relationship! Any other solutions? Best regards, Jens
-
Hi, Performing the updates in the correct order is not a good solution aswell. I have put the table adapter update of parent at the end, but this comes with another problem. Let's say you have to create the parent, the table adapter needs to first do the inserts of the parents, otherwise the children won't be added because of the relationship! Any other solutions? Best regards, Jens
Perhaps I was not clear enough in my previous post. When updating a data source containing multiple related tables, you should update in this order: 1. ChildTable Deleted Records 2. New, Modified, and Deleted ParentTable Records 3. New ChildTable Records 4. Modified ChildTable Records This order ensures that updates can be performed properly, taking into account the relationship between the tables. The link in my previous post will walk you through how to update records based on their status (Added, Modified, Deleted). Hope this helps.