Syllabus Schedule Project Labs Canvas Server Access CSS Tutorial Assignment 1 Assignment 2 Assignment 3 Assignment 4 Assignment 5 Assignment 6 Assignment 7 Assignment 8 Assignment 9

Assignment 7 - Shopping Cart

This week you will create a shopping cart. Shopping carts can store data using cookies, a database, or a session. The database approach is the most robust approach and is used by Amazon and other large e-commerce sites. Advantages of this approach are that the data persists indefinitely and the user can retrieve their cart items from different devices. 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:

database fields

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:

  1. cart features are needed in several .aspx pages (productPage, SearchBrowse, ShoppingCart and Checkout.)
  2. the cart represents a real-world object with properties and methods.
  3. 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:

ShoppingCart class

You do not need to make any changes to the shopping cart class.

File management: To avoid confusion with grading it is a good idea to copy your music store files to a new folder for each assignment. For this assignment copy all the files from Musicstore1 to a new folder with a name something like Musicstore2. An application can have only one set of App_ folders, such as App_Code and App_Data, so do not copy them.

Remove absolute paths: After you have copied your music store files to a new folder check to make sure that the internal paths are relative (not absolute). For instance, the paths to your masterpage should be "Masterpage.master" not "~/musicstore1/masterpage.master." Similiarly, the register tags for user controls (located in the master page) should be "controls/leftMenu.ascx" rather than "~/musicstore1/controls/leftMenu.ascx." You do not want any of the files in folder Musicstore2 linking to files in Musicstore1.

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.

  1. Add Eval tags to the DataList ItemTemplate to display all required fields (image, price per item, total price per item).
  2. The .ToString() method provides an easy way to display prices in currency. Example: to display a variable "TotalCost" as currency use: TotalCost.ToString("C")
  3. Display cart subtotal, shipping and total cost as shown in the sample site.

Submission instructions: Submit assignments via the Canvas course management system. Submit the full URL for each exercise in the assignment, listing the URLs in the same order that they are listed in the assignment. To minimize typos in URLs it is strongly recommended that you copy the URLs from the address bar of the browser rather than trying to type them. Incorrect URLs will not be graded and no credit will be given.

When pages are connected via navigation (as in your music store project) it is only necessary to submit the URL of the first page.