Creating a Simple Shopping Cart Application

In this section, you'll modify your DataGridWebApplication you created earlier to turn it into a simple shopping cart. You'll store the shopping cart in a Session object.

Note 

As mentioned in the previous section, in a real application you'll probably want to store the shopping cart in the database rather than in a Session object.

Figure 15.24 shows the final running form that you'll build. You use the Buy button to add a product to the shopping cart shown on the right of the form. As you can see from Figure 15.24, I've added three products to the shopping cart by pressing the Buy button for each product in the grid on the left.

click to expand
Figure 15.24: The running form

You can either follow the steps shown in the following sections to add the Buy button and the shopping cart to your form, or you can replace the ASP.NET code in your form with the code in the WebForm1.aspx file contained in the VS .NET Projects\DataGridWebApplication directory. You replace the code in your form by selecting and deleting the existing code in your form and pasting in the code from the WebForm1.aspx file. You'll also need to replace the code behind your form with the code in the WebForm1.aspx.cs file contained in the VS .NET Projects\DataGridWebApplication directory.

Adding the Buy Button

In this section, you'll add the Buy button to the DataGrid1 object of the DataGridWebApplication. Perform the following steps:

  1. Open DataGridWebApplication by selecting File Open Project, double-click the Data-GridWebApplication folder, and double-click the DataGridWebApplication.sln file.

  2. Open the WebForm1.aspx file in Design mode by double-clicking this file in the Solution Explorer window. Click on the DataGrid1 control in the form. Figure 15.25 shows the properties for DataGrid1.

    click to expand
    Figure 15.25: DataGrid1 properties

  3. Next, click the Property Builder link near the bottom of the Properties window. Do the following to add the Buy button:

    1. Click Columns on the left of the DataGrid1 Properties dialog box.

    2. Expand the Button Column node of the Available Columns section.

    3. Add a Select button to the Selected Columns area.

    4. Set Text to Buy. This is the text that is shown on the button.

    5. Set Command Name to AddToCart. This is the method that is called when the button is pressed. (You'll create this method later.)

    6. Set Button Type to PushButton.

      Figure 15.26 shows the final properties of the Buy button.

      click to expand
      Figure 15.26: Buy button properties

  4. Click OK to add the button to DataGrid1. Figure 15.27 shows DataGrid1 with the newly added Buy button.

    click to expand
    Figure 15.27: DataGrid1 with Buy button

Adding the Shopping Cart

In this section, you'll add a DataGrid to store the shopping cart. Drag a DataGrid control from the Toolbox to the right of DataGrid1 on your form. Set the ID of this new DataGrid to ShoppingCart, as shown in Figure 15.28.

click to expand
Figure 15.28: ShoppingCart DataGrid

Note 

You might have to close all windows except the form designer so that you have enough screen space to add the new DataGrid to your form.

Adding Code to the WebForm1.aspx.cs File

Your next task is to add some additional code to the WebForm1.aspx.cs file to support the shopping cart. As mentioned earlier, either you can follow the steps shown in this section or you can replace the code behind your form with the code in the WebForm1.aspx.cs file contained in the VS .NET Projects\DataGridWebApplication directory. You replace the code in your form by selecting and deleting the existing code in your form and pasting in the code from the WebForm1.aspx.cs file.

Perform the following steps if you want to modify the code yourself:

  1. Select View Code, or press F7 on your keyboard to view the code. Add a DataTable object named Cart and a DataView object named CartView to the WebForm1 class, as shown in the following code:

     public class WebForm1 : System.Web.UI.Page {   protected DataTable Cart;   protected DataView CartView; 

    Your Cart object is used to store the shopping cart and will be populated with the products selected using the Buy button. Your CartView object is used to view the shopping cart.

  2. Next, set your Page_Load() method to the following code; notice that this method creates a DataTable to store the shopping cart, and that this DataTable is stored in the Session object:

     private void Page_Load(object sender, System.EventArgs e) {   // Put user code to initialize the page here   // populate the Session object with the shopping cart   if (Session["ShoppingCart"] == null)   {     Cart = new DataTable();     Cart.Columns.Add(new DataColumn("Product Name", typeof(string)));     Cart.Columns.Add(new DataColumn("Unit Price", typeof(string)));     Session["ShoppingCart"] = Cart;   }   else   {     Cart = (DataTable) Session["ShoppingCart"];   }   CartView = new DataView(Cart);   ShoppingCart.DataSource = CartView;   ShoppingCart.DataBind();   if (!this.IsPostBack)   {     // populate dataSet11 with the rows from the Products DataTable    sqlDataAdapter1.Fill(dataSet11, "Products");    this.DataBind();   } } 

  3. Next, you need to add the following AddToCart() method to your WebForm1 class. This method is called when the user presses the Buy button. Notice this method creates a DataRow object and populates it with TableCell objects to store the product name and unit price in the Shopping-Cart DataTable that you previously added to your form.

     protected void AddToCart(Object sender, DataGridCommandEventArgs e) {   DataRow product = Cart.NewRow();   // e.Item is the row of the table where the command is raised.   // For bound columns the value is stored in the Text property of TableCell   TableCell productNameCell = e.Item.Cells[1];   TableCell unitPriceCell = e.Item.Cells[3];   string productName = productNameCell.Text;   string unitPrice = unitPriceCell.Text;   if (((Button)e.CommandSource).CommandName == "AddToCart")   {     product[0] = productName;     product[1] = unitPrice;     Cart.Rows.Add(product);   }   ShoppingCart.DataBind(); } 

  4. The only thing left to do is to run your form. To do this, select Debug Start Without Debugging, or press Ctrl+F5 on your keyboard.

Click the Buy button for different products in the grid to add them to your shopping cart.




Mastering C# Database Programming
Mastering the SAP Business Information Warehouse: Leveraging the Business Intelligence Capabilities of SAP NetWeaver
ISBN: 0764596373
EAN: 2147483647
Year: 2003
Pages: 181

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net