protected void btnAddToCart_Click(object sender, EventArgs e) { if (!loginStatus()) { ClientScript.RegisterClientScriptBlock(this.GetType(), "{Login first}", "alert('Please Login First!');", true); } else { OrderAccess oa = new OrderAccess(); ProductAccess pa = new ProductAccess(); SqlDataReader reader = pa.SearchProductByID(product_id); while (reader.Read()) //while search product succeed { //parameters to send in int order_id = oa.getMaxOrderID() + 1; int customer_id = Convert.ToInt16(Request.Cookies["user_id"].Value); DateTime dateTime = DateTime.Now; float orderTotalPrice = float.Parse(reader.GetDouble(2).ToString()) * Convert.ToInt16(dropdownQuantity.Text); int orderQuantity = Convert.ToInt16(dropdownQuantity.Text); int cart_id = Convert.ToInt32(customer_id.ToString() + Request.Cookies["date"].Value); //generates a unique cart_id try { //save the cart_id to cookie for checkout screen Response.Cookies["cart_id"].Value = cart_id.ToString(); Response.Cookies["cart_id"].Expires = DateTime.Now.AddDays(10); //save order thru database layer oa.SaveOrder(order_id, customer_id, dateTime, product_id, orderTotalPrice, orderQuantity, cart_id); int a = 1; //identify if it's the first order after login if (!cartStatus()) //if it's the first order, cart quantity will be 1 { Response.Cookies["itemQuantity"].Value = a.ToString(); //if so, set 1 to cart quantity Response.Cookies["itemQuantity"].Expires = DateTime.Now.AddDays(10); } else { Response.Cookies["itemQuantity"].Value = (Convert.ToInt16(Request.Cookies["itemQuantity"].Value) + 1).ToString(); //if not, plus 1 to the cart quantity } ClientScript.RegisterClientScriptBlock(this.GetType(), "{Order Success}", "alert('Order Succesfull Added');window.location.href='Default.aspx' ", true); } catch { //shoe error to tell item save failed ClientScript.RegisterClientScriptBlock(this.GetType(), "{error}", "alert('{productDetail error!}'); window.location.href='Default.aspx'", true); } } } }
protected void Page_Load(object sender, EventArgs e) { product_id = Convert.ToInt16(Request.QueryString["product_id"]); ProductAccess pa = new ProductAccess(); SqlDataReader reader = pa.SearchProductByID(product_id); OrderAccess oa = new OrderAccess(); while (reader.Read()) { //set the datas to the UI ImageProduct.ImageUrl = reader.GetString(6); lblProductName.Text = reader.GetString(5) + " " + reader.GetString(1); lblPrice.Text = "$" + reader.GetDouble(2); lblDescription.Text = reader.GetString(3); } pa.CloseConnection(); }
protected void linkLogin_Click(object sender, EventArgs e) { try { if (loginStatus()) //logout; clear all cookies { Response.Cookies["name"].Expires = DateTime.MinValue; //delete user cookies Response.Cookies["user_id"].Expires = DateTime.MinValue; Response.Cookies["date"].Expires = DateTime.MinValue; Response.Cookies["roles"].Expires = DateTime.MinValue; linkLogin.Text = "Login"; //set link to "Login" again try { if (cartStatus()) //if cart item is still there { OrderAccess oa = new OrderAccess(); oa.DeleteCart(Request.Cookies["cart_id"].Value); //delete all cart orders from the database Response.Cookies["cart_id"].Expires = DateTime.MinValue; Response.Cookies["itemQuantity"].Expires = DateTime.MinValue; //and delete cart cookie } } catch { Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "{error}", "alert('{Master page logout error}');window.location.href='Login.aspx' ", true); } Response.Redirect("Login.aspx"); } else { Response.Redirect("Login.aspx"); } } catch { Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "{error}", "alert('{Master logout error}');window.location.href='Login.aspx' ", true); } }
protected void btnDeleteAll_Click(object sender, EventArgs e) { string cart_id = Request.Cookies["cart_id"].Value; OrderAccess oa = new OrderAccess(); try { oa.DeleteCart(cart_id); Response.Cookies["cart_id"].Expires = DateTime.MinValue; Response.Cookies["itemQuantity"].Expires = DateTime.MinValue; ClientScript.RegisterClientScriptBlock(this.GetType(), "{delete Success}", "alert('Everything in Cart was deleted! heading back to home');window.location.href='Default.aspx' ", true); } catch { oa.DeleteCart(cart_id); ClientScript.RegisterClientScriptBlock(this.GetType(), "{error}", "alert('database connect error');window.location.href='Default.aspx' ", true); } }