Results 1 to 16 of 16

Thread: Shopping Cart Woes... [RESOLVED]

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2002
    Location
    Cleveland, Ohio
    Posts
    185

    Angry Shopping Cart Woes... [RESOLVED]

    Hello Again!

    I am trying to create a shopping cart in ASP. I have been successful in that I can add single items to the shopping cart, however, I cannot seem to delete them from the shopping cart. Below is the code that I have for a delete item script, basically taken from asp101, but I can't seem to get it to work. Any suggestions?

    HTML Code:
    Sub DeleteItemFromCart()
    	Dim ItemID
    	
    	ItemID = Request.QueryString("Key")
    	
    	Set dctCart = Session("ShoppingCart")
    
    	If dctCart.Exists(ItemID) Then
    			dctCart.Remove dctCart(ItemID)
    	Else
    		Response.Write("Couldn't find that item.<br>")
    	End If
    	
    	Set Session("ShoppingCart") = dctCart
    	
    End Sub
    Thanks,

    Jim P.
    Last edited by jpiller; Jan 20th, 2005 at 10:32 AM. Reason: Resolved the issue
    "The Force will be with you, always."

    --Ben Kenobi--

  2. #2
    Big D Danial's Avatar
    Join Date
    Jul 2000
    Location
    ASP.Net Forum
    Posts
    2,877

    Re: Shopping Cart Woes...

    Can you expand on "Can not Delete"? Do you get any error message or it is printing out "Couldn't find that item."? If thats the case make sure you are passing hte right Item ID, use response.write to print out the content of ItemID variable, e.g response.write ItemID.
    [VBF RSS Feed]

    There is a great war coming. Are you sure you are on the right side? Atleast I have chosen a side.

    If I have been helpful, Please Rate my Post. Thanks.

    This post was powered by :

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jul 2002
    Location
    Cleveland, Ohio
    Posts
    185

    Re: Shopping Cart Woes...

    Yes, I have been using the response.write method to make sure the Key value is correct and it is....the output is that it cannot find the item...sorry about that.

    Thanks,

    Jim P.
    "The Force will be with you, always."

    --Ben Kenobi--

  4. #4
    Big D Danial's Avatar
    Join Date
    Jul 2000
    Location
    ASP.Net Forum
    Posts
    2,877

    Re: Shopping Cart Woes...

    Quote Originally Posted by jpiller
    Yes, I have been using the response.write method to make sure the Key value is correct and it is....the output is that it cannot find the item...sorry about that.

    Thanks,

    Jim P.

    It seems like there is something wrong with the Exist method, is it possible to post the code of the Exist method? Without seeing the code it would be hard to tell what causing the problem.
    [VBF RSS Feed]

    There is a great war coming. Are you sure you are on the right side? Atleast I have chosen a side.

    If I have been helpful, Please Rate my Post. Thanks.

    This post was powered by :

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jul 2002
    Location
    Cleveland, Ohio
    Posts
    185

    Re: Shopping Cart Woes...

    it explains the exists method here....

    http://www.w3schools.com/asp/met_exists.asp

    Jim P.
    "The Force will be with you, always."

    --Ben Kenobi--

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jul 2002
    Location
    Cleveland, Ohio
    Posts
    185

    Re: Shopping Cart Woes...

    The RemoveAll method works like a charm, but everytime I try and delete this item from the cart, it just can't find it????

    Jim P.
    "The Force will be with you, always."

    --Ben Kenobi--

  7. #7
    Big D Danial's Avatar
    Join Date
    Jul 2000
    Location
    ASP.Net Forum
    Posts
    2,877

    Re: Shopping Cart Woes...

    Quote Originally Posted by jpiller
    The RemoveAll method works like a charm, but everytime I try and delete this item from the cart, it just can't find it????

    Jim P.
    Oh, you are using Dictionary Object, i thought it was an custom made object. Anyhow make sure you are adding the item with a key properly.

    I would use a loop and display the item name and item key just to see if it actually exist. This will tell you whether the item actually exist in the dictionary object.

    Hope this helps.
    [VBF RSS Feed]

    There is a great war coming. Are you sure you are on the right side? Atleast I have chosen a side.

    If I have been helpful, Please Rate my Post. Thanks.

    This post was powered by :

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Jul 2002
    Location
    Cleveland, Ohio
    Posts
    185

    Re: Shopping Cart Woes...

    there's no rule that says the key can't be all numbers is there? For example, could a key be 123456789?

    Jim P.
    "The Force will be with you, always."

    --Ben Kenobi--

  9. #9
    Big D Danial's Avatar
    Join Date
    Jul 2000
    Location
    ASP.Net Forum
    Posts
    2,877

    Re: Shopping Cart Woes...

    Quote Originally Posted by jpiller
    there's no rule that says the key can't be all numbers is there? For example, could a key be 123456789?

    Jim P.
    As far as i know you can, I just tested it, it seems you can.

    Here is my test Code :

    VB Code:
    1. <%
    2.     Dim d  
    3.    
    4.     Set d = CreateObject("Scripting.Dictionary")
    5.     d.Add "a", "abc"  
    6.     d.Add 134, "Test"
    7.     d.Add "b", "bbc"
    8.  
    9.     dim a,k
    10.  
    11.     a= d.items
    12.     k = d.keys
    13.  
    14.     for i=0 to d.count-1
    15.         response.write "Key:" & k(i)  & " Value:" & a(i) & "<br>"
    16.     next
    17.  
    18.  
    19. %>
    [VBF RSS Feed]

    There is a great war coming. Are you sure you are on the right side? Atleast I have chosen a side.

    If I have been helpful, Please Rate my Post. Thanks.

    This post was powered by :

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Jul 2002
    Location
    Cleveland, Ohio
    Posts
    185

    Re: Shopping Cart Woes...

    okay, i've pretty much done my own version of the example on asp101. Here is the code that I have and I cannot for the life of me see the problem. The other issue that I'm having is that the cart won't add a quantity of a single item. In other words, if you run this code and add the same item over and over, it will add it as an individual item each time, which isn't supposed to happen. Any thoughts?

    Thanks,

    Jim P.

    HTML Code:
    <%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
    <!-- #include file="template.asp" -->
    
    <%
    
    Dim Cart
    
    'Check to see if the cart exists.  If not, then add one
    If IsObject(Session("ShoppingCart")) Then
    	Set Cart = Session("ShoppingCart")
    Else
    	Set Session("ShoppingCart") = Server.CreateObject("Scripting.Dictionary")
    	Set Cart = Session("ShoppingCart")
    End If
    
    'Add the item to the cart
    Sub AddToCart(ItemID, ItemCount)
    
    	If Cart.Exists(ItemID) Then	
    		Cart(ItemID) = Cart(ItemID) + ItemCount
    	Else
    		Cart.Add ItemID, ItemCount
    	End If
    	
    End Sub
    
    'Remove the selected item from the cart. *****  NOT WORKING  *****
    Sub DeleteFromCart(ItemID, ItemCount)
    	Response.Write("This is the ItemID: " & ItemID & "<br>")
    	If Cart.Exists(ItemID) Then
    		If Cart(ItemID) <= ItemCount Then
    			Cart.Remove(ItemID)
    		Else
    			Cart(ItemID) = Cart(ItemID) - ItemCount
    		End If
    	Else
    		Response.Write("Can't find that item in your shopping cart.")
    	End If
    	
    End Sub
    
    'Just simply for error checking...all keys and values seem to be right.
    Sub CheckKeys()
    
    	For Each Key in Cart
    	
    		Response.Write("<br>The value for key " & Key & " is " & Cart(Key) & "<br>")
    		
    	Next
    	
    End Sub
    
    'Show the contents of the cart
    Sub ShowCart()
    
    	Dim cnConn, rsGetDetails
    	Dim dblSubTotal
    	
    	dblSubTotal = 0
    	
    	Set cnConn = Server.CreateObject("ADODB.Connection")
    	
    	cnConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("online_wizard.mdb")
    	cnConn.CursorLocation = 3
    	
    	Set rsGetDetails = Server.CreateObject("ADODB.Recordset")
    	
    		Response.Write("<table width='60%' border='1' align='center'>")
    			Response.Write("<tr bgcolor='#FFFFFF'>")
    				Response.Write("<th>ISBN # / Item #</th>")
    				Response.Write("<th>Book Title</th>")
    				Response.Write("<th>Quantity</th>")
    				Response.Write("<th>Price</th>")
    				Response.Write("<th>Delete Item</th>")
    			Response.Write("</tr>")
    
    			For Each Key in Cart
    			
    			rsGetDetails.Open "SELECT * FROM tblBooks WHERE ISBN = '" & Key & "'", cnConn
    			
    			Response.Write("<tr>")
    				Response.Write("<td>" & Key & "</td>")
    				Response.Write("<td>" & rsGetDetails("BookTitle") & "</td>")
    				Response.Write("<td align='center'><input type='text' size='2' name='ItemCount' value='1'></td>")
    				Response.Write("<td>" & FormatCurrency(rsGetDetails("ListPrice")) & "</td>")
    				dblSubTotal = dblSubTotal + rsGetDetails("ListPrice")
    				Response.Write("<td><a href='cart_functions.asp?Action=delete&Key=" & Key & "&ItemCount=1'>Delete Item</a>")
    			Response.Write("</tr>")
    			
    			rsGetDetails.Close
    			
    			Next
    			Response.Write("<tr>")
    				Response.Write("<td colspan='5' align='center'><a href='cart_functions.asp?Action=clear'>Empty All Items From Cart</a></td>")
    			Response.Write("</tr>")
    		Response.Write("</table>")
    		
    		Response.Write("<br><br>Your Subtotal Is: " & FormatCurrency(dblSubTotal))
    		
    		Set rsGetDetails = Nothing
    		
    		cnConn.Close
    		Set cnConn = Nothing
    		
    End Sub
    
    'Select the action based on what has been selected by the user
    Select Case Request.QueryString("Action")
    
    	Case "add"
    		AddToCart Request.QueryString("ItemID"), "1"
    		
    	Case "delete"
    		DeleteFromCart Request.QueryString("Key"), Request.QueryString("ItemCount")
    		
    	Case "clear"
    		Cart.RemoveAll
    		
    End Select
    
    'Show the cart no matter what.
    ShowCart
    
    %>
    "The Force will be with you, always."

    --Ben Kenobi--

  11. #11
    Big D Danial's Avatar
    Join Date
    Jul 2000
    Location
    ASP.Net Forum
    Posts
    2,877

    Re: Shopping Cart Woes...

    Just tested your code, it seems like the problem is arising because you are using the key as a number. Add a charecter before the key e.g "ItemID" & Request.QueryString("ItemID"). That will resolve the problem.


    VB Code:
    1. Select Case Request.QueryString("Action")
    2.  
    3.     Case "add"
    4.         AddToCart "ID" & Request.QueryString("ItemID"), "1"
    5.  
    6.     Case "delete"
    7.         DeleteFromCart "ID" &  Request.QueryString("Key"), Request.QueryString("ItemCount")
    8.  
    9.     Case "clear"
    10.         Cart.RemoveAll
    11.  
    12. End Select
    [VBF RSS Feed]

    There is a great war coming. Are you sure you are on the right side? Atleast I have chosen a side.

    If I have been helpful, Please Rate my Post. Thanks.

    This post was powered by :

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Jul 2002
    Location
    Cleveland, Ohio
    Posts
    185

    Unhappy Re: Shopping Cart Woes...

    Danial,

    Thanks once again for all your help, but it still is not working. I changed the code as you suggested and now it's adding two "ID"'s at the beginning of the string and it's still not finding it...still says can't find that item in your cart. Could this be some kind of configuration problem with the server itself? I'm running win2k3 and IIS6. I wouldn't think that would be the problem, but you never know?

    Thanks again for all your help.

    Jim P.
    "The Force will be with you, always."

    --Ben Kenobi--

  13. #13
    Big D Danial's Avatar
    Join Date
    Jul 2000
    Location
    ASP.Net Forum
    Posts
    2,877

    Re: Shopping Cart Woes...

    Quote Originally Posted by jpiller
    Danial,

    Thanks once again for all your help, but it still is not working. I changed the code as you suggested and now it's adding two "ID"'s at the beginning of the string and it's still not finding it...still says can't find that item in your cart. Could this be some kind of configuration problem with the server itself? I'm running win2k3 and IIS6. I wouldn't think that would be the problem, but you never know?

    Thanks again for all your help.

    Jim P.
    No, its not working because your DeleteFromCart function is coded incorrectly. Here is the correct version. I have highlighted the secion in red. As you can see you need to convert a value to a number before you can do <= comparision. All variable in VBScript are Variant so you need to convert them to integer (cInt) or any other numerical form if you want to do numerical comparison.

    Hope this helps.


    '
    VB Code:
    1. Remove the selected item from the cart. *****  NOT WORKING  *****
    2. Sub DeleteFromCart(ItemID, ItemCount)
    3.  
    4.     Response.Write("This is the ItemID: " & ItemID & "<br>")
    5.  
    6.     If Cart.Exists(ItemID) Then
    7.  
    8.         [COLOR=Red]If cInt(Cart(ItemID)) <= cInt(ItemCount) Then[/COLOR]
    9.             Cart.Remove(ItemID)
    10.             Response.write "Item Removed"
    11.         Else
    12.             Cart(ItemID) = Cart(ItemID) - ItemCount
    13.             Response.write "Count Decreased"
    14.         End If
    15.     Else
    16.         Response.Write("Can't find that item in your shopping cart.")
    17.     End If
    18.  
    19. End Sub
    [VBF RSS Feed]

    There is a great war coming. Are you sure you are on the right side? Atleast I have chosen a side.

    If I have been helpful, Please Rate my Post. Thanks.

    This post was powered by :

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Jul 2002
    Location
    Cleveland, Ohio
    Posts
    185

    Re: Shopping Cart Woes...

    Danial,

    The script isn't working b/c the object that is in the cart isn't being seen by the Cart.Remove statement. It keeps saying that it doesn't find that object. The if statement you highlighted simply decides whether or not to delete all of that particular item or just the number specified by the user.

    What I don't understand is why in the heck the item is showing up in the cart when I display it, it shows up when I loop through it, but when I go to delete the item, it doesn't see it. There must be something I'm doing wrong and I cannot figure out what it is....

    Jim P.
    "The Force will be with you, always."

    --Ben Kenobi--

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Jul 2002
    Location
    Cleveland, Ohio
    Posts
    185

    Talking Figured out the issue....

    Of all the stupid things....None of the documentation that I read and none of the examples that I worked with explained this, but here's the issue. In my original code, the DeleteFromCart Sub read like this:

    HTML Code:
    Sub DeleteFromCart()
    	
    	
    	If Cart.Exists(Request.QueryString("Key")) Then
    		Cart.Remove Request.QueryString("Key")
    	Else
    		Response.Write("Can't find that item in your shopping cart.")
    	End If
    	
    End Sub
    The solution reads like this:


    Sub DeleteFromCart()


    If Cart.Exists(CStr(Request.QueryString("Key"))) Then
    Cart.Remove CStr(Request.QueryString("Key"))
    Else
    Response.Write("Can't find that item in your shopping cart.")
    End If

    End Sub


    When using Remove, your "Key" must be in string format, and b/c in ASP all variables are written as variant, I had to add the CStr function to convert it to a string.

    Thanks Danial for all your help!

    Jim P.
    "The Force will be with you, always."

    --Ben Kenobi--

  16. #16
    Big D Danial's Avatar
    Join Date
    Jul 2000
    Location
    ASP.Net Forum
    Posts
    2,877

    Re: Figured out the issue....

    Quote Originally Posted by jpiller
    Of all the stupid things....None of the documentation that I read and none of the examples that I worked with explained this, but here's the issue. In my original code, the DeleteFromCart Sub read like this:


    When using Remove, your "Key" must be in string format, and b/c in ASP all variables are written as variant, I had to add the CStr function to convert it to a string.

    Thanks Danial for all your help!

    Jim P.
    Quote Originally Posted by Danial
    All variable in VBScript are Variant so you need to convert them to integer (cInt) or any other numerical form if you want to do numerical comparison.
    Well I did kinda warn you about the variable type .

    Yes its good to work with a strong typed, but in your case your Delete function you needed to convert the ItevCount to integer for it to work. You do no need to use cstr to convert it to a string.

    Glad to know your problem is resolved.

    Good luck and happy learning

    [VBF RSS Feed]

    There is a great war coming. Are you sure you are on the right side? Atleast I have chosen a side.

    If I have been helpful, Please Rate my Post. Thanks.

    This post was powered by :

Posting Permissions

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



Click Here to Expand Forum to Full Width