Click here to Skip to main content
Licence CPOL
First Posted 16 Feb 2007
Views 117,961
Bookmarked 54 times

GridView Order Page Shopping Cart Page ASP.NET SQL

By | 23 Feb 2007 | Article
GridView Image order page shopping cart page

Introduction

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. Create an Images Folder in the project solution
  4. Add some image file into the Images folder
  5. Screenshot - P2.gif

  6. Rename the Default.aspx web page to OrderPage.aspx
  7. Drag and drop GridView object from the toolbox on to the web form.
  8. Create the following GridView Columns and set the GridView AutoGenerateColumn to false.
  9. Screenshot - p3.gif

  10. Where AddToCart is a button, Picture ID, Title and Date Added are text fields and PictureURL is Image field.
  11. GridView will look like this:

    Screenshot - p4.gif

  12. 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.

  13. Enter some data to the SQL table:

    Screenshot - p5.gif

    Screenshot - p6.gif

  14. Build and run the application.
  15. The following result will be displayed:

    Screenshot - p7.gif

  16. AddToCart button click event:

    Screenshot - p8.gif

    Set the AddToCart button property.

  17. Add this to the GridView property using the source page or the HTML page
    OnRowCommand="GridView1_RowCommand"
  18. 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()); 
    } 
    } 
  19. 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.
  20. Before we make an entry to the table we need order Number, we can get the order number from ControlTable since ControlTable holds the last used order number.
  21. The method to insert selected item to shopping cart will get the last used order number from ControlTable, and after inserting the values to the OrderTable will update the order number in the ControlTable.
    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); 
    } 
    } 
  22. 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"; 
    } 
    } 
  23. 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(); 
    } 
  24. Add new page to the project and name it ShoppingCart.aspx.
  25. Drag and drop GridView object from the toolbox on to the web form.
  26. Create the following GridView columns and set the GridView AutoGenerateColumn to false.

    Screenshot - p9.gif

  27. Where Delete is a button, Picture ID, Title, Price and Date Added are text fields and PictureURL is Image field.
  28. GridView will look like this:

    Screenshot - p10.gif

  29. 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(); 
  30. Build and run. After placing some order, make the shopping cart startup page and you will see all the orders you have placed listed there.

License

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

About the Author

T.Ashraf

Technical Lead
TANVTECH
United States United States

Member

More than 10 years of experience in design, architecture and development of various commercial objects oriented application using C# 3.5, ASP.net, relational database like SQL 2008, Oracle, MySQL and SharePoint. Experienced in client-server application development, data security management system, e- commerce, automation processes, claim payment systems, file system and tracking systems, also has very good exposure to the entire software development life cycle.
Tanvtech.com
http://tanvtech.com/Articles/MyFirstSSRSReport.aspx
http://tanvtech.com/Articles/MyFirstSSRSSubReport.aspx
http://tanvtech.com/Articles/MyFirstWcfArticle.aspx
http://tanvtech.com/Articles/MyFirstWcfClientArticle.aspx

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Questionhow to add quantity & edit or delete shopping cart Pinmemberkamran nemati2:47 7 Jan '12  
Questionconversion failed when converting the varchar value '" + ordernumber + "' to data type int Pinmemberkamran nemati18:18 4 Jan '12  
AnswerRe: conversion failed when converting the varchar value '" + ordernumber + "' to data type int PinmemberT.Ashraf14:17 5 Jan '12  
QuestionIncorrect syntax near the keyword 'Order' [modified] Pinmemberkamran nemati4:11 4 Jan '12  
AnswerRe: Incorrect syntax near the keyword 'Order' PinmemberT.Ashraf4:44 4 Jan '12  
GeneralMy vote of 1 Pinmemberalper551:55 18 Apr '10  
GeneralDatabase needed! PinmemberCoder178622:11 14 Mar '10  
GeneralRe: Database needed! PinmemberT.Ashraf0:51 15 Mar '10  
GeneralDatabase of this article PinmemberMember 46466612:13 23 Feb '10  
GeneralDatabase required PinmemberMember 46466612:10 23 Feb '10  
GeneralData not displaying. Pinmemberdee.arthur6:52 9 Jan '10  
GeneralRe: Data not displaying. PinmemberT.Ashraf10:03 11 Jan '10  
GeneralRe: Data not displaying. Pinmemberdee.arthur3:38 21 Jan '10  
GeneralRe: Data not displaying. Pinmemberdee.arthur5:38 21 Jan '10  
Questionplease put your next code and give me that link if already placed Pinmembersid_kitu20:33 31 Oct '07  
AnswerRe: please put your next code and give me that link if already placed PinmemberT.Ashraf23:41 31 Oct '07  
GeneralSql server Image datatype problem Pinmembersrinivaaaaaas0:38 12 Sep '07  
GeneralRe: Sql server Image datatype problem PinmemberT.Ashraf15:16 27 Sep '07  
GeneralCart Items in Session Pinmemberxpthinker13:54 9 Aug '07  
GeneralRe: Cart Items in Session PinmemberT.Ashraf5:41 10 Aug '07  
Questionhow to create table Pinmemberreddyamar_uk12:01 8 Mar '07  
AnswerRe: how to create table PinmemberT.Ashraf12:08 8 Mar '07  
Generalneeds improving Pinmembercykophysh3911:47 17 Feb '07  
GeneralRe: needs improving PinmemberT.Ashraf23:39 17 Feb '07  
Thanks for the comment, This is some part of the project i want to add.Next update i will add more detail.
GeneralRe: needs improving Pinmembercykophysh3911:04 19 Feb '07  

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

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120529.1 | Last Updated 23 Feb 2007
Article Copyright 2007 by T.Ashraf
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid