Sunday, July 1, 2012

How to Create Shopping Cart in asp.net(C#)


This article discusses how to create an order page and shopping cart page using GridView ASP.NET C# and SQL Server
  1. Create three tables:
    Screenshot - P11.gif
  2. Create a web Project
  3. Name of the Server can be anything.
  4. You can name anything to your Database.
  5. Create an Images Folder in the project solution
  6. Add some image file into the Images folder
  7. Screenshot - P2.gif
  8. Rename the Default.aspx web page to OrderPage.aspx
  9. Drag and drop GridView object from the toolbox on to the web form.
  10. Create the following GridView Columns and set the GridView AutoGenerateColumn to false.
  11. Change the Header Text to change the name of the columns in GridView
  12. Screenshot - p3.gif
  13. Where AddToCart is a button, Picture ID, Title and Date Added are text fields and PictureURL is Image field.
  14. GridView will look like this:
    Screenshot - p4.gif
  15. When the order page is loaded, all the items have to be loaded to the GridView for the user to select, copy the following code to the load page event:
    string Sel = "Select * from ItemTable"; 
    SqlConnection Con = new SqlConnection(Cn); 
    SqlCommand cmd = new SqlCommand(Sel, Con); 
    Con.Open(); 
    DataTable dt = new DataTable(); 
    dt.Columns.Add(new DataColumn("PictureID", typeof(int))); 
    dt.Columns.Add(new DataColumn("PictureURL", typeof(string))); 
    dt.Columns.Add(new DataColumn("Title", typeof(string))); 
    dt.Columns.Add(new DataColumn("DateAdded", typeof(DateTime))); 
    SqlDataReader reader = cmd.ExecuteReader(); 
    while (reader.Read()) 
    { 
    DataRow dr = dt.NewRow(); 
    dr["PictureID"] = Convert.ToInt32(reader["PictureId"]); 
    dr["PictureURL"] = ResolveUrl("~/Images/" +reader["PictureURL"]); 
    dr["Title"] = reader["Title"]; 
    dr["DateAdded"] = reader["DateAdded"]; 
    dt.Rows.Add(dr); 
    } 
    Con.Close(); 
    GridView1.DataSource = dt; 
    GridView1.DataBind(); 
    Where Cn Is the connection string.
  16. Enter some data to the SQL table:
    Screenshot - p5.gif
    Screenshot - p6.gif
  17. Build and run the application.
  18. The following result will be displayed:
    Screenshot - p7.gif
  19. AddToCart button click event:
    Screenshot - p8.gif
    Set the AddToCart button property.
  20. Add this to the GridView property using the source page or the HTML page
    OnRowCommand="GridView1_RowCommand"
  21. Add these lines of code to the page code behind:
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) 
    { 
    if (e.CommandName == "AddToCart") 
    { 
    int index = Convert.ToInt32(e.CommandArgument); 
    GridViewRow row = GridView1.Rows[index]; 
    AddShopCart(row.Cells[1].Text.ToString()); 
    } 
    } 
  22. After you click the AddToCart button, you need to move the item to the shopping cart. To do that, you need to write code to make a data entry to OrderTable, since ItemId is unique. Based on ItemId we insert a data entry to the OrderTable.
  23. Before we make an entry to the table we need order Number, we can get the order number fromControlTable since ControlTable holds the last used order number.
  24. The method to insert selected item to shopping cart will get the last used order number fromControlTable, and after inserting the values to the OrderTable will update the order number in theControlTable.
    private void AddShopCart(string ItemId) 
    { 
    string ord = OrderNumber(); 
    if (ord != "Bad order") 
    { 
    int ordernumber = Convert.ToInt32(ord); 
    ordernumber += 1; 
    SqlConnection C_n = new SqlConnection(Cn); 
    SqlCommand cm = new SqlCommand("Insert INTO OrderTable VALUES
     ('" + ordernumber + "', '" + ItemId + "', '" + "101" + 
     "', '" + Convert.ToDateTime("2/19/2007") + "','" + "1" + "')", C_n); 
    C_n.Open(); 
    SqlDataReader dr = cm.ExecuteReader(); 
    C_n.Close(); 
    UpdateOrderNumber(ordernumber); 
    } 
    } 
  25. The method to get the last used order number.
    private string OrderNumber() 
    { 
    SqlConnection Or_Cn = new SqlConnection(Cn); 
    SqlCommand Or_Cm = new SqlCommand("Select OrderNumber from ControlTable", Or_Cn); 
    Or_Cn.Open(); 
    SqlDataReader Or_rd = Or_Cm.ExecuteReader(); 
    if (Or_rd.Read()) 
    { 
    return Or_rd["OrderNumber"].ToString(); 
    } 
    else 
    { 
    return "Bad order"; 
    } 
    } 
  26. The method to update the order number in the ControlTable:
    private void UpdateOrderNumber(int UpdatedNumber) 
    { 
    SqlConnection Op_Cn = new SqlConnection(Cn); 
    SqlCommand Op_Cm = new SqlCommand
     ("Update ControlTable Set OrderNumber=" + UpdatedNumber, Op_Cn); 
    Op_Cn.Open(); 
    SqlDataReader Op_rd = Op_Cm.ExecuteReader(); 
    Op_Cn.Close(); 
    } 
  27. Add new page to the project and name it ShoppingCart.aspx.
  28. Drag and drop GridView object from the toolbox on to the web form.
  29. Create the following GridView columns and set the GridView AutoGenerateColumn to false.
    Screenshot - p9.gif
  30. Where Delete is a button, Picture ID, Title, Price and Date Added are text fields and PictureURL is Image field.
  31. GridView will look like this:
    Screenshot - p10.gif
  32. Under page load event, copy the following code:
    string Sel = "Select a.* from ItemTable as a INNER JOIN 
      OrderTable as b ON a.PictureId=b.ItemId"; 
    SqlConnection Con = new SqlConnection(Cn); 
    SqlCommand cmd = new SqlCommand(Sel, Con); 
    Con.Open(); 
    DataTable dt = new DataTable(); 
    dt.Columns.Add(new DataColumn("PictureID", typeof(int))); 
    dt.Columns.Add(new DataColumn("Title", typeof(string))); 
    dt.Columns.Add(new DataColumn("Price", typeof(string))); 
    dt.Columns.Add(new DataColumn("DateAdded", typeof(DateTime))); 
    dt.Columns.Add(new DataColumn("PictureURL", typeof(string))); 
    SqlDataReader reader = cmd.ExecuteReader(); 
    while (reader.Read()) 
    { 
    DataRow dr = dt.NewRow(); 
    dr["PictureID"] = Convert.ToInt32(reader["PictureId"]); 
    dr["Title"] = reader["Title"]; 
    dr["Price"] = reader["Price"]; 
    dr["DateAdded"] = reader["DateAdded"]; 
    dr["PictureURL"] = ResolveUrl("~/Images/" + reader["PictureURL"]); 
    dt.Rows.Add(dr); 
    } 
    Con.Close(); 
    GridView1.DataSource = dt; 
    GridView1.DataBind(); 
  33. F5

8 comments: