Solved

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

Posted on 2008-10-23
12
248 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
ASP.NET e-commerce website 4 30
asp.net bundle 8 36
DataGridview Currency Formating? 22 39
Adjust the position 3 14
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…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now