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.

Help with ASP.NET Simple shopping cart

Discussion in 'C#' started by Camaro, Mar 20, 2010.

  1. #1
    Hi,

    I am trying to create a simple shopping cart. My background is PHP and decided to learn ASP, unfortunately there is a massive difference between the two and am hitting a lot of stumbling blocks. My problem is I'm trying to display a users shopping cart using a session variable. Using that session variable convert it to an array of product ids and then using those product id's to select the products from the database.
    Here is the code from the script:
           If addprod > 0 Then
                If Session("cart") Is Nothing Then
                    Session("cart") = addprod
                Else
                    Session("cart") = Session("cart") & "|" & addprod
                End If
                
            End If
                  
            WordArray = Split(Session("cart"), "|")
    For Each x In WordArray
                
                AccessDataSource2.SelectCommand = "SELECT * from products where products_id = " & WordArray(i)
                i = i + 1
            Next
    
    Code (markup):
    And then at the part of the page where the products will be displayed I've got:

    
           <tr><td colspan="3"><hr /></td></tr><tr><td colspan="3">
             Session String:<asp:Label ID="Label1" runat="server"></asp:Label></td></tr>       
               <% 
                   
                   For Each x In WordArray
                       
              %>
                <asp:Repeater ID="Repeater2" runat="server" DataSourceID="AccessDataSource2">
                <ItemTemplate>
                <tr>
                <td><%#Eval("products_name")%></td>
                <td>ass</td>
                <td>ass</td>
                </tr>   
                </ItemTemplate>
                </asp:Repeater>  
                
              <% 
                  i = i + 1
              Next
              %>
             </table>
            </td>
            </tr>
    
    Code (markup):
    It works to a certain degree however only displays the last item in the session multiple times down the page. Why is this happening and how do I alter the control to make it display the correct information??? Here is a sample of data:
    
    Sample session string: 6|4|2|8|12|11|8
    
    WordArray(0) = 6
    WordArray(1) = 4
    WordArray(2) = 2
    WordArray(3) = 8
    WordArray(4) = 12
    WordArray(5) = 11
    WordArray(6) = 8
    UBound.WordArray = 7
    
    Information displayed on product page:
    
    Product ID
    8
    8
    8
    8
    8
    8
    8
    
    
    Code (markup):
    It seems to only pick on the last value and repeat it for the total number of items in the array (this is cool though as long as it would print the right data).

    Any help appreciated.
    Cheers
     
    Camaro, Mar 20, 2010 IP
  2. islandhopper8

    islandhopper8 Active Member

    Messages:
    100
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    73
    #2
    I think it would be easier to create a database where you store the cart items and use the profile.username to store and request the cart items. This has the advantage that if customers come back and they didnt check out they will find there items still stored in their shopping cart. With the session variables they will be only available during the same session, closing a browser will loose your cart items and that can discourage shoppers when they have to shop again.
     
    islandhopper8, Jun 1, 2010 IP