Hot-Link Menu in Right-Side Column

Wal Mart Online → Store Pickup → Free Shipping

Add CartItem Class

The next step is to add the CartItem class. In case you've forgotten how to add a class, you can review how we created the Product Class. In a similar, or even the exact same, manner, create the CartItem Class with the code below:

Imports Microsoft.VisualBasic

Public Class CartItem
    Public SelectedItem As Product
    Public Quantity As Integer

    Public Function Display() As String
        Return SelectedItem.Name & " (" &Quantity.ToString() & " at " _
            & FormatCurrency(SelectedItem.UnitPrice) & " each)"
    End Function
End Class

After the above code is entered, and you have set Order.aspx.vb as the start page, run the program, to make sure everything is still good. Although, we have entered the class, since we aren't using it, it should have no effect yet. Change the selected product to Hippie and the screen you should be seeing is:

VB.NET Browser View of Hippie as Selected Product

Figure CC-22: Change Selected Product to Hippie

Wow, what a cutie!!! So that's what happens when you have 40 years of Free Love, Peace and Doing Your Own Thing. Where do I sign up?

Buy from Centurion

Next, we will set the PostBackUrl property of the Continue Shopping button on the DisplayCart page, so that it jumps back to the order page. Performing a Single-Click on the ContinueShopping button will bring up the Properties Page somewhere on your screen. Left-Click on the values column of the PostBackUrl property on the btnContinue properties page as indicated in the diagram below:

Set Continue Shopping PostBackUrl Property

Figure CC-23: Select PostBackUrl Property of Continue Shopping Button

Help Support Mr. Centurion - Donate or Visit any Sponsor

No Purchase Necessary.

Help us keep this site operational by making a donation. Any amount is greatly appreciated. OR, if you visit our sponsor, they will make a donation for you, just for visiting.

Thank you

Press the Select Url button to the right of the PostBackUrl Value Column (right where the arrow is pointing), to bring up the Select URL dialog box as indicated in the diagram below:

Select Order Page as PostBackUrl Dialog Box

Figure CC-23: Select PostBackUrl Dialog Box

Excellent Tech Support - Centurion Approved

Switch back to the Design View page for Orders.aspx and change the PostBackUrl on the Go To Cart button to jump to the DisplayCart page, like we just did for the Continue Shopping button in the example above. Completing a circuit where we can freely navigate between the pages.

To make your personal navigation experience safer and more rewarding, check out the latest in marine navigation equipment available through

Centurion Online → Electronics → Marine Electronics

Once you have divested yourself of all that unnecessary disposable capital @ Centurion Online, come back and run the program jumping between the pages using the Go To Cart and Continue Shopping buttons.

Create GetCartContents Function Using Session State

A function to return the Cart Contents is needed. For this application, we will use SessionState to hold the contents of the cart. Enter the following function in the Order.aspx.vb module:

'Function to return the contents of the shopping cart held in session state
Private Function GetCartContents() As SortedList

    'Create a variable to hold the session name so it will be insertedable by Intellisense.
    Dim SessionName As String = "Cart"

    'If no shopping cart exists yet, create one.
    If Session(SessionName) Is Nothing Then
        Session.Add(SessionName, New SortedList)
    End If

    'Convert Session Variable to SortedList Data Type and return
    Return CType (Session(SessionName), SortedList)
End Function

It is good practice to run the code as you add the functions, just to make sure you aren't making any fatal errors along the way. Run the program, and change a few selections, to make sure you haven't lost functionality as we proceed. I try to add new functions, so that as I add them, the code still functions, which means entering code at the lowest level first, then working your way up.

Visit Our Sponsor to Help Support Our Site - No Purchase Necessary

The next code snippet we need to add is a Sub to add an individual item to the Users Shopping Cart. Enter the code below to process adding an item to the cart:

'Sub to add selected item to the Current Shopping Cart Contents
Private Sub AddToCart(ByVal ItemToAdd As CartItem)

    'Create a SortedList container for the selected item.
    Dim CurrentCartContents As SortedList

    'Create a String to Hold the Product ID
    Dim ProductCode As String

    'Call Function to Place Selected Item in SortedList Data Type
    CurrentCartContents = Me.GetCartContents

    'Get The ProductID to use as a key
    ProductCode = SelectedProduct.ProductID

    'Check to see if item is already in Cart
    If CurrentCartContents.ContainsKey(ProductCode) Then
        ItemToAdd = CType(CurrentCartContents(ProductCode), CartItem)
        ItemToAdd.Quantity += CInto(txtQuantity.Text)
        CurrentCartContents.Add(ProductCode, ItemToAdd)
    End If
End Sub

If you would like like to see a shopping cart in action, so you can see where we are headed with this, take a visit to:

Wal Mart Online → Store Pickup → Free Shipping

In order to continue with this guide, and continue the discussion on Adding the Shopping Cart Class Press the Button below:

Visit Our Sponsor to Help Support Our Site - No Purchase Necessary


Visual Basic

Introduction to VB.NET

.NET Framework

VS2008 IDE

How VB is Compiled

Start Visual Studio

Windows Form App

Save Your Work

VB OOP Programming

Visual Basic Code

Exit Code

Button Event Code

Coding Recommendations


Error List Window

Comment Syntax

Help Window

Language Essentianl

Built-In Data Types

Declare Variables

Declare Constants

Code Arithmetic Expressions

Assignment Statements

Operator Precedence

Type Casting

Math Class

String Declaration

Conversion Functions

Conversion Methods

Formatting Functions

String Formatting

Variable Scope


Nullable Types

Loop Constructs

For Next Loop

Do While Loop

Do Until Loop



Exit Do | Exit For


Nested Loops


Array Declaration

Rnd( ) Function

Listbox Control


Parallel Arrays

Key Event Args

Dynamic Arrays

Redimension Array

Set Breakpoint

Start Debugger

ReDim Preserve

MultiDimensional Arrays

DataGridView Control

Length and Sort Methods


Pad Right

Split Method

IsNumeric Function

Multiform Projects

Add Form To Project

Form Object Methods

Form Show Method

ShowDialog Method

Form Close Method

Form Accept Button

Multiform Project Example

ASP.NET Web Programming

Create Data Source

Configure Access Data Source

Add Product Class

Extract Local Database Data

Order PageLoad VB Code

Add New Web Page

Set Start Page

Display Cart Aspx Code

Display Cart Design View

Sorted List Definition

VB.NET Session State

Create CartItem Class

GetCartContents Function

Add To Cart Event Handler

Remove Cart Item Event

Clear Cart Event Handler

Can't open VisitorHistory.txt for reading