problem using dictionary object for shopping cart

Results 1 to 2 of 2

Thread: problem using dictionary object for shopping cart

  1. #1
    Join Date
    Dec 1969

    Default problem using dictionary object for shopping cart

    I am using dictionary object in a session for a shopping cart code. I am storing the product code and quantity in the dict object.My shopping cart has a product code in a dropdown list and a text box for entering the quantity.On clicking the submit button, the data is submitted to the same page and the shopping cart view is also shown on the same page.<BR>The code is as below:<BR><BR>&#039;create a dictionary object, which stores Pcode as iItemID and Qty as iItemCount.<BR><BR>If IsObject(Session("cart")) Then<BR>Set dictCart = Session("cart")<BR>Else<BR>Set dictCart = Server.CreateObject("Scripting.Dictionary")<BR>End If<BR><BR>2)on submit call the below function which adds the Pcode and qty to the dictionary object.<BR>here i am passing product code (iItemID) and quantity (iItemCount) from the shopping form.<BR><BR>Sub AddItemToCart(iItemID, iItemCount)<BR>If dictCart.Exists(iItemID) Then<BR>dictCart(iItemID) = dictCart(iItemID) + iItemCount<BR>Else<BR>dictCart.Add iItemID, iItemCount<BR>End If<BR>End Sub<BR><BR>3) the below function displys the items in the shopping cart.<BR>&#060;%<BR>Sub ShowItemsInCart()<BR>For Each Key in dictCart<BR>%&#062;<BR>&#060;TR&#062;<BR>&#060;TD ALIGN="Center"&#062;&#060;%= Key %&#062;&#060;/TD&#062;<BR>&#060;TD ALIGN="Center"&#062;&#060;%= dictCart(Key) %&#062;&#060;/TD&#062;<BR>&#060;/TR&#062;<BR>&#060;%<BR>Next<BR>%&#062;<BR>end sub<BR><BR><BR>THE PROBLEM: I am facing is that the qty which i add for a particular product code adds to the value in the dictyionary code in a very abrupt manner. If i add qty=1 for ProductCode=pro001, it adds well and the shopping cart displays 1 qty for pro001.if i again add qty=1 for the same product, the shopping cart now shows qty=3 for pro001.every now and then an extra qty value creeps in and gets added to the pro001 quantity value in the dictionary object(dictCart).<BR><BR>hope i get some positive ideas on solving this problem.<BR>Thanks.

  2. #2
    Join Date
    Dec 1969

    Default Aside from the fact...

    ...that saving a Dictionary in a Session variable isn&#039;t a good idea (threading issues, but probably can ignore if this isn&#039;t a really busy site), I see nothing in that code to indicate a problem.<BR><BR>Possibly the SUBMIT is being processed twice? Possibly you want to see if you can put in a "lock" in JS client-side code to prevent that?<BR><BR><BR><BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts