Ejemplo n.º 1
0
        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)
        {
            //if the role is not admin, redirect to default
            if (!(Request.Cookies["roles"].Value == "2"))
            {
                ClientScript.RegisterClientScriptBlock(this.GetType(),
                                                       "{noAccess}", "alert('{You have no Access to this!}');window.location.href='Default.aspx' ", true);
            }

            ProductAccess pa = new ProductAccess();

            txtProductID.Text = "" + (pa.getMaxProductID() + 1);
            pa.CloseConnection();   //should I close it?
        }
Ejemplo n.º 3
0
        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 btnAddProduct_Click(object sender, EventArgs e)
        {
            ProductAccess pa = new ProductAccess();

            //all the elements to save to database
            int    product_id   = Convert.ToInt32(txtProductID.Text);
            string productName  = txtProductName.Text;
            float  productPrice = float.Parse(txtProductPrice.Text);
            string description  = txtDescription.Text;
            string type         = dropdownType.Text;
            string brand        = txtBrand.Text;
            string image        = "NA";

            string fileName = Path.GetFileName(fileUploadImg.FileName); //get file name


            string filePath = "~/images/";

            filePath += fileName;

            image = filePath; //change imgae name to the file name

            fileUploadImg.PostedFile.SaveAs(Server.MapPath(filePath));


            bool saveProduct = pa.SaveProduct(product_id, productName, productPrice, description,
                                              type, brand, image);

            if (saveProduct)
            {
                //notification for succeed and refresh the page
                ClientScript.RegisterClientScriptBlock(this.GetType(),
                                                       "{saveProduct}", "alert('{Product saved!}');window.location.href='AddProduct.aspx' ", true);
            }
            else
            {
                //notification failure
                ClientScript.RegisterClientScriptBlock(this.GetType(),
                                                       "{saveError}", "alert('{Save Failed! Contact the developer!}');window.location.href='AddProduct.aspx' ", true);
            }
        }