store itemList in session
i have the follwong database structure
catgegory (CatId,CatType,CatName)
Product(ProductId,ProductName,CatId)
Service(ServiceId,ServiceName,CarId)
in the category Table (CatType is either product or services store as "P" OR "S")
im using SortedList to store session of product or service
my problem im using productId as Key in the sortedList however there might be situation where ServiceId and productId as identically i would like to get some adivce how i can redesign or improve this code thank you
code
Private Sub AddToCart(ByVal CartItem As CartItem)
Dim Cart As SortedList = GetCart()
Dim k As Integer = objTempCart.ProductId
If Cart.ContainsKey(k) Then
CartItem = CType(Cart(k), CartItem)
CartItem.objProduct.OrderQty = CartItem.objProduct.OrderQty + 1
CartItem.objProduct.Total = CartItem.objProduct.OrderQty * CartItem.objProduct.ProductPrice
Else
Cart.Add(k, CartItem)
CartItem.objProduct.OrderQty = 1
CartItem.objProd开发者_如何学Cuct.Total = CartItem.objProduct.OrderQty * CartItem.objProduct.ProductPrice
End Sub
Private Function GetCart() As SortedList
If Session("Cart") Is Nothing Then
Session.Add("Cart", New SortedList)
End If
Return CType(Session("Cart"), SortedList)
End Function
You could concat the CatType
and the ID
to get a unique string as key for the SortedList
(f.e. "P4711"
and "S12345"
).
But normally i would use two SortedList, one for every category. That makes the code less error-prone and more legible.
精彩评论