Building An Online Shopping Cart Using C Sharp and ASP Net Part 1
Building An Online Shopping Cart Using C Sharp and ASP Net Part 1
1 / 16
Programme
Search C#Today
Living Book
i Index
j
k
l
m
n
j Full Text
k
l
m
n
Advanced
CATEGORIES
HOME
SITE MAP
SEARCH
REFERENCE
FORUM
FEEDBACK
ADVERTISE
SU
Next art
August 2
Reader Comments
ABSTRACT
In this article Juan Martnez discusses and implements the building blocks of an online shopping cart covering in this part item catalogs, item details, a cart and a checkout system. This acts as the
foundation for further articles, where Juan covers other functionality like administration pages and
setting up credit card payments.
Article
Usefu
Innov
Inform
15 resp
Article Discussion
Related Links
Index Entries
ARTICLE
Editor's Note: This article's code has been updated to work with the final release of the .Net framework.
As web developers we are required to face a wide variety of application needs. Each web site developed is unique
and furnished according to each client's specific needs. The fact is that websites are indeed fabricated for each
client with different specifications but every site shares some common characteristics. Those parts of the site that
share functionality features can be treated as separate applications to be reused.
In this case we will address the development cycle for one of the common blocks in today's websites - an Online
Shopping Cart.
We will analyze the development of a shopping cart as a group of components described clearly before we
implement them, thus allowing us to use this knowledge in areas other than ASP.NET. After a description of each
component the implementation will be explained using C#.
The application will be designed to work with a SQLServer database for storage. Application logic will be done
within the Web Form and presented to the user through the web browser. Core logic will reside in a separate C#
component using the code behind technique. It will also be .NET framework Beta 2 compliant.
It is assumed that you have regular knowledge of the C# language, web development knowledge and database
design basics.
http://www.csharptoday.com/content/articles/20010821.asp?WROXE...
2002-07-04
2 / 16
After these steps we will have a clear path of development and be ready to implement our online shopping cart in
the C# language.
Digging in the Online Shopping Cart Model
We will first take a look at a simplified diagram of an Online Shopping Cart. These are the functionality blocks to
be discussed.
These are the basic blocks to be implemented in our online store. It includes the indispensable functionality that
will be described in detail later. These blocks could be enriched further with more features, which will be covered
in later articles.
Defining requirements
As in every software development cycle, we need to define our requirements first so we can design software
capable of giving satisfaction to our customers.
Our online shopping cart application should do the following:
z Have a list of categories and subcategories.
z Items should be arranged in its corresponding subcategory.
z Items could be selected for category and home promotion.
z Each product should have an id, name, short and long descriptions, small and large images, stock and price.
z Users should be able to add products to the basket and remove them.
z The user should be given an order number and will be able to track it through an order tracking system.
http://www.csharptoday.com/content/articles/20010821.asp?WROXE...
2002-07-04
3 / 16
From our requirements we define the database schema. The tables are shown as a conceptual model, with all
tables used in this version of the shopping cart.
Catalog Section
The green part corresponds to the catalog section. It is composed of two tables. This is a very simple
arrangement in which the subsection table inherits the section Id. This way we can display the items in a
http://www.csharptoday.com/content/articles/20010821.asp?WROXE...
2002-07-04
4 / 16
section/subsection approach. As this approach will work in most cases, you could improve this to a completely
flexible design using recursive sections, which will inherit a parent Id.
Item Details
The item details part of our model is a trimmed down version of an item details design. We have only one table in
which we save the vital information of the item such as description, price and images. We have a couple of
Boolean values that are used to specify if the item should be displayed as a home or section item.
Shopping cart basket
The shopping basket is a simple table that is used temporarily to store the items selected by the user. We just
save the session Id, the item id and the quantity. We will implement this as an in-memory data table and
hold our selected items in a session variable. This table is shown to show you're the information that needs to be
retained during our session.
Checkout system
This is the most complicated part or our system. Upon checkout we create an order Id in the order table. Then the
items stored in the shopping basket are transferred and saved in our order Items table. The user information is
stored in the order details table. These two tables inherit the order Id. We have a fourth table which also inherits
the order Id. This is the order Progress table and it is used for order tracking. As progress is done, the online shop
administrator should add a record to this table indicating the progress done to date. This is then checked by the
buyer.
<configuration>
<appSettings>
<add key="connString" value="server=(local)\NetSDK;database=ShoppingCart;Trusted_Con
</appSettings>
</configuration>
To retrieve this information we use the following code snippet.
<%@
<%@
<%@
<%@
http://www.csharptoday.com/content/articles/20010821.asp?WROXE...
2002-07-04
<%@
<%@
<%@
<%@
Register
Register
Register
Register
TagPrefix="SC"
TagPrefix="SC"
TagPrefix="SC"
TagPrefix="SC"
5 / 16
http://www.csharptoday.com/content/articles/20010821.asp?WROXE...
2002-07-04
6 / 16
<html>
<head>
<title></title>
<script language="C#" runat="server"></script>
</head>
<body>
<font face="Verdana" size="-1">
<asp:DataList id="MySectionList" runat="server"
BorderColor="black"
BorderWidth="1"
GridLines="Both"
CellPadding="3"
Font-Name="Verdana"
Font-Size="8pt"
Width="200px"
HeaderStyle-BackColor="#aaaadd"
SelectedItemStyle-BackColor="Gainsboro"
>
<HeaderTemplate>
Sections
</HeaderTemplate>
<ItemTemplate>
<a href="catalog.aspx?sectionId=<%# DataBinder.Eval(Container.DataItem,"ca
§ionIndex=<%#Cont
<%# DataBinder.Eval(Container.DataItem, "catalogS
</ItemTemplate>
<SelectedItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "catalogSectionName") %>
</SelectedItemTemplate>
</asp:DataList>
</font>
</body>
</html>
We place some more html into the control and we drop a DataList object to handle the rendering of the
information. The <%# DataBinder.Eval(Container.DataItem, "catalogSectionName") %> code is
used to select specific information from the bound data and put it in place.
In our codebehind class we first have to declare our DataList as protected. We then do some database work. Once
we have the information we need ready from our database, we bind this data to our Web Control, the DataList.
Our User Control inherits the code behind class and that way they can work together. It is important to declare
your shared variables (such as Web Form Controls) as protected so that the can be reached within the code
behind class.
http://www.csharptoday.com/content/articles/20010821.asp?WROXE...
2002-07-04
7 / 16
We need to filter the three possibilities and create the proper query. This is done in the UcProductList class
with this code. After we have our query we bind it to a DataGrid Web Control to display the items in a table
arrangement.
All the controls work together to form catalog.aspx. The user controls make use of codebehind classes for
database access and bind the DataLists in our page.
Our catalog.aspx file should yield something like this:
http://www.csharptoday.com/content/articles/20010821.asp?WROXE...
2002-07-04
8 / 16
Item Details
Our next step in building our shopping cart is to show off the item details. We first will use some of the code done
for the catalog presentation before. We will take the basic html framework and three user controls. We will reuse
the header, section and subsection control.
The difference here will be to replace the item list control with a new item details control. We will use a data list
Web Control to display the items characteristics as well as its corresponding class in our code behind repository.
The code to do this simple task is as follows for the user control:
http://www.csharptoday.com/content/articles/20010821.asp?WROXE...
2002-07-04
9 / 16
>
<ItemTemplate>
<table>
<tr>
<td valign="top">
<img src="images/<%# DataBinder.Eval(Container.DataItem, "itemLargeImage") %>" wid
</td>
<td valign="top">
<span class="itemTitle"><%# DataBinder.Eval(Container.DataItem, "itemName") %></sp
<br>
<span class="itemText"><%# DataBinder.Eval(Container.DataItem, "itemLongDescriptio
<br><br>
<span class="itemText">Price: <%# (DataBinder.Eval(Container.DataItem, "itemPrice"
<br>
<span class="itemText">Stock: <%# DataBinder.Eval(Container.DataItem, "itemStock")
<br><br>
<a href="catalog.aspx?sectionId=<%=sectionId%>§ionIndex=<%=sectionIndex%>&subs
&subsectionIndex=<%=subsectionIndex
<span class="itemText"> | </span>
<a href="cart.aspx?cartAction=1&itemId=<%# DataBinder.Eval(Container.DataItem, "it
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
</font>
And our code behind class:
http://www.csharptoday.com/content/articles/20010821.asp?WROXE...
2002-07-04
http://www.csharptoday.com/content/articles/20010821.asp?WROXE...
2002-07-04
Cart = (DataTable)Session["ShoppingCart"];
}
CartView = new DataView(Cart);
CartView.Sort = "Item";
if (!IsPostBack) {
//Add new entry to the shopping cart
int myCartAction = 0;
if (Request.QueryString["cartAction"] != null) {
myCartAction = Int32.Parse(Request.QueryString["cartAction"]);
}
if (myCartAction==1) {
//Take the items details from the database
string myItemId = "0";
if (Request.QueryString["itemId"] != null) {
myItemId = Request.QueryString["itemId"];
}
string SQLQuery = "SELECT itemName, itemPrice FROM item WHERE itemId = " + myIt
String connString = ConfigurationSettings.AppSettings["connString"];
SqlConnection myConnection = new SqlConnection(connString);
SqlCommand myCommand = new SqlCommand(SQLQuery, myConnection);
myConnection.Open();
SqlDataReader dr2 = myCommand.ExecuteReader();
if (dr2.Read()) {
DataRow dr = Cart.NewRow();
dr[0] = myItemId;
dr[1] = dr2.GetString(0);
dr[2] = "1";
dr[3] = dr2.GetSqlMoney(1).ToString();
Cart.Rows.Add(dr);
}
}
BindCartList();
BindTotalList();
}
}
The functions to update and delete data are as follows:
http://www.csharptoday.com/content/articles/20010821.asp?WROXE...
2002-07-04
protected
double
double
double
double
void BindTotalList() {
mySubTotal = 0;
myTaxRate = 0.15;
myTax = 0;
myTotal = 0;
http://www.csharptoday.com/content/articles/20010821.asp?WROXE...
2002-07-04
Checkout System
The checkout system has the responsibility of saving the client info and selected items for processing. We use
three tables to save the client info. These are the following:
z orderData - Stores the date of the order and generates the order Id.
z orderItem - List of items bought, it also saves the price at the time of purchase.
z orderDetail - Saves shipping and billing information for the order.
shipping costs. This should be done accordingly to each store's necessities. We validate credit card information
also. This should be done using a specific provider like paypal.
z Confirmation - Here we present a summary of the information received and show the list of items to be bought,
as well as the grand total of the order. The user is asked to submit the information if it is correct.
z Summary - We finish the transaction and show the user the order number.
We post the info from the first to the second step. We then use Web Form Controls to ask for confirmation. If the
info is correct, then we register the order for our client.
We do need to implement some form validation before posting the information and implement some credit card
validation through an external provider or your own software.
Tracking your order
The final part of our online shopping cart application is the tracking system. We will implement a basic tracking
system which will ask the user for their email and order number. The system will then show a list of milestones
registered in the tracking database.
We use a single table to save the tracking information. It consists of the order id and the information of the goals
achieved. Upon valid email and order number we display the list of events below.
http://www.csharptoday.com/content/articles/20010821.asp?WROXE...
2002-07-04
http://www.csharptoday.com/content/articles/20010821.asp?WROXE...
2002-07-04
Technical improvements
z Build based on components for performance and possible code reuse
z Migrate to SQL Server database using stored procedures for performance
z Implement SSL in your server (a must but out of scope!)
z Implement real time credit card processing and charging.
Conclusions
The system presented shows the basis of a working online shopping cart. It involves the main aspects of an online
store which are the catalog of items, the item detail module, the shopping cart basket and the checkout module.
Every shopping cart online today must implement these basic parts to be functional. Hopefully this article has
given you the knowledge to build your own with ease and to add new functionality to meet your needs as ideas for
improvement have been given.
In future articles, we will cover some of the important requirements of a real - world shopping cart that have been
left out of this article for the sake of brevity - a management console to add, modify and delete categories, items
and orders; item reviews; SSL for credit card payments; and a discussion of working with a credit card processing
provider.
No
j n
k
l
m
n
j n
k
l
m
j n
k
l
m
j n
k
l
m
j Yes, Very
k
l
m
Innovative?
No
j n
k
l
m
n
j n
k
l
m
j n
k
l
m
j n
k
l
m
j Yes, Very
k
l
m
Informative?
Brief Reader Comments?
No
j n
k
l
m
n
j n
k
l
m
j n
k
l
m
j n
k
l
m
j Yes, Very
k
l
m
USEFUL LINKS
Related Tasks:
z Download the support material for this
z Enter Technical Discussion on this Artic
z Technical Support on this article - support@
z
z
z
z
z
Your Name:
http://www.csharptoday.com/content/articles/20010821.asp?WROXE...
2002-07-04
(Optional)
Related Sources
z asp101: http://www.asp101.com/samples/shopping.asp
z Payment Online: http://www.paymentonline.com/
z Power Designer:
http://www.sybase.com/products/enterprisemodeling/powerdesigner
z Pay Pal: www.paypal.com
i Index
j
k
l
m
n
HOME
Advanced
j Full Text
k
l
m
n
|
SITE MAP
Ecommerce
Data Access/ADO.NET
INDEX
Performance
Application
Development
SEARCH
z C#
z item detai
z checkout system
z online stor
z persistenc
z contents, displaying
z session va
z contents, updating
z sessions
z creating
z shopping c
z database design
z SQL Serve
z DataGrid
z tracking sy
z DataList control
z user contr
z DataTable object
z web contro
z designing
z web forms
z item catalog
z web.config
REFERENCE
FEEDBACK
ADVERTIS
SO
Security
Site Design
XML
Web Services
Graphics/Games
Mobile
Other Technologies
C#Today is brought to you by Wrox Press (www.wrox.com). Please see our terms and conditions and privacy
C#Today is optimised for Microsoft Internet Explorer 5 browsers.
Please report any website problems to [email protected]. Copyright 2002 Wrox Press. All Rights
http://www.csharptoday.com/content/articles/20010821.asp?WROXE...
2002-07-04