Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET VB.NET
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: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>
 
 
 

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 3-Nov-12 2:53am
Edited 3-Nov-12 3:23am
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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:
 
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:
 
 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
  Permalink  
v2

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

  Print Answers RSS
0 George Jonsson 215
1 Kornfeld Eliyahu Peter 169
2 Zoltán Zörgő 139
3 PIEBALDconsult 130
4 OriginalGriff 120
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,342


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 5 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100