void gvBind() //to bind the discount coupon into the gridview { List <DiscountCoupon> discountCoupons = DiscountCouponDB.getAllDiscountCoupon(); gvDiscount.DataSource = discountCoupons; gvDiscount.DataBind(); }
protected void btnAdd_Click(object sender, EventArgs e) //add the new discount coupon { Admin a = (Admin)Session["admin"]; double disAMT; if (tbxAMT.Text == "") { disAMT = 0; } else { disAMT = Convert.ToDouble(tbxAMT.Text); } DiscountCoupon dc = new DiscountCoupon() { CouponCode = tbxCc.Text, Name = tbxName.Text, Description = tbxDesc.Text, Percentage = tbxPercentage.Text, Amount = disAMT, StartDate = Convert.ToDateTime(tbxSd.Text), EndDate = Convert.ToDateTime(tbxEd.Text), AdminID = a }; if (Convert.ToDateTime(tbxSd.Text) < DateTime.Now) //to check the start date must be future and cannot be past { lblOutput.Text = "Start date must be in the future"; } else { if (Convert.ToDateTime(tbxEd.Text) < Convert.ToDateTime(tbxSd.Text)) //to check end date cannot be before the start date { lblOutput.Text = "End date cannot be before the start date"; } else { try { int id = DiscountCouponDB.addDiscountCoupon(dc); //add row into the database lblOutput.Text = id + "Added Successfully!"; gvBind(); tbxAMT.Text = ""; tbxCc.Text = ""; tbxDesc.Text = ""; tbxEd.Text = ""; tbxName.Text = ""; tbxPercentage.Text = ""; tbxSd.Text = ""; } catch (Exception ex) { lblOutput.Text = "Cannot Add!" + ex.Message; //to show the error message cannot add the discount coupon } } } }
//if user apply with the discount coupon protected void btnApply_Click(object sender, EventArgs e) { //retrieve the shopping cart session ShoppingCart sc = (ShoppingCart)Session["cart"]; //initialize the total amount with double double tAmt = Convert.ToDouble(sc.TotalPrice); //get discount coupon from database DiscountCoupon dc = DiscountCouponDB.getDiscountCouponByCode(tbxDC.Text); //check if discount coupon is not null if (dc != null) { if (dc.EndDate > DateTime.Now) //check the enddate from datetime.now functions { Session["DC"] = dc; //create a session for discount coupon if (dc.Percentage == null) // check if the percentage is null { double netAMT = sc.TotalPrice - dc.Amount; //total price will be minus from the amount sc.DiscountedAmt = dc.Amount; //show the amount price of discount coupon lblDiscountAmt.Text = "$" + dc.Amount; //show with the dollar sign sc.NetAMount = (tAmt - dc.Amount); lblNetAmount.Text = "$" + sc.NetAMount; lblTax.Text = sc.Tax.ToString("C"); lblAmtPayable.Text = sc.AmtPayable.ToString("C"); Session["AmtPayable"] = sc.AmtPayable; //create a session for amt payable } else { sc.DiscountedAmt = tAmt * Convert.ToDouble(dc.Percentage); //if the amount is null, then the price will be minus from the percentage sc.NetAMount = tAmt - (tAmt * Convert.ToDouble(dc.Percentage)); //netamount times with discount percentage lblDiscountAmt.Text = "$" + sc.DiscountedAmt; //show with the dollar sign lblNetAmount.Text = "$" + sc.NetAMount; lblTax.Text = sc.Tax.ToString("C"); lblAmtPayable.Text = sc.AmtPayable.ToString("C"); Session["AmtPayable"] = sc.AmtPayable; //create a session for amt payable } } else { lblError.Text = "Coupon has expired"; //if the coupon code has expired, show the error message } } else { lblError.Text = "Coupon Code is not valid"; //when the coupon code is not valid } }