Assignment 7 - Shopping Cart
An online shopping cart can maintain data using cookies, a database, or a session.
The database approach is the most robust approach. It is used by Amazon and other high-quality
e-commerce sites. Advantages of this approach are that the data persists indefinitely
and the user can retrieve it from different computers.
Storing shopping cart data in a database also allows user behavior to
be analyzed (data mining) for insights on how to improve the site and its features.
Users typically are not required to sign-in to use a shopping cart since this could deter people
from putting items in their cart. This creates a dilemma since the web site needs a method for
tracking the state of individual users. Fortunately ASP.NET (and many other server-side
programming frameworks) automatically creates
a unique identifier for each user, the ASP.NET SessionID. It is a string of 24 random letters and numbers
that is stored in a cookie on the user's computer and is included with each request and
response. The session ID may be
retrieved programmatically with the code "Session.SessionID". When using a database shopping cart
this ID is written to the database along with the cart data.
ASP.NET generates a new session ID with each request unless some data is stored in the session.
We could add data in the page_load method with code something
like:
Session["PutSomethingInSession"] = "Go Vikings"
Since users can enter the web site from any of several pages a better solution is to add
data to the session in the Global.asax file. This file allows
us to write code for handling application events, such as the Session_start event. This event fires
when a new user arrives at the web site. Download this
global.asax file and
place it in your application root folder (remove the .txt extension). Note
that under the handler for Session_start it adds
some data to the session object. You can confirm that the session is not changing with each request
by using the trace feature (add Trace="true" to the page declaration).
Database: Retrieving product information from Amazon web services
is relatively slow so to avoid multiple requests to Amazon we
will store the essential product attributes in the shopping cart. Your
database needs a table named tblCart to store the cart items. Create a new table
in your database named tblCart with the following fields:

Note for Winter 2013: If you are using campus computers the "create table" function in
VWD will not work because of the version problem between VWD and Sql Server. Consequently
the sql statements needed to create new tables is being provided.
To create the new table in VWD right click on the database in Server Explorer and select New Query.
Run the following sql statement:
create table tblCart
(
SessionID NVARCHAR(50) NOT NULL,
ASIN NVARCHAR(50) NOT NULL,
qty INT NOT NULL,
title VARCHAR(200) NOT NULL,
artist VARCHAR(100) NOT NULL,
price FLOAT NOT NULL,
dateAdded DATETIME NOT NULL,
PRIMARY KEY (SessionID, ASIN)
)
ShoppingCart Class: The shopping cart class contains the underlying
functionality of a shopping cart: adding items, updating quantities, removing items,
listing items, etc. The shopping cart's functionality is a good candidate for a
class because:
- cart features are needed in several .aspx pages (productPage, SearchBrowse, ShoppingCart
and Checkout.)
- the cart represents a real-world object with properties and methods.
- the cart consist of a lots of code that is easily hidden behind a small class interface.
Add the shopping cart class to your application by creating a new class in you
your App_Code folder named ShoppingCart.cs. Paste the source
code for the shopping cart class into a new file in your App_Code folder.
This will add shopping cart functionality to your application as shown here:
Shopping cart properties and methods:
|
Inside the ShoppingCart class change the name of the database connection string from "MusicStoreSample2008"
to name used in your web.config file.
Project files: It is a good idea to copy your music store files to a new folder
for each assignment. This avoids confusion with grading, file modification dates,
and provides you with a backup copy. For this assignment copy all the files from
Musicstore1 to a new folder with a name something like Musicstore2. Files in the
App_Code and App_Data folder are not easily copied so keep only one copy of them.
1.
SearchBrowse.aspx - Items are added to the shopping cart from the
ProductPage and optionally the SearchBrowse page. The sample SearchBrowse.aspx (source)
page shows how to handle "Add to cart" click events and retrieve data from dataList
control.
When a use clicks on the "add to cart" button the click event is captured by the
button's parent control, the datalist. The datalist calls a handler for the event
that adds the information to tblCart using the ShoppingCart class.
Note: Amazon web services should not re-retrieve product information when the user
clicks the "add to cart" button. The call to Amazon Web services should be made only
the first time the page loads. This is done by calling it from inside
"if (!Page.IsPostBack) ..." within the Page_Load method.
2. ShoppingCart.aspx - The overall logic of the database
cart is shown in this basic
database shopping cart
(source code). Modify this sample to display product images,
item price information, shipping and total cost.
Temporary database version:
To submit your assignment for grading send an email with the URLs for your assignment
to:
- Professor Sandvig and the TA at
(note: this address is for homework assignments only. Please send correspondence
to
)
- cc. a copy to yourself
The subject line of your email should read "MIS324 AXX YourName"
where XX is the assignment number. Please check that your URLs are correct before
submitting them for grading. Files with incorrect URLs will not be graded.
When pages are connected via navigation (as in your Music store project) it is only
necessary to submit the URL of the first page.