Download presentation
Presentation is loading. Please wait.
1
Shopping Cart Demo
2
Shopping Cart Search and display product information Add item to cart
Delete item from cart View cart contents Change quantities of a cart item Check out Note: Project: ShopCart
3
Shopping Cart Example Database: OrderID: SessionID
WebCustomer: CustID, CustName, Addr, Phone, WebOrder: OrderID, CustID, OrderDate, CreditCardType, CreditCardNo WebLine: OrderID, PID, Qty WebProduct: Pid, Pname, Price, UnitsInStock OrderID: SessionID Login user using cookie
4
Implementing Shopping Cart as Class
Shopping Cart Properties: public string oid; public string cid; public DateTime ODate; public string CreditCardType; public string CreditCardNo; public List<CartLine> detailLines = new List<CartLine>(); public double CartTotal **** Read only calculated property Methods: AddItem, DeleteItem, ViewCart, CheckOut DetailLine Properties: OID PID Pname Price Qty Amount = Qty * Price **** read only calculated property
5
Adding a Class to a Website
Project/Add Class
6
ShopCart Class Properties
public string oid { get; set; } public string cid { get; set; } public DateTime ODate { get; set; } public string CreditCardType { get; set; } public string CreditCardNo { get; set; } public List<CartLine> detailLines = new List<CartLine>(); public double CartTotal { get { double total=0; foreach (CartLine item in detailLines) { total+=item.amount; } return total; } }
7
AddItem Method public void AddItem(CartLine line) {
Boolean incart=false; if (detailLines.Count>0) foreach(CartLine item in detailLines) if (item.pid==line.pid) item.qty=line.qty; incart=true; break; } if (incart==false) detailLines.Add(line);
8
public void deleteItem(int deleteIndex)
{ detailLines.RemoveAt(deleteIndex); } public void checkOut() string strConn = "Data Source=(localdb)\\projects;Initial Catalog=SalesDB;Integrated Security=True"; SqlConnection objConn = new SqlConnection(strConn); string strSQL = "insert into weborder values ('" + oid + "','" + cid + "','" + ODate.ToString() + "','" + CreditCardType + "','" + CreditCardNo + "')"; SqlCommand objComm = new SqlCommand(strSQL,objConn); objConn.Open(); objComm.ExecuteNonQuery(); foreach(CartLine objLine in detailLines) strSQL = "insert into webline values ('" + objLine.oid + "','" + objLine.pid + "'," + objLine.qty.ToString() + ")"; objComm.CommandText = strSQL; objConn.Close(); }
9
CartLine Class { public string oid { get; set; }
public class CartLine { public string oid { get; set; } public string pid { get; set; } public string pname { get; set; } public double price { get; set; } public double qty { get; set; } public double amount { get {return qty*price;}} }
10
Home Page Reading cookie: ViewProduct button event:
protected void Page_Load(object sender, EventArgs e) { String CID; CID = Request.Cookies["CID"].Value; Session["CID"] = CID; } ViewProduct button event: ShopCart MyCart = new ShopCart(); MyCart.oid = Session.SessionID; MyCart.cid = (string)Session["CID"]; Session["MyCart"] = MyCart; Response.Redirect("ViewProduct.aspx");
11
ViewProduct PageLoad event
DataSet objDataSet = new DataSet(); ShopCart MyCart = new ShopCart(); protected void Page_Load(object sender, EventArgs e) { string strConn = "Data Source=(localdb)\\projects;Initial Catalog=SalesDB;Integrated Security=True"; SqlConnection objConn = new SqlConnection(strConn); string strSQL = "select * from WebProducts;"; SqlDataAdapter objAdapter = new SqlDataAdapter(strSQL,strConn); if (!Page.IsPostBack) objAdapter.Fill(objDataSet, "WebProducts"); Session["MyDs"]= objDataSet; } else MyCart = (ShopCart)Session["MyCart"]; objDataSet = (DataSet)Session["MyDs"];
12
Price Radiobutton Event
DataView objDataView = new DataView(); objDataView = objDataSet.Tables["WebProducts"].DefaultView; objDataView.RowFilter = "price " + RadioButtonList1.SelectedValue.ToString(); GridView1.DataSource = objDataView; GridView1.DataBind();
13
AddToCart Event CartLine objLine = new CartLine();
MyCart = (ShopCart)Session["MyCart"]; objLine.oid = MyCart.oid; objLine.pid = GridView1.SelectedRow.Cells[0].Text; objLine.pname = GridView1.SelectedRow.Cells[1].Text; objLine.price = double.Parse(GridView1.SelectedRow.Cells[2].Text); TextBox t = (TextBox)(GridView1.SelectedRow.Cells[4].Controls[1]); objLine.qty = double.Parse(t.Text); MyCart.AddItem(objLine); Response.Write("number of items in cart:" MyCart.detailLines.Count.ToString()); Session["MyCart"] = MyCart;
14
ViewCart event DeleteLine event: ShopCart MyCart = new ShopCart();
protected void Page_Load(object sender, EventArgs e) { MyCart =(ShopCart) Session["MyCart"]; GridView1.DataSource = MyCart.detailLines; GridView1.DataBind(); TextBox1.Text = MyCart.CartTotal.ToString("C"); } DeleteLine event: MyCart.deleteItem(e.RowIndex); Session["MyCart"] = MyCart; GridView1.DataSource = MyCart.detailLines; GridView1.DataBind(); TextBox1.Text = MyCart.CartTotal.ToString("C");
15
CheckOut Procedure ShopCart MyCart = new ShopCart();
protected void Page_Load(object sender, EventArgs e) { MyCart = (ShopCart) Session["MyCart"]; } protected void Button1_Click(object sender, EventArgs e) MyCart.ODate = DateTime.Now; MyCart.CreditCardType = RadioButtonList1.SelectedValue.ToString(); MyCart.CreditCardNo = TextBox1.Text; MyCart.checkOut(); Response.Write("<p align='center'><font size='5'><b>Thank you for shopping at My.Com</b></font></p>"); Session.Abandon();
16
Cookies
17
Data in Cookies System.Web
Which web site set the cookie Expiration date DateTime data type TimeSpan data type One or more pieces of data Keys: A collection of cookie’s names Define a new cookie: HttpCookie cookieCID = new HttpCookie("CID"); Write cookie to user: Response.Cookies.Add(cookieCID);
18
Cookie’s Properties Properties To write a cookie: Name Value Expires
Response.Cookies.Add(cookieObj)
19
Creating Cookies DateTime dt; dt=DateTime.Now;
TimeSpan ts = new TimeSpan(30,0,0,0); HttpCookie cookieCID = new HttpCookie("CID"); HttpCookie cookieCname = new HttpCookie("Cname"); cookieCID.Value = Login1.UserName; cookieCID.Expires = dt.Add(ts); Response.Cookies.Add(cookieCID); Note: The name(or key)of cookieCID is "CID";
20
Reading Cookies Response.Write(Request.Cookies["CID"].Name);
Response.Write(Request.Cookies["CID"].Value);
21
Using Cookie with DataReader
string strConn = "Data Source=(localdb)\\projects;Initial Catalog=SalesDB;Integrated Security=True"; SqlConnection objConn = new SqlConnection(strConn); String CID; CID = Request.Cookies["CID"].Value; string strSQL = "select * from webcustomer where CustID= '" + CID + "'"; SqlCommand objComm = new SqlCommand(strSQL, objConn); SqlDataReader objDataReader; objConn.Open(); objDataReader = objComm.ExecuteReader(); if (objDataReader.Read()) { Session["Cname"] = objDataReader["CustName"]; Response.Write("<hr>Welcome:" + objDataReader["CustName"] + "<hr>"); } else Response.Write("<hr>We don't have your record <hr>"); objConn.Close();
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.