Sharing the things I learn day by day

Saturday, December 4, 2010

SIMPLE ASP.NET SHOPPING CART

Hey All,

In this blog post i will explain to you how to create a very simple shopping cart by using a Data Table with C#. First of all you have to understand the main functionality in a shopping cart. I will explain to you in this blog how to,
  1. Create a cart using a data table.
  2. Add Items to the cart.
  3. Viewing the cart.
  4. Delete Items from the cart.
Creating the Cart

              The Shopping cart that I'm going to explain is based on a Air Ticket Reservation System which I did for my 1st year University project. Since a shopping cart is needed for each and every member of your site it is advisable to create the shopping cart when a user logs in to the site.
Here is how you can create the shopping cart for each user in the logging event of your website.

Here what is happening is that I'm creating a Data Table called cartData and adding the necessary columns to the Data Table. After creating the table finally I'm adding the table to a Session called "cartDetails" so that I can access my shopping cart from the Session when the user wants to add some item to the cart.

Adding Items to the Cart

        The next thing we want to do is to add items to the cart that we created.


Here what I'm going to do is that when the user clicks on the Add To Cart link the details that is shown in the Grid View will be added to the Cart that we created.
First I'm creating a array of type Object to hold the values in the Grid View and a Data Table to hold my shopping cart temporarily.
        object[] values; 
        DataTable cartTable;

Here First I'm declaring a array called values of type object. The values array will  hold the values in the Grid View temporarily.
          According to my website I'm allowing the visitors as well as registered members to search flight details. But when they try to to click on the Add To Cart link it will check whether the user has logged in or not. If he is not logged in he will be redirected to the log in page. I'm checking that by using a Session "LoggedIn".
          If the user is logged in, then I'm dynamically creating the values array. The size of the values array is the number of columns in my Grid View. After that I'm adding the data in the Grid View to my values array one by one by using the for loop.
          I added my shopping cart to a Session when the user logged in to the site. So in order to add details to the cart i can get the shopping cart from the Session. Since my cart is a Data Table I'm storing the reference to the shopping cart in the cartTable variable that I created.

cartTable = (DataTable)Session["cartDetails"];

         Then I can add data to the cart by using the Rows.Add() method by passing the values array as the parameter. Then the data will be added to the shopping cart.

cartTable.Rows.Add(values);
   
     Finally add the shopping cart back to the Session.

Session["cartDetails"] = cartTable;


 Viewing the Cart

     OK. Now I have added Items to the Cart. The next thing is to view the cart.


        
Here I'm using a Grid View to view  the Cart details to the user. Here I'm setting the AutoGenarateDeleteButton Property of the Grid View to True so that it will be easier for us to implement the delete method for the cart.















Here I'm getting the cart from the Session and storing it in a Data Table type variable called table.

table = (DataTable)Session["cartDetails"];

Then the total amount of the cart is calculated by using the loop. The I'm setting the Data Source of my Grid View to the Data Table table and binding the data to the Grid View.

GridView1.DataSource = table.DefaultView;
GridView1.DataBind();

Finally the total amount of the cart is displayed in a Text Box.

Delete Items from the Cart

     Since we set the AutoGenarateDeleteButton Property of the Grid View to True, you can see a Delete link in every row of the Grid View. We can use this link to remove items from the cart.
     Double click on the RowDeleting event of the Grid View. Here you can write the code to happen when the user click on the Delete link of a row. In our case we want to delete that item from our cart.














Here what is happening is that before I remove the row from the cart I'm getting the price, in my case the price of the ticket and reducing it from the total value of the cart and displaying it to the user by using the Text Box.
   I can get the index of the row that the user is trying to delete by using e.RowIndex .By using the row index I can easily delete the row from the cart by calling the Rows.Delete() method.

table.Rows[e.RowIndex].Delete();

table is  a variable of type Data Table that we created in the View cart stage. It has the reference to our cart. The above code deletes the record at the given row index.
After deleting the row add the cart back in to the Session as we did in the adding items to the cart stage.

Session["cartDetails"] = table;

To view the changes to the cart that the user made, set the Data Source of the Grid View back to the table and bind the data back. You can check whether the user has deleted all the rows by using,

if (table.Rows.Count==0)

condition and print the proper message to the user.

That's about it on how to create a simple ASP.Net shopping cart. Hope you'll will find my blog interesting.
Thanx.
Please leave you valuable comments....

11 comments:

Unknown said...

I have added source code for all the individual operations in the blog.What sort of a source code are you referring to?

Unknown said...

where is datalist control.so that user can see a collection of products and just click on button 'add to cart' and the product price & quantity should move to datatable...plz give the code for this..

Unknown said...

Well I think "Add items to the cart" section explains on how to do it. In this case the user sees the flight information and then adds the item to the cart using the "Add to cart" button which adds the selected flight to the shopping cart.

Unknown said...

Session[cartDetails] is not storing the dta in cartTable...so while viewing the cart no items

Unknown said...

Did you add the cartTable to the Session["cartDetails"] after populating the cartTable with records?

theju said...

When we select second item to add to cart, the data in the cart details show only the 1st item and not the second or the items added later. What is to be done for that?

Unknown said...

Please check whether you are inserting to the same cart added to the session. if not then the added items will not be displayed in the grid. When you are adding a item to the cart always get the cart from the session, then add to it and insert it back to the session. Then when displaying all ways take the cart from the session since it has the latest updated cart.

Unknown said...

hello i am using a repeater control that is populated data from micvrosoft sql server now what i need to find out is how do i get your source code working with my repeater thanks in advance

if possible could you provide a demo project of this source code

Unknown said...

Hi Yasir,

I have implemented the cart using GridView control.Is there a specific reason to use a repeater control in your project?

I did this project when I was in my first year of university.So i dont have the demo project with me now.

Unknown said...

what about updating a cell in shopping cart

safyrradel said...

Slot Machines at JT's Casino Resort | Hotel Rates & Reviews
JT's Casino Resort is 안양 출장안마 located in the beautiful and impressive Wynn resort 강릉 출장마사지 area. Located near the 포항 출장안마 Las Vegas 상주 출장마사지 Strip and under a 45-minute walk from 군포 출장마사지 McCarran International Airport