Click here to Skip to main content
11,932,677 members (59,931 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


30 bookmarked

Simple Shopping Cart

, 31 Dec 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
Simple shopping cart with session


This is a simple basket or shopping cart, that user can select some goods from list and add them to shopping cart. Also a user can remove or change the count of each good. In this article, I use session for storing the shopping cart user can submit for buying and shopping cart saved in database.

Using the Code

In Web, if you need to have common variable in all pages, you must save in session. Session variables store in server and for each user webserver creates a new session ID and session variables. You can store any object in session. I create a DataTable for storing shopping cart and set it in session variable.

Creating a session is very simple:

Session["variable-name"] = variable-value;

Session variable will remove after session time out (default time is 20 minutes), but if you want to remove a session variable:


In this project, I store DataTable in session. In Page_Load, if there is session variable it reads and if not, it creates an empty DataTable.

if (Session["basket"] != null)
	///read Basket_DataTable from session if exist
	Basket_DataTable = (DataTable)Session["basket"];
	//create an empty DataTable and Add some columns to it
	Basket_DataTable = new DataTable();

After this, all changes will be made on Basket_DataTable and finally Basket_DataTable saved in session.

Session["basket"] = Basket_DataTable; 

For adding items to shopping cart, I first search for item in Basket_DataTable and if it not found, item will add to shopping cart:

//search item in DataTable
bool Found = false;
for (int i = 0; i < Basket_DataTable.Rows.Count; i++)
    if (Basket_DataTable.Rows[i][0].ToString() == 
        Found = true;

In the above code, Request["ID"] is ID of Item.

Before adding Item, I search item ID in database and if it finds item, add to basket.

//add item to DataTable
if (Found == false)
    //search item in database
    DataAcess data = new DataAcess();
    string sql = "SELECT * FROM kala where id=" + Request["ID"];
    DataTable ret= data.exe_select(sql);
    //if it found in database add it to basket
    if (ret != null && ret.Rows.Count == 1)
        Basket_DataTable.Rows.Add(new object[] 
        { Request["ID"],ret.Rows[0]["name"].ToString()

For removing items from shopping cart, I add a link in each Item that send item id. To remove I search item and if it is found, it will be removed.

if (Request["DelID"] != null)
    //search and remove
    for (int i = 0; i < Basket_DataTable.Rows.Count; i++)
        if (Basket_DataTable.Rows[i][0].ToString() == 

Also I add a TextBox for each item, for item count. User can change item count and click update button and item count will change in DataTable.

for (int i = 0; i < GridView2.Rows.Count; i++)
    TextBox Tb=(TextBox) GridView2.Rows[i].FindControl("TextBoxCount");
    Basket_DataTable.Rows[i]["count"] = Tb.Text;
    Basket_DataTable.Rows[i]["total"] = Convert.ToInt32(Tb.Text)*

I also add some code for saving shopping cart in database, I save item id and item count in database.

DataAcess data = new DataAcess();
for (int i = 0; i < GridView2.Rows.Count; i++)
	String ID = GridView2.Rows[i].Cells[0].Text.ToString(); ;
	TextBox Tb = (TextBox)GridView2.Rows[i].FindControl("TextBoxCount");
	string sql = "INSERT INTO Basket (count, id)VALUES ({0}, {1})";
	sql = string.Format(sql, Tb.Text, ID);


In this article, I describe a simple shopping cart with session. It is simple but I hope it helps you. Also, you can find a simple and useful class for connecting to database (I don't mention in the article).

Have fun with C# coding.


  • 31st December, 2010: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

mohammad hajibegloo
Engineer neyshabur azad univeristy
Iran (Islamic Republic Of) Iran (Islamic Republic Of)
I had worked as programmer,project manager,web developer for more than 3 years, and have worked on programming personaly for more than 10 years.
I have worked in university as a teacher for 8 years.
my favorite langueges are : VC++,C#,ASP.NET,PHP

You may also be interested in...

Comments and Discussions

Questiondatabase not opening and coming in binary numbers Pin
Member 113469455-Jan-15 22:17
memberMember 113469455-Jan-15 22:17 
Questionuse object list as alternative option Pin
adriancs5-Dec-14 14:16
professionaladriancs5-Dec-14 14:16 
QuestionAbout Database Pin
Gharuboy30-Sep-14 23:00
memberGharuboy30-Sep-14 23:00 
SuggestionPut all shopping cart items in session ID that slows down website performance. Pin
pfsze26-May-14 9:06
memberpfsze26-May-14 9:06 
QuestionThanks Pin
Member 1083938323-May-14 4:03
memberMember 1083938323-May-14 4:03 
Questionthanks and q to u? Pin
shajahansaju15-May-14 19:32
membershajahansaju15-May-14 19:32 
QuestionNot saving in database Pin
ajay.shetti4-May-14 5:01
memberajay.shetti4-May-14 5:01 
QuestionHow to calculate sum of totalprice column Pin
Member 104991625-Feb-14 1:33
memberMember 104991625-Feb-14 1:33 
QuestionProject doesn't build. Pin
pitlab13-Jan-14 6:01
memberpitlab13-Jan-14 6:01 
QuestionSimple Shopping Cart Project Pin
Member 102478026-Oct-13 4:24
memberMember 102478026-Oct-13 4:24 
Questionsalam Pin
morteza66m30-Jun-13 21:35
membermorteza66m30-Jun-13 21:35 
GeneralMy vote of 1 Pin
Morteza Khaalili23-Jun-13 8:15
memberMorteza Khaalili23-Jun-13 8:15 
GeneralMy vote of 3 Pin
Je79-Apr-13 9:53
memberJe79-Apr-13 9:53 
Questionhi Pin
steffit15-Mar-13 5:10
membersteffit15-Mar-13 5:10 
AnswerRe: hi Pin
mohammad hajibegloo16-Mar-13 23:41
membermohammad hajibegloo16-Mar-13 23:41 
Questionshopping cart Pin
Yakov Dmitriy17-Nov-12 0:24
memberYakov Dmitriy17-Nov-12 0:24 
GeneralMy vote of 1 Pin
Galva_Delphi20-May-12 7:00
memberGalva_Delphi20-May-12 7:00 
Suggestion[My vote of 1] Not good Pin
widestination30-Mar-12 3:03
groupwidestination30-Mar-12 3:03 
GeneralRe: [My vote of 1] Not good Pin
mohamad hajibegloo30-Mar-12 22:52
membermohamad hajibegloo30-Mar-12 22:52 
GeneralRe: [My vote of 1] Not good Pin
lida.y28-Aug-12 23:18
memberlida.y28-Aug-12 23:18 
GeneralMy vote of 5 Pin
asdf1234hn6-Jan-11 22:03
memberasdf1234hn6-Jan-11 22:03 
GeneralMy vote of 5 Pin
Sunasara Imdadhusen5-Jan-11 0:17
memberSunasara Imdadhusen5-Jan-11 0:17 
GeneralMy vote of 1 Pin
g0got231-Dec-10 11:45
memberg0got231-Dec-10 11:45 
RantRe: My vote of 1 Pin
mohamad hajibegloo1-Jan-11 9:10
membermohamad hajibegloo1-Jan-11 9:10 
GeneralRe: My vote of 1 Pin
Sunasara Imdadhusen5-Jan-11 0:17
memberSunasara Imdadhusen5-Jan-11 0:17 
General[My vote of 2] Using SQL in UI Pin
Gagan111831-Dec-10 8:13
groupGagan111831-Dec-10 8:13 
GeneralMy vote of 1 Pin
Mark Nischalke31-Dec-10 8:02
mvpMark Nischalke31-Dec-10 8:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.151126.1 | Last Updated 31 Dec 2010
Article Copyright 2010 by mohammad hajibegloo
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid