1 / 50

PLACING ORDERS-SHOPPPING CARTS

PLACING ORDERS-SHOPPPING CARTS. Introduction. There are several stages to the order process, starting with an order page, which allows customers to add product s to a shopping cart.

dsaunders
Download Presentation

PLACING ORDERS-SHOPPPING CARTS

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PLACING ORDERS-SHOPPPING CARTS

  2. Introduction • There are several stages to the order process, starting with an order page, which allows customersto add products to a shopping cart. • Once customers have selected their order items, theycan then proceed to the checkout, where the delivery address and credit card details need to becollected. • Finally, you create the order in the database, and all the updates to the database as well as the real delivery of the products are going to be done.

  3. Essential Background • Learn how to create custom classes • See how to use the Session object to store the shopping cart • Learn about the ObjectDataSource control

  4. The Order Process • Before building the order pages, you must work out the process of ordering items. This will give you anindication of exactly what you will need. Following are the things you will need: • An order page, where you can select the product items. • A shopping cart, to store the selected products. • A page to collect the delivery details and credit card payment.

  5. The Order Process-Continue • Each of the previous pages needs some thought, with the process of ordering and storing data worked out inadvance. • For example, you must decide whether to have an order page that is separate from the catalogue page. Keeping them the same would mean that you can simply add a button alongside each item. This would add the item to the shopping cart. • Alternatively, you could have a text area allowingthe user to enter the number of items to be added. This makes the page a little harder to code.

  6. Where to store orders • Once you’ve decided on the way orders will be performed, you can decide where to store them before • the order is confirmed—the shopping cart. • There are places to store shopping carts: • In a database—As the user adds items to the cart, the items could be added to a table in thedatabase. When the order is confirmed, the entries could be copied into the OrderItems table.One problem with this approach is that if the user leaves the site without confirming the order,then the shopping cart table will have unused data in it, which will need to be removed. • In the Profile—The Profile is a feature of ASP.NET 2.0 that allows storage of data against a user.We won’t be using the Profile, but one problem with using the Profile for storingthe shopping cart is that the Profile is meant for long-lived data—data that persists across usersessions. Some sites allow shopping carts to keep their data for when you come back to the site. • In the Session —The Session contains data about the active session. It starts when you firstaccess the site and ends when you exit the site (plus a timeout value). Any data stored in thesession will be held only while you are browsing the site.

  7. Developing a Shopping Cart • Many ecommerce web sites require a shopping cart as an essential part. There are many ways to develop these shopping carts. • Some of them are - cookie based, • session based and • database driven. • Each technique has advantages and disadvantages of its own. • We will explore each technique with code sample and finally present you a generic solution that will work in any of these situations.

  8. Using Cookies for state storage • Any shopping cart essentially needs to store product details such as product code, product name, unit price and quantity. • You will be presenting a product catalog to the user from which he can select the products. He may also navigate to other parts of the site while he is shopping. You need to maintain his selection across the pages so that finally when he visits the shopping cart page you can show collective details there. • Cookies can be used to preserve this state information across the requests. In ASP.NET cookie is represented by a class called HttpCookie. We will be using multi-value cookies for our example.

  9. Developing a simple product listing page • We will first build a simple web form that lists the Products table of the database in a DataGrid. • Add a web form called ProductCatalog.aspx to your project • Drag and drop a DataGrid control on it. • Write a function called BindGrid() as shown below:

  10. BindGrid() private void BindGrid() { SqlDataAdapter da=new SqlDataAdapter("select * from products",@"data source=.\vsdotnet;initial catalog=_________;user id=__"); DataSet ds=new DataSet(); da.Fill(ds,"products"); DataGrid1.DataSource=ds; DataGrid1.DataBind(); }

  11. In the Page_Load event handler private void Page_Load(object sender,System.EventArgs e) { if(!Page.IsPostBack) { BindGrid(); } }

  12. In the SelectedIndexChanged event of the DataGrid private void DataGrid1_SelectedIndexChanged (object sender,System.EventArgs e) { HttpCookie c=null; if(HttpContext.Current.Request.Cookies["shoppingcart"]==null) c=new HttpCookie("shoppingcart"); else c=HttpContext.Current.Request.Cookies["shoppingcart"]; string itemdetails; itemdetails=DataGrid1.SelectedItem.Cells[1].Text + "|" + DataGrid1.SelectedItem.Cells[2].Text + "|" + DataGrid1.SelectedItem.Cells[3].Text; c.Values[DataGrid1.SelectedItem.Cells[1].Text]=itemdetails; Response.Cookies.Add(c); }

  13. Shopping Cart Cookies • Here, we created a cookie called shoppingcart. This cookie further contains subkey-value pairs. Based on user selection we simply add sub keys to this cookie with product id as the key. Then we write that cookie to Response.Cookies collection. • Drag and drop a button control on the web form and write the following code in the click event handler.

  14. click event handler • Here, we are simply navigating to the cart.aspx page which displays the shopping cart. • So we also need to create the shopping cart web form private void Button1_Click (object sender, System.EventArgs e) { Response.Redirect("cart.aspx"); }

  15. Creating the shopping cart web form • Add another web form to your project called cart.aspx • Create a class called CShoppingCartItem as shown below:

  16. The Code for the CShoppingCartItem

  17. Continue • This class is going to represent one item of the shopping cart. • Drag and drop a DataGrid on the web form. • Create a function called FillCartFromCookies() as shown below • Here, we are reading the cookies that we set previously and constructing CShoppingCartItem instances based on the selected values. These instances are then added to an ArrayList. Finally, this ArrayList is bound with the DataGrid.

  18. Code for the FillCartFromCookies()

  19. Write some code for the Recalculate event • Drag and drop a button called Recalculate and write following code to its click event handler. • This code calculates the total amount of the items selected based on the quantity entered and displays it in a label.

  20. The Code for the Recalculate event

  21. Code to delete items from the cart

  22. Problems with the above Shopping Carts • We saw how to use cookies to preserve shopping cart values. • This approach is quick and easy to code but has one big disadvantage. • Not all browsers will have cookies enabled. Hence, you should use this technique with care. • NOTE I will give you a full working example using a Shopping Cart using COOKIES

  23. Shopping Carts with Sessions • In the previous slides we saw how to create a shopping cart using cookies. • Continuing the concept further in the following slides we will illustrate how to use session variables to create a shopping cart. • First we again have to develop a simple product listing page. • We will then build a simple web form that lists Products table coming from our database in a DataGrid control. • We also write a function called BindGrid() as shown below:

  24. Code for the Datagrid

  25. In the Page_Load event handler

  26. Code in the SelectedIndexChanged event of the DataGrid

  27. What Are Sessions? You can use the Session objects to store values that are global rather than page-specific for either a particular user (the Session) or to all users (the Application).The Session variables are stored on the server. Client browsers are then attached to the session through a cookie. As a result, the client must have cookies enabled in the browser for Session and Application variables to work.How to Use Session Variables The power of the Session object comes from the fact that it can store variables that are global to just that specific user; as a result, each user can have their own individual value for that variable. Session objects are not always created automatically for every user when they enter your application. However, storing or accessing a variable in the Session object creates the Session object and fires the Session_OnStart event.To demonstrate how to use the Session object in an ASP page, follow these steps:

  28. Sessions Continue • Paste the following code between the <BODY> </BODY> tags of the ASP page that you created earlier : • <% 'Store information in a Session variable. Session("myInformation") = "somevalue" 'Display the contents of the Session variable. Response.Write Session("myInformation") %> • Click View in Browser from the View menu. • When you are prompted to save the file, click OK. • The browser displays the information in the variable.

  29. Need a Button for Handling • Notice that we created an ArrayList for storing the selected products. • We check whether if we have already stored it in the session or no. If we have already stored then we get hold of the existing session variable else we create a new session variable. • Then we to Drag and drop a button control on the web form and write following code in the it's click event handler, simply navigating to the cart.aspx page which displays the shopping cart

  30. Creation of the Cart Page- Creation of the CShoppingCartItem CLASS • This class is going to represent one item of the shopping cart.

  31. Addition of a DataGrid on the web form. • Drag and drop a DataGrid on the web form. • Create a function called FillCartFromSession() as shown below. • Here, we get hold of the ArrayList that we saved in the session previously and then bind the grid with the arraylist.

  32. Addition of the Recalculate button --Its click event handler • This code calculates the total amount of the items selected based on the quantity entered and displays it in a label.

  33. The code to delete items from the cart • We simply iterate through the ArrayList stored in the session and remove the item based on ProductID

  34. Summary • We saw how to use Session variables to store shopping cart. • This approach has an advantage as compared to cookie driven cart that you need not worry whether client browser is supporting cookies. • On the downside you are putting burden on the server because in default mode Session variables are stored in the memory of the web server. • So, this technique is suitable to small to moderately trafficked web sites. • In the next example we will see how to store shopping cart in SQL Server database. • NOTE : I will give you a complete working example of shopping cart using Session Variables

  35. Introduction to Database Driven Shopping Carts • In the previous example we saw how to create a shopping cart using session variables. • Continuing the concept further the following example will illustrate how to use the database in order to store your shopping cart in a database. • This technique is more robust and scalable that the previous two techniques. • Before you proceed with any coding, you need to create the following table

  36. Creation of the Shopping Cart Table

  37. Developing a simple product listing page • We will first build a simple web form that lists Products from the corresponding table of your database in a DataGrid control. • Add a web form called ProductCatalog.aspx t • Drag and drop a DataGrid control on it. • Write a function called BindGrid() as shown below:

  38. Code for BindGrid()

  39. In the Page_Load event handler private void Page_Load(object sender, System.EventArgs e) { if(!Page.IsPostBack) { BindGrid(); }

  40. Code in the SelectedIndexChanged event of the DataGrid

  41. The CShoppingCartItem class

  42. Addition of a button to redirect to the Cart page • private void Button1_Click (object sender, System.EventArgs e) { Response.Redirect("cart.aspx"); } Here, we are simply navigating to the cart.aspx page which displays the shopping cart.

  43. The CShoppingCart class • This is the most important class in our application because it actually performs the job of storing or retrieving shopping cart items into a database table. • This class uses SqlConnection, SqlCommand and SqlDataReader classes to perform various tasks such as INSERT, UPDATE and SELECT. • It consists of static methods and looks as shown below:

  44. Creating the shopping cart web form • Add another web form to the above project called cart.aspx • Drag and drop a DataGrid on the web form. • Create a function called FillCartFromDb() as shown below: private void FillCartFromDb() { DataSet ds=CShoppingCart.GetAll(Session.SessionID); DataGrid1.DataSource=ds; DataGrid1.DataBind(); Button1_Click(null,null); } Here, we call the GetAll method of the CShoppingCart class whichreturns a DataSet. This DataSet acts as a datasource for the DataGrid control.

  45. The code for the Recalculate Button • This code calculates the total amount of the items selected based on the quantity entered and displays it in a label.

  46. Finally, the code to delete items from the cart. • We simply simply delete a particular product by calling the DeleteItem method of CShoppingCart class.

  47. Summary • In this example we saw how to use SQL Server database to store a shopping cart. • This approach though requires more coding is recommended for big sites. Since you are storing the data in a SQL server database, you are not putting any overhead on the web server (as against Session variables). • Also, this approach is better than cookies because you are not dependent of client browser supporting cookies. In terms of performance this approach will however be slower than the other two techniques. However, overall it is more robust and scalable than cookies or sessions. • Can somebody think of how to create a single wrapper to all he three approaches so that without any code change you can switch between these three techniques??????????

More Related