Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

See number of items and total cost in shopping cart on all web pages

Posted on 2008-10-23
12
251 Views
Last Modified: 2012-06-27
I am developing a web site in ASP.NET with VB.NET on MS Visual Web Devloper 2005.  I have a shopping cart based on a datatable and session variable.  It works great when you click the Add To Cart button (posts back) and it displays the number of items in the cart and the total cost for those items.  But I need it to display that count and total on all pages of the web site.  Code snippet is for the shopping cart that works when you click the button, followed by the page cose for where it is supposed to be displayed.  I want it to display the count and total on every page upon page_load.  I'm struggling with this.
Dim objDT As System.Data.DataTable 
Dim objDR As System.Data.DataRow 
 
    Private Sub Page_Load(ByVal s As Object, ByVal e As EventArgs)
        chkCopyBillingInfo.Attributes.Add("onclick", "javascript:data_copy();")
        If Not IsNothing(Session("Cart")) Then
            objDT = Session("Cart")
            
            dg.DataSource = objDT
            dg.DataBind()
            
            Dim myTotal As String = GetItemTotal()
            Dim SubTotal As String
            If IsNothing(myTotal) Then
                SubTotal = "0.00"
            Else
                SubTotal = myTotal
            End If
                
            Dim myCount As String = GetItemCount()
            Dim ItemCount As String
            If IsNothing(myCount) Then
                ItemCount = "There are no items in your Shopping Cart."
            Else
                ItemCount = myCount & " item(s)"
            End If
            
            lblSubTotal.Text = SubTotal
            lblItemCount.Text = ItemCount & " item(s)"
        End If
    End Sub
 
    Function GetItemTotal() As Decimal
        Dim decRunningTotal As Decimal
        	 
        For intCounter = 0 To objDT.Rows.Count - 1
            objDR = objDT.Rows(intCounter)
            decRunningTotal += (objDR("Cost") * objDR("Quantity"))
        Next
        
        Session("ProductTotal") = decRunningTotal
	 
        Return decRunningTotal
    End Function
 
    Function GetItemCount() As Integer
        Dim intQuantityCounter As Integer
        	 
        For intCounter = 0 To objDT.Rows.Count - 1
            objDR = objDT.Rows(intCounter)
            intQuantityCounter += objDR("Quantity")
        Next
	 
        Return intQuantityCounter
    End Function
 
 
on web page:
 
<table border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td rowspan="3" width="42" valign="middle"><a href="cart.aspx"><img src="images/cart.jpg" border="0" height="42" width="42"/></a></td>
                <td class="shopping-cart">Log into <a href="Login.aspx">My Account</a></td>
              </tr>
              <tr>
                <td class="shopping-cart"><asp:Label ID="Label1" runat="server" /> Item(s)&nbsp;&nbsp;
                $<asp:Label id="Label2" runat="server" /></td>
              </tr>
              <tr>
                <td class="shopping-cart"><a href="cart.aspx">View Cart</a>&nbsp;&nbsp;<a href="Checkout.aspx">Checkout</a></td>
              </tr>
            </table>

Open in new window

0
Comment
Question by:OVC-it-guy
  • 9
  • 3
12 Comments
 
LVL 10

Expert Comment

by:Bane83
ID: 22788714
Create a UserControl (ascx) and add the markup to that, then add the code to the Page_Load event of the UserControl.  You can then add this control to every page that needs it.

A tutorial on how to create and use UserControls:
http://www.dotnetheaven.com/UploadFile/prathore/WebUserControl05152007023645AM/WebUserControl.aspx
0
 

Author Comment

by:OVC-it-guy
ID: 22788926
I'm pressed for time.  The tutorial looks like it would work, if I had the time, but I was looking for a code suggestion in VB.NET.
0
 

Author Comment

by:OVC-it-guy
ID: 22788942
Does anyone see in my code why I only get the display of count and total when I add something to the cart (post back)?
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 10

Expert Comment

by:Bane83
ID: 22789390
Ah, I misunderstood your issue, I believe.  What code do you have in your button click event?
0
 

Author Comment

by:OVC-it-guy
ID: 22789542
There is no button to click for the display (see little screenshot) on every page of the web site.  I want it to display the lblCount and lblTotal upon page-load.  You know, so the customer has an idea of what the items they have already got in the cart will cost them as they continue to shop or navigate around the site.
But, since you asked, on the page that lists the individual products (template populated from categories of products in our database), the Add To Cart button,
<asp:Button id="btnAdd" runat="server" Text="Add To Cart" onClick="AddToCart" />
calls the AddToCart function
(see snippet)

Sub AddToCart(ByVal s As Object, ByVal e As EventArgs)
            objDT = Session("Cart")
            Dim Product = lblProduct.Text
            Dim blnMatch As Boolean = False
            Dim intCounter As Integer
 
            For Each objDR In objDT.Rows
                If objDR("Product") = lblProduct.Text Then
                    objDR("Quantity") += txtQuantity.Text
                    blnMatch = True
                    Exit For
                End If
            Next
            
            If Not blnMatch Then
                objDR = objDT.NewRow
                objDR("Sku") = lblSKU.Text
                objDR("Product") = lblProduct.Text
                objDR("Cost") = Decimal.Parse(lblPrice.Text)
                objDT.Rows.Add(objDR)
                objDR("Quantity") = txtQuantity.Text
                'Dim decSubTotal As Decimal
                'decSubTotal += (objDR("Cost") * objDR("Quantity"))
                'objDT.Rows.Add(objDR)
                'objDR("Item SubTotal") = Decimal.Parse(decSubTotal)
            End If
            Session("Cart") = objDT
 
            dg.DataSource = objDT
            dg.DataBind()
	 
            lblTotal.Text = "$" & GetItemTotal()
            lblCount.Text = GetItemCount() & " item(s)"
        End Sub

Open in new window

cart.jpg
0
 

Author Comment

by:OVC-it-guy
ID: 22789566
Actually, if the cart were empty, I'm trying to get it to display:
0 Item(s)  $0.00
but, as I said, I'm struggling to get it to display, except on the product page, after I add something to the cart.
0
 

Author Comment

by:OVC-it-guy
ID: 22789699
Here, on the product page, I've entered a quantity of 1 and clicked the Add To Cart button.  The page posted back and you can see the lblCount and lblTotal now carry the values.  That page works just fine.  As I said before, I just need count and total to be displayed on every page.  So, what am I missing in my code (first post - please ignore the chkCopyBillingInfo.Attributes.Add)?
0
 
LVL 10

Accepted Solution

by:
Bane83 earned 500 total points
ID: 22789771
Going by your code (the labels are appearing blank) the ONLY way that this can happen is if the code block in your Page_Load is not being run.  The only reason that it wouldn't get past

If Not IsNothing(Session("Cart")) Then

is that Session("Cart") is Nothing.  Can you set a breakpoint and confirm this?
0
 

Author Comment

by:OVC-it-guy
ID: 22790491
I'll give that a try.
0
 

Author Comment

by:OVC-it-guy
ID: 22790626
I think I'm on to something.  The two calculation functions I call, GetItemTotal() and GetItemCount(), are stepping through each objDR (datatable row).  My AddToCart function, in a sense, "creates" the datatable from my Session("Cart") and binds the data, that's why the little shopping cart only works after adding something to it.

Dim intCounter As Integer
Session("Cart") = objDT (or the reverse of this)
dg.DataSource = objDT
dg.DataBind()
lblTotal.Text = "$" & GetItemTotal()
lblCount.Text = GetItemCount() & " item(s)"

Your thoughts?
0
 

Author Comment

by:OVC-it-guy
ID: 22792028
Ok, for anyone else researching the same/similar issue, here's how it ended up with when I got it to work:

Public Sub Page_Load(ByVal s As Object, ByVal e As EventArgs)
        If Not IsNothing(Session("Cart")) Then
            objDT = Session("Cart")
            Session("Cart") = objDT
            dg.DataSource = objDT
            dg.DataBind()
            lblTotal.Text = "$" & GetItemTotal()
            lblCount.Text = GetItemCount() & " item(s)"

            lblTotal.Text = GetItemTotal()
            lblCount.Text = GetItemCount()
        End If
    End Sub
   
    Function GetItemTotal() As Decimal
        Dim intCounter As Integer
        Dim decRunningTotal As Decimal
              
        For intCounter = 0 To objDT.Rows.Count - 1
            objDR = objDT.Rows(intCounter)
            decRunningTotal += (objDR("Cost") * objDR("Quantity"))
        Next
      
        Return decRunningTotal
    End Function
 
    Function GetItemCount() As Integer
        Dim intQuantityCounter As Integer
              
        For intCounter = 0 To objDT.Rows.Count - 1
            objDR = objDT.Rows(intCounter)
            intQuantityCounter += objDR("Quantity")
        Next
      
        Return intQuantityCounter
    End Function
0
 

Author Closing Comment

by:OVC-it-guy
ID: 31509317
I would (if this system allows it) award some of the points to myself for figuring it out.  By I owe Bane83 for prompting me, having me look at this again logically and work it out.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question