1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

cookies for eshop basket

Discussion in 'C#' started by ktsirig, Dec 6, 2007.

  1. #1
    Hello!

    I want to create an eshop basket page for my website.

    In the page of each product there is a button which when I press sets a cookie with the value of the respective product_id, using the following code:

    <asp:Button ID="btbasket" runat="server" Text="Buy this product" />



    In the code behind file, I have the following:

    Protected Sub insert_cookie(ByVal sender As Object, ByVal e As System.EventArgs) _
    Handles btbasket.Click
    Dim MyCookie As New HttpCookie("eshopCookie")
    MyCookie.Value = MyCookie.Value & product_code
    Response.Cookies.Add(MyCookie)
    MyCookie.Expires = DateTime.Now.AddHours(12)
    End Sub

    Then, when the user wants to see all the things he/she has ordered, they visit the Basket.aspx page,

    which has this:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim colCookies As New ArrayList()
    For i As Integer = 0 To Request.Cookies.Count - 1
    colCookies.Add(Request.Cookies(i))
    Next

    grdCookies.DataSource = colCookies
    grdCookies.DataBind()
    End Sub



    and, in the .aspx file,

    <asp:GridView
    id="grdCookies"
    Runat="server"/>

    But my problem is that the gridview only shows 1 product_id, and, in particular, tha last product the user has selected a he browses the list of the available products...

    Is there something else I must do to store all selected product_ids in the cookie and then retrieve them in Basket.aspx page? Must I use different cookie each time?

    Thank you for your time...
     
    ktsirig, Dec 6, 2007 IP
  2. urstop

    urstop Peon

    Messages:
    224
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    The problem is you are inserting a cookie with the same name each time "eshopCookie", so since you are using the same name, the existing cookie value gets replaced by the new data, so at the end you are left with the last item u insterted thru the cookie. Try using differnt names, may be prefix with a number for each new cookie like 1_eshopCookie , 2_eshopCookie and your code should work fine.
     
    urstop, Dec 7, 2007 IP
  3. AstarothSolutions

    AstarothSolutions Peon

    Messages:
    2,680
    Likes Received:
    77
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Your code isnt picking up the current value of the cookie so you would need to do that to do what you are proposing.

    The other problem you will have once you have fixed this is that you are going to have a single long string for the product IDs (eg if you had products 12345 and 54321 in the cart it would have a string 1234554321) so you really need to insert some break between them to as well.
     
    AstarothSolutions, Dec 7, 2007 IP
  4. imvain2

    imvain2 Peon

    Messages:
    218
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #4
    If you want to use a more efficient method

    create a database table and call it basket or something

    When a user adds items to their cart:
    Add the product id, qty, date time, Session ID and IP as a new record

    You would also need to run a script that cleans out all cart items that have been in the cart for more then say 1 hour
     
    imvain2, Dec 9, 2007 IP
  5. AstarothSolutions

    AstarothSolutions Peon

    Messages:
    2,680
    Likes Received:
    77
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Alternatively dont clear them out but have a status field that can be changed. Knowing what people are adding to their basket but not going on to buy is a useful piece of MI
     
    AstarothSolutions, Dec 10, 2007 IP
  6. Brinked

    Brinked Well-Known Member

    Messages:
    777
    Likes Received:
    51
    Best Answers:
    0
    Trophy Points:
    160
    #6
    I agree with imvain, store it in a database table and setup a cronjob every xx minutes to clear it out

    Thank You Cookies
     
    Brinked, Dec 11, 2007 IP
  7. AstarothSolutions

    AstarothSolutions Peon

    Messages:
    2,680
    Likes Received:
    77
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Other than cron jobs dont exist in IIS
     
    AstarothSolutions, Dec 11, 2007 IP
  8. imvain2

    imvain2 Peon

    Messages:
    218
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Technically you could use the windows scheduler

    However, I use this method for my custom shopping carts, and I have include files that are included on every page. That is where I run my cart cleanup

    
    	CartTimeOut = 2 * (60 * 60) 'get milliseconds
    	CLEAN_DB_SQL= "delete from cart_items where unix_timestamp(now()) - unix_timestamp(dateadded) >= " & CartTimeOut
    	run sql code
    
    Code (markup):
     
    imvain2, Dec 11, 2007 IP