M
MattB
Hi. I have an ecommerce application that has a shopping cart as many do.
I've created a way to automatically add other items to the cart if
certain items are added, like "Get a fee t-shirt if you buy this" kind
of stuff.
The DataTable that holds the cart items has a column for "ID" (primary
key) and parentID that references another item's ID for items added with
another, so they can be lumped together. This lets me delete both the
main item and the free t-shirt item if the main item is deleted.
The method I have to do this is pretty crude:
I in a DataTable DeleteCommand, I just create a dataview to find the
child items with a RowFilter: "parentID = " & e.Item.ItemIndex("ID") and
delete those too.
This has worked for a while, but a client was making an item that would
add another item that would add another item. So there is now a
recursive relationship and my delete logic fails leaving "orphaned"
items that have a parentID, but the parent item is gone. This violates
the business rules (no free t-shirt without qualifying item).
So is there a best practice for this kind of thing? I played with
DataRelations a little, but I always wound up violating constraints if I
added an item with no child. Maybe I'm just doing it wrong, but if
anyone has a suggestion or can point me to a good link that illustrated
a recursive delete from a datatable, I would certainly appreciate it.
Hope this made sense. Thanks!
Matt
I've created a way to automatically add other items to the cart if
certain items are added, like "Get a fee t-shirt if you buy this" kind
of stuff.
The DataTable that holds the cart items has a column for "ID" (primary
key) and parentID that references another item's ID for items added with
another, so they can be lumped together. This lets me delete both the
main item and the free t-shirt item if the main item is deleted.
The method I have to do this is pretty crude:
I in a DataTable DeleteCommand, I just create a dataview to find the
child items with a RowFilter: "parentID = " & e.Item.ItemIndex("ID") and
delete those too.
This has worked for a while, but a client was making an item that would
add another item that would add another item. So there is now a
recursive relationship and my delete logic fails leaving "orphaned"
items that have a parentID, but the parent item is gone. This violates
the business rules (no free t-shirt without qualifying item).
So is there a best practice for this kind of thing? I played with
DataRelations a little, but I always wound up violating constraints if I
added an item with no child. Maybe I'm just doing it wrong, but if
anyone has a suggestion or can point me to a good link that illustrated
a recursive delete from a datatable, I would certainly appreciate it.
Hope this made sense. Thanks!
Matt