Click here to Skip to main content
6,631,889 members and growing! (15,833 online)
Email Password   helpLost your password?
Database » Database » General     Advanced

GridView order page shopping cart page ASP.NET SQL

By T.Ashraf

GridView Image order page shopping cart page
C# 2.0, Windows, .NET 2.0SQL 2005, VS2005, DBA, Dev
Posted:16 Feb 2007
Updated:23 Feb 2007
Views:71,610
Bookmarked:35 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
10 votes for this article.
Popularity: 3.56 Rating: 3.56 out of 5
2 votes, 20.0%
1

2
1 vote, 10.0%
3
3 votes, 30.0%
4
4 votes, 40.0%
5

How to Create Order Page and shopping cart Page using GridView ASP.net C# and SQL server

1-Create Three table

Screenshot - P11.gif

2-Create a web Project

3-Create a Images Folder in the project solution

4-Add some image file into the Images folder

Screenshot - P2.gif

5-Rename the Default.aspx web page to OrderPage.aspx

6-Droup and drop GridView object from the toolbox on to the web form.

7-Create the following GridView Columns and set the GridView AutoGenerateColumn to false.

Screenshot - p3.gif

8. Where AddToCart is a button, Picture ID, Title and Date Added are text fields and PictureURL is Image field.

9-GridView will look like this.

Screenshot - p4.gif

10-When the order page is loaded all the item 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 connection string.

11. Enter some data to the SQL table

Screenshot - p5.gif

Screenshot - p6.gif

12. Build and run the application.

13. The following result will be displayed.

Screenshot - p7.gif

14-AddToCart button click event

Screenshot - p8.gif

Set the AddToCart button property to

15. Add this to the GridView property using the source page or the HTML page

OnRowCommand="GridView1_RowCommand"

16. 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());

}

}

17-After you click the AddToCart button need to move the item to shopping cart.To do that need to write code to make a data entry to OrderTable, since ItemId is unique ,base on ItemId we insert a data entry to the OrderTable.

18-Before we make entry to the table we need order Number, we can get the order number from ControlTable since ControlTable hold the last used order number.

19-Method to insert selected item to shoping cart,this method will get the last used order number from ControlTable, and after inserting the values to the OrderTable will updated 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);

}

}

20-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";

}

}

21- Method to updated 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();

}

22-Add new page to the project and name it ShoppingCart.aspx

23-Droup and drop GridView object from the toolbox on to the web form.

24-Create the following GridView Columns and set the GridView AutoGenerateColumn to false.

Screenshot - p9.gif

25- Where Delete is a button, Picture ID, Title, Price and Date Added are text fields and PictureURL is Image field.

26-GridView will look like this.

Screenshot - p10.gif

27-Uder 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();

28-Build and run after placing some order make the shoppingcart startup page and you will see all the order you have placed listed there.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

T.Ashraf


Member
More than 9 years of experience in programming, design, architecture and development of various commercial object oriented application using C#, Visual Basic.net, ASP.net, relational database (SQL ,Oracle, MYSQL) in Window NT/2000/XP and UNIX environment. Experienced in client-server application development, data security management, third party tools and also has very good exposure to the entire Software Development Life Cycle viz. Requirements Collection and other Project Management activities.
WWW.TANVTECH.COM
www.cheaplimotaxi.com
Occupation: Software Developer (Senior)
Company: TANVTECH
Location: United States United States

Other popular Database articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 11 of 11 (Total in Forum: 11) (Refresh)FirstPrevNext
Questionplease put your next code and give me that link if already placed Pinmembersid_kitu21:33 31 Oct '07  
AnswerRe: please put your next code and give me that link if already placed PinmemberT.Ashraf0:41 1 Nov '07  
GeneralSql server Image datatype problem Pinmembersrinivaaaaaas1:38 12 Sep '07  
GeneralRe: Sql server Image datatype problem PinmemberT.Ashraf16:16 27 Sep '07  
GeneralCart Items in Session Pinmemberxpthinker14:54 9 Aug '07  
GeneralRe: Cart Items in Session PinmemberT.Ashraf6:41 10 Aug '07  
Generalhow to create table Pinmemberreddyamar_uk13:01 8 Mar '07  
GeneralRe: how to create table PinmemberT.Ashraf13:08 8 Mar '07  
Generalneeds improving Pinmembercykophysh3912:47 17 Feb '07  
GeneralRe: needs improving PinmemberT.Ashraf0:39 18 Feb '07  
GeneralRe: needs improving Pinmembercykophysh3912:04 19 Feb '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 23 Feb 2007
Editor:
Copyright 2007 by T.Ashraf
Everything else Copyright © CodeProject, 1999-2009
Web22 | Advertise on the Code Project