Advertisement
Guest User

cart.aspx.cs

a guest
Jun 28th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.95 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Data;
  8. using System.Data.SqlClient;
  9.  
  10. public partial class Account_Cart : System.Web.UI.Page
  11. {
  12.     SqlConnection con = new SqlConnection(Helper.GetCon());
  13.     protected void Page_Load(object sender, EventArgs e)
  14.     {
  15.         if (!IsPostBack)
  16.         {
  17.             GetCart();
  18.             GetOrderSummary();
  19.         }
  20.     }
  21.  
  22.     void GetCart()
  23.     {
  24.         con.Open();
  25.         SqlCommand cmd = new SqlCommand();
  26.         cmd.Connection = con;
  27.         cmd.CommandText = @"SELECT od.DetailID, od.OrderNo, p.ProductID, p.Name, pi.Image, p.Code,
  28.            b.BrandName, p.UnitPrice, od.Quantity, od.Amount
  29.            FROM OrderDetails od INNER JOIN Product p
  30.            ON od.ProductID = p.ProductID
  31.            INNER JOIN ProductImages pi ON p.ProductID=pi.ProductID
  32.            INNER JOIN Brands b ON p.BrandID = b.BrandID
  33.            WHERE od.OrderNo=@OrderNo"; //AND od.UserID=@UserID
  34.         cmd.Parameters.AddWithValue("@OrderNo", 0);
  35.         //cmd.Parameters.AddWithValue("@UserID", 1); //Session["userid"].ToString();
  36.         SqlDataReader dr = cmd.ExecuteReader();
  37.         lvCart.DataSource = dr;
  38.         lvCart.DataBind();
  39.         con.Close();
  40.     }
  41.     double GetPrice(string ProductID)
  42.     {
  43.         using (SqlConnection con = new SqlConnection(Helper.GetCon()))
  44.         {
  45.             string query = @"SELECT UnitPrice FROM Product WHERE ProductID = @ProductID";
  46.             con.Open();
  47.             using (SqlCommand cmd = new SqlCommand(query, con))
  48.             {
  49.                 cmd.Parameters.AddWithValue("@ProductID", ProductID);
  50.                 return Convert.ToDouble((decimal)cmd.ExecuteScalar());
  51.  
  52.                 //decimal price = (decimal)cmd.ExecuteScalar();
  53.                 //return Convert.ToDouble(price);
  54.  
  55.             }
  56.         }
  57.     }
  58.     protected void lvCart_ItemCommand(object sender, ListViewCommandEventArgs e)
  59.     {
  60.         if (e.CommandName == "cartupdate")
  61.         {
  62.             Literal ltProductID = (Literal)e.Item.FindControl("ltProductID");
  63.             TextBox txtQty = (TextBox)e.Item.FindControl("txtQty");
  64.             int qty = int.Parse(txtQty.Text);
  65.             double price = Helper.GetPrice(ltProductID.Text);
  66.             double discount = Helper.GetDiscount(ltProductID.Text) / 100;
  67.             double discountedPrice = price * (1 - discount);
  68.  
  69.             using (SqlConnection con = new SqlConnection(Helper.GetCon()))
  70.  
  71.             {
  72.                 con.Open();
  73.                 string query = @"UPDATE OrderDetails SET Price=@Price, Quantity=@Quantity,
  74.                    Amount=@Amount WHERE OrderNo=0 AND ProductID=@ProductID";
  75.                 using (SqlCommand cmd = new SqlCommand(query, con))
  76.                 {
  77.                     cmd.Parameters.AddWithValue("@ProductID", ltProductID.Text);
  78.                     cmd.Parameters.AddWithValue("@Quantity", qty);
  79.                     if (Helper.AvailableDiscount(ltProductID.Text))
  80.                         cmd.Parameters.AddWithValue("@Price", discountedPrice);
  81.                     else
  82.                         cmd.Parameters.AddWithValue("@Price", price);
  83.                     if (Helper.AvailableDiscount(ltProductID.Text))
  84.                         cmd.Parameters.AddWithValue("@Amount", discountedPrice * qty);
  85.                     else
  86.                         cmd.Parameters.AddWithValue("@Amount", price * qty);
  87.                     cmd.ExecuteNonQuery();
  88.                 }
  89.             }
  90.         }
  91.         else if (e.CommandName == "itemdelete")
  92.         {
  93.             Literal ltDetailID = (Literal)e.Item.FindControl("ltDetailID");
  94.             using (SqlConnection con = new SqlConnection(Helper.GetCon()))
  95.             {
  96.                 con.Open();
  97.                 string query = "DELETE FROM OrderDetails WHERE DetailID=@DetailID";
  98.                 using (SqlCommand cmd = new SqlCommand(query, con))
  99.                 {
  100.                     cmd.Parameters.AddWithValue("@DetailID", ltDetailID.Text);
  101.                     cmd.ExecuteNonQuery();
  102.                 }
  103.             }
  104.         }
  105.         GetCart();
  106.         GetOrderSummary();
  107.     }
  108.  
  109.  
  110.     void GetOrderSummary()
  111.     { //AND UserID=@UserID
  112.         double total = 0;
  113.         con.Open();
  114.         SqlCommand cmd = new SqlCommand();
  115.         cmd.Connection = con;
  116.         cmd.CommandText = @"SELECT SUM(Amount) FROM OrderDetails
  117.            WHERE OrderNo=@OrderNo
  118.            HAVING COUNT(OrderNo) > 0";
  119.         cmd.Parameters.AddWithValue("@OrderNo", 0);
  120.         cmd.Parameters.AddWithValue("@UserID", 1); // Session["userid"].ToString();
  121.         SqlDataReader dr = cmd.ExecuteReader();
  122.  
  123.         if (dr.HasRows)
  124.         {
  125.             while (dr.Read())
  126.             {
  127.                 total = double.Parse(dr[0].ToString());
  128.             }
  129.             con.Close();
  130.         }
  131.         else
  132.         {
  133.             total = 0;
  134.             con.Close();
  135.         }
  136.  
  137.         ltGross.Text = (total * .88).ToString("#,##0.00");
  138.         ltVAT.Text = (total * .12).ToString("#,##0.00");
  139.         //ltDelivery.Text = (total * .05).ToString("#,##0.00");
  140.         ltTotal.Text = (total * 1.00).ToString("#,##0.00");
  141.     }
  142.  
  143.     protected void btnDelete_Click(object sender, EventArgs e)
  144.     {
  145.         using (SqlConnection con = new SqlConnection(Helper.GetCon()))
  146.  
  147.         {
  148.             con.Open();
  149.             string query = @"DELETE FROM OrderDetails WHERE OrderNo=@OrderNo AND UserID=@UserID";
  150.                  
  151.             using (SqlCommand cmd = new SqlCommand(query, con))
  152.             {
  153.                 cmd.Parameters.AddWithValue("@OrderNo", "0");
  154.                 cmd.Parameters.AddWithValue("@UserID", "1");
  155.                 cmd.ExecuteNonQuery();
  156.             }
  157.             GetCart();
  158.             GetOrderSummary();
  159.         }
  160.  
  161.     }
  162.  
  163.     protected void lvCart_ItemDataBound(object sender, ListViewItemEventArgs e)
  164.     {
  165.         if (e.Item.ItemType == ListViewItemType.DataItem)
  166.         {
  167.             Literal ltProductID = (Literal)e.Item.FindControl("ltProductID");
  168.             Literal ltPrice = (Literal)e.Item.FindControl("ltPrice");
  169.             Literal ltDiscount = (Literal)e.Item.FindControl("ltDiscount");
  170.             Literal ltAmount = (Literal)e.Item.FindControl("ltAmount");
  171.             TextBox txtQty = (TextBox)e.Item.FindControl("txtQty");
  172.             double price = Helper.GetPrice(ltProductID.Text);
  173.             double discount = Helper.GetDiscount(ltProductID.Text) / 100;
  174.             double discountedPrice = price * (1 - discount);
  175.             int quantity = int.Parse(txtQty.Text);
  176.  
  177.            
  178.             ltPrice.Text = Helper.AvailableDiscount(ltProductID.Text) ?
  179.                 "<s>" + price.ToString("#,###,##0.00") + "</s>" : price.ToString("#,###,##0.00");
  180.             ltDiscount.Text = discountedPrice.ToString("#,###,##0.00");
  181.             ltDiscount.Visible = Helper.AvailableDiscount(ltProductID.Text);
  182.         }
  183.     }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement