Click here to Skip to main content
15,894,546 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
hi all, am quite new to asp.net am trying to implement an 'add to cart' button which is generated in a listView
on clicking the imagebutton am trying to add the the product id and quantity=1 to my sortedlist Session
i tried to use command argument but no success
am i doing it right ?

ASP.NET
<asp:ListView  ID="listview1" GroupItemCount="3" runat="server">
        <LayoutTemplate>
            <div id="itemListView">
                <asp:PlaceHolder runat="server" ID="groupPlaceHolder" />
            </div>
        </LayoutTemplate>
        <GroupTemplate>
            <div style="clear: both;">
                <asp:PlaceHolder runat="server" ID="itemPlaceHolder" />
            </div>
        </GroupTemplate>
        <ItemTemplate>
            <div class="lvProduct">
                
               
                <div class="productDisplayImage">
                        <asp:Imagebutton  imageURl='<%# Eval("imageUrl") %>'
                       PostBackUrl= '<%#string.concat("~/productDetail.aspx?pid=",Eval("pid")) %>'
                       
                        width="175px" Height="160px" runat="server"/>
                        
                </div>
                
                <div class="productPriceDisplay">
                        Price: $<%# Eval("price")%> <asp:ImageButton imageUrl="~/assignment/images/addToCartButton.png" ImageAlign="top" 
 onCommand="addToCart" commandArgument= '<%#string.concat("Eval("pid")) %>'                        
                        Width="100px" height="25px" style="margin-left:10px;" runat="server"/>
                </div>
            </div>
        </ItemTemplate>
        <Itemseparatortemplate>
            <div class="itemSep">
            </div>
        </ItemSeparatorTemplate>
        <GroupSeparatorTemplate>
            <div class="groupSep">
            </div>
        </GroupSeparatorTemplate>
        <EmptyDataTemplate>
        </EmptyDataTemplate>
    </asp:ListView>


VB
Protected Function addToCart(ByVal sender As Object, ByVal e As CommandEventArgs) As Integer



        Dim pid As String = e.CommandArgument.ToString


        If Not IsNothing(Session("cart")) Then
            Dim slCart As SortedList = CType(Session("cart"), SortedList)
            slCart.Add(pid, "1")
            Response.Redirect("~/testing.aspx")
        Else
            Dim slCart As New SortedList

            slCart.Add(pid, "1")
 Response.Redirect("~/testing.aspx")

        End If

        Return Nothing
    End Function
Posted
Updated 3-Nov-12 2:23am
v2

1 solution

The issue you are having is because the session variable "cart" is nothing/null when the addCart method is fired. I have updated your code as below:

VB
Protected Function addToCart(ByVal sender As Object, ByVal e As CommandEventArgs) As Integer



        Dim pid As String = e.CommandArgument.ToString


        If Not IsNothing(Session("cart")) Then
            Dim slCart As SortedList = CType(Session("cart"), SortedList)
            slCart.Add(pid, "1")
            Response.Redirect("~/testing.aspx")
        Else
            Dim slCart As New SortedList

            slCart.Add(pid, "1")
            Session("cart") = slCart '<-- Missing this line
            Response.Redirect("~/testing.aspx")

        End If

        Return Nothing
    End Function



To further enhance the mechanics of your page logic and to work with good coding practice, is to define your session cart variable as a class member:

VB
Private Property cart() As SortedList
       Get
           If Session("cart") Is Nothing Then
               Session("cart") = New SortedList
           End If
           Return Session("cart")
       End Get
       Set(ByVal value As SortedList)
           Session("cart") = value
       End Set
   End Property
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900