protected void btnUpdateCart_Click(object sender, System.EventArgs e) { // We declare the variable CartCookie to check if the HttpCookie "CartCookie" exits and to // be able to remove a key from it. HttpCookie CartCookie = Request.Cookies.Get("CartCookie"); // We check to see if the HttpCookie with the name of CartCookie exists so that we not // will get any null point exeptions if the HttpCookie does not exist. if (CartCookie != null) { if (CartCookie.HasKeys) { foreach (RepeaterItem RepeaterRow in CartRepeater.Items) { Literal ProductObj = (Literal)RepeaterRow.FindControl("ltProductID"); TextBox QuantityObj = (TextBox)RepeaterRow.FindControl("txtQuantity"); CartCookie.Values[ProductObj.Text] = QuantityObj.Text; CartCookie.Expires = DateTime.Now.AddHours(3); Response.Cookies.Add(CartCookie); } } // Recalculate the shopping cart CalculateCartSums(); // Update the small shopping cart by calling a public method in the Start class for the masterpage. ((Start1)this.Master).LoadSmallCart(); } }
protected void CalculateOrderSums() { // We declare two variables that we will use for our calculations of total sums. decimal PriceExVat = 0m; decimal VatMoney = 0m; // We iterate through each row in the "ProductRowRepeater and add values to our two variables foreach (RepeaterItem RepeaterRow in ProductRowRepeater.Items) { Literal VatObj = (Literal)RepeaterRow.FindControl("ltVAT"); Literal RowSumObj = (Literal)RepeaterRow.FindControl("ltRowSum"); PriceExVat += Convert.ToDecimal(RowSumObj.Text); VatMoney += Convert.ToDecimal(RowSumObj.Text) * (Convert.ToDecimal(VatObj.Text) / 100); } // We add the sums to labels and calculate the totalsum as price excluding VAT plus VAT in money lblPriceTotal.Text = Convert.ToString(PriceExVat); lblVatTotal.Text = Convert.ToString(VatMoney); lblTotalSum.Text = Convert.ToString(PriceExVat + VatMoney); }
protected void CalculateCartSums() { // We declare variables to calculate totals for the shopping cart. decimal PriceTotal = 0m; decimal VatTotal = 0m; // We iterate through each row in the "CartRepeater" and add values to our three variables foreach (RepeaterItem RepeaterRow in CartRepeater.Items) { Literal VatObj = (Literal)RepeaterRow.FindControl("ltVAT"); Literal PriceExVat = (Literal)RepeaterRow.FindControl("ltPrice"); TextBox QuantityObj = (TextBox)RepeaterRow.FindControl("txtQuantity"); // Additions for totals PriceTotal += Convert.ToDecimal(PriceExVat.Text) * Convert.ToDecimal(QuantityObj.Text); VatTotal += Convert.ToDecimal(PriceExVat.Text) * Convert.ToDecimal(QuantityObj.Text) * (Convert.ToDecimal(VatObj.Text) / 100); } // We set the totals to labels under our repeater control and calculate the totalsum lblPriceTotal.Text = Convert.ToString(PriceTotal); lblVatTotal.Text = Convert.ToString(VatTotal); lblTotalSum.Text = Convert.ToString(PriceTotal + VatTotal); }
protected void btnOrder_Click(object sender, System.EventArgs e) { // We declare the variable "Identity" that will store the OrderID of the order that is added so that we can use // this OrdeID number when we are to insert data in the table "OrderProduct" that has a many to many relationsship // to the Order table. Check database diagrams to se the relationships between tables. Int64 Identity = 0; // We insert an order in the "Orders" table that has OrderID as a identity field that increments with 1. // We have added ; SELECT SCOPE_IDENTITY() to the SQL statement to get the OrderID of the inserted order // and use "ExecuteScalar()" instead of "ExecuteNonQuery()" in the SQL command. try { string ConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString(); string sql = "INSERT INTO Orders (OrderDate, CustomerID, Company, OrgNumber, Contact, Attention, " + "Adress, PostalCode, City, Country) VALUES (@OrderDate, @CustomerID, @Company, @OrgNumber, " + "@Contact, @Attention, @Adress, @PostalCode, @City, @Country); SELECT SCOPE_IDENTITY()"; // Create a SqlConnection. The using block is used to call dispose (close) automatically even if // there are an exception. using (SqlConnection cn = new SqlConnection(ConnString)) { // Create a SqlCommand. The using block is used to call dispose (close) automatically even if // there are an exception. using (SqlCommand cmd = new SqlCommand(sql, cn)) { // Add parameters. cmd.Parameters.AddWithValue("@OrderDate", DateTime.Now.ToString()); cmd.Parameters.AddWithValue("@CustomerID", HiddenCustomerID.Value); cmd.Parameters.AddWithValue("@Company", txtCompany.Text); cmd.Parameters.AddWithValue("@OrgNumber", txtOrganisationNumber.Text); cmd.Parameters.AddWithValue("@Contact", txtContact.Text); cmd.Parameters.AddWithValue("@Attention", txtAttention.Text); cmd.Parameters.AddWithValue("@Adress", txtAdress.Text); cmd.Parameters.AddWithValue("@PostalCode", txtPostalCode.Text); cmd.Parameters.AddWithValue("@City", txtCity.Text); cmd.Parameters.AddWithValue("@Country", txtCountry.Text); // Open the connection cn.Open(); // Execute the INSERT statement and get the Identity number. Identity = Convert.ToInt64(cmd.ExecuteScalar()); } } } catch (SqlException Sqlex) { Response.Write(Sqlex.Message); } catch (Exception ex) { Response.Write(ex.Message); } // When we have inserted an order we want to insert the product rows for the order in the "OrdersProducts" table. // We first check to see that the identity variable not has a blank value before we insert data to the // "OrdersProducts" table. if (Identity > 0) { // We iterate through each repeater row in the "CartRepeater" and insert every row in the // "OrdersProduct" table. foreach (RepeaterItem RepeaterRow in CartRepeater.Items) { Literal ProductIDObj = (Literal)RepeaterRow.FindControl("ltProductID"); Literal VATObj = (Literal)RepeaterRow.FindControl("ltVAT"); TextBox QuantityObj = (TextBox)RepeaterRow.FindControl("txtQuantity"); Literal PriceObj = (Literal)RepeaterRow.FindControl("ltPrice"); try { string ConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString(); string sql = "INSERT INTO OrdersProducts (OrderID, ProductID, SaleTaxPercent, Quantity, PriceExSaleTax) VALUES (@OrderID, @ProductID, @SaleTaxPercent, @Quantity, @PriceExSaleTax)"; // The using block is used to call dispose (close) automatically even if there are an exception. using (SqlConnection cn = new SqlConnection(ConnString)) { using (SqlCommand cmd = new SqlCommand(sql, cn)) { cmd.Parameters.AddWithValue("@OrderID", Identity); cmd.Parameters.AddWithValue("@ProductID", ProductIDObj.Text); cmd.Parameters.AddWithValue("@SaleTaxPercent", Convert.ToDecimal(VATObj.Text) / 100); cmd.Parameters.AddWithValue("@Quantity", Convert.ToDecimal(QuantityObj.Text)); cmd.Parameters.AddWithValue("@PriceExSaleTax", Convert.ToDecimal(PriceObj.Text)); cn.Open(); cmd.ExecuteNonQuery(); } } } catch (SqlException Sqlex) { Response.Write(Sqlex.Message); } catch (Exception ex) { Response.Write(ex.Message); } } // Delete the HttpCookie, we declare the variable CartCookie to check if the HttpCookie "CartCookie" // exits and to be able to remove it. HttpCookie CartCookie = Request.Cookies.Get("CartCookie"); // We check if the CartCookie exists and then sets the expiration date to current DateTime minus one hour if (CartCookie != null) { CartCookie.Expires = DateTime.Now.AddHours(-1); Response.Cookies.Add(CartCookie); } // Redirect the user to the order list webpage Response.Redirect("Orders.aspx"); } }