protected void btnUpdateCart_Click(object sender, EventArgs e) { // Connect to the database DatabaseMgmt objdbMgmt = new DatabaseMgmt(); string strSqlCmd; // Iterate through all rows within shopping cart list // (i.e. the dgShopCart data grid) int i; for (i = 0; i <= dgShopCart.Rows.Count - 1; i++) { // Obtain references to row's controls TextBox txtQuantity; txtQuantity = (TextBox)dgShopCart.Rows[i].FindControl("txtQty"); // Get the quantity value from textbox // and convert to integer int intQuantity; intQuantity = Convert.ToInt16(txtQuantity.Text); if (intQuantity <= 0) { Response.Write("<script>alert('Invalid quantity');</script>"); return; } // Get the ProductId from the first cell // and convert to integer int intProductID; intProductID = Convert.ToInt16(dgShopCart.Rows[i].Cells[0].Text); strSqlCmd = $"select quantity from Product where productId = {intProductID}"; SqlDataReader dR = objdbMgmt.ExecuteSelect(strSqlCmd); if (dR.Read()) { if (intQuantity > int.Parse(dR["quantity"].ToString())) { Response.Write("<script>alert('Invalid quantity');</script>"); return; } } objdbMgmt.Close(); // Update quantity order of shopping cart in database strSqlCmd = "UPDATE ShopCartItem SET Quantity=" + intQuantity + " WHERE ShopCartID=" + Session["ShopCartID"] + " AND ProductId=" + intProductID; objdbMgmt.Open(); objdbMgmt.ExecuteNonQuery(strSqlCmd); } displayShopCart(); objdbMgmt.Close(); }
protected void Page_Load(object sender, EventArgs e) { // Connect to the database DatabaseMgmt objdbMgmt = new DatabaseMgmt(); objdbMgmt.Connect(); string strSqlCmd; // System.Data.OleDb.OleDbDataReader objDataReader; System.Data.SqlClient.SqlDataReader objDataReader; double curOringalPrice; // Retrieve the details of product of a given product ID // from the database int intProductId; intProductId = Convert.ToInt32(Request.QueryString["ProductId"]); strSqlCmd = "SELECT * FROM Product WHERE ProductId=" + intProductId; objDataReader = objdbMgmt.ExecuteSelect(strSqlCmd); // Read the only record retrieved if (objDataReader.Read()) { // Display the product title lblProductTitle.Text = (string)objDataReader["ProductTitle"]; // Display the product description if (objDataReader["ProductDesc"] != DBNull.Value) { lblProductDesc.Text = (string)objDataReader["ProductDesc"]; } // Display the product image imgProduct.ImageUrl = "images/products/" + (string)objDataReader["ProductImage"]; // Display the product price // Format to display two decimals curOringalPrice = Convert.ToDouble(objDataReader["Price"]); lblProductPrice.Text = "$" + string.Format(Convert.ToString(curOringalPrice), "0.00"); objDataReader.Close(); } // Retrieve the dynamic attributes of a given product strSqlCmd = "SELECT an.AttributeName, pa.AttributeVal FROM AttributeName an INNER JOIN ProductAttribute pa ON an.AttributeNameID=pa.AttributeNameID WHERE pa.ProductID=" + intProductId; objDataReader = objdbMgmt.ExecuteSelect(strSqlCmd); // Bind the records to the data grid control dgAttribute.DataSource = objDataReader; dgAttribute.DataBind(); objDataReader.Close(); objdbMgmt.Close(); }
protected void Page_Load(object sender, EventArgs e) { DatabaseMgmt dbObj = new DatabaseMgmt(); string strSqlCmd; SqlDataReader sqlDataReader; strSqlCmd = "Select * From Department"; sqlDataReader = dbObj.ExecuteSelect(strSqlCmd); dgDept.DataSource = sqlDataReader; dgDept.DataBind(); dbObj.Close(); }
private void displayShopCart() { lblEmptyShopCart.Visible = false; dgShopCart.Visible = true; lblSubTotal.Visible = true; btnUpdateCart.Visible = true; btnCheckOut.Visible = true; // Establish a database connection DatabaseMgmt objdbMgmt = new DatabaseMgmt(); // Get items in the designated shopping cart. string strSqlCmd; SqlDataReader objDataReader; strSqlCmd = "SELECT ShopCartID, ProductID, Name, Price, Quantity, DateAdded, Price * Quantity as Total FROM ShopCartItem WHERE ShopCartID=" + Session["ShopCartId"]; objDataReader = objdbMgmt.ExecuteSelect(strSqlCmd); // Bind the records to the data list control dgShopCart.DataSource = objDataReader; dgShopCart.DataBind(); objDataReader.Close(); // Check for empty shopping cart if (dgShopCart.Rows.Count == 0) { // Call a subroutine to display empty Shopping Cart message displayEmptyShopCart(); } else { // Calculate the subtotal. double dblSubTotal; strSqlCmd = "SELECT SUM(Price * Quantity) as SubTotal FROM ShopCartItem WHERE ShopCartID=" + Session["ShopCartId"]; dblSubTotal = Convert.ToDouble(objdbMgmt.ExecuteScalar(strSqlCmd)); // Display the subtotal. string strSubTotal = string.Format("{0:c}", dblSubTotal, 2); lblSubTotal.Text = "Subtotal (before tax and shipping charge): " + strSubTotal; } objdbMgmt.Close(); }
protected void Page_Load(object sender, EventArgs e) { // Don't allow non-member to purchase a product if (Session["ShopperID"] == null) { Response.Redirect("Login.aspx"); } int intProductID; string strProductName; double dblProductPrice; int intQuantity; int intShopCartID; int intShopperID; bool flgAddItem; string strSqlCmd; // Read product's details from session variables //intProductID = Convert.ToInt32(Session["ProductID"]); intProductID = Convert.ToInt32(Request.QueryString["ProductId"]); dblProductPrice = Convert.ToDouble(Double.Parse((string)Session["ProductPrice"], System.Globalization.NumberStyles.Currency)); intQuantity = Convert.ToInt32(Session["Quantity"]); // Replace any single-quote with two single-quote strProductName = Convert.ToString(Session["ProductName"]); // Connect to the database DatabaseMgmt objdbMgmt = new DatabaseMgmt(); //objdbMgmt.Connect(); // If it is a new item, add item into shopping cart. strSqlCmd = $"Delete from ShopCartItem where productId ='{intProductID}'"; objdbMgmt.ExecuteNonQuery(strSqlCmd); objdbMgmt.Close(); // Re-direct to ShopCart page for display of shopping cart Response.Redirect("ShopCart.aspx"); }
protected void Page_Load(object sender, EventArgs e) { // Don't allow non-member to purchase a product if (Session["ShopperID"] == null) { Response.Redirect("Login.aspx"); } int intProductID; string strProductName; double dblProductPrice; int intQuantity; int intShopCartID; int intShopperID; bool flgAddItem; string strSqlCmd; // Read product's details from session variables intProductID = Convert.ToInt32(Session["ProductID"]); dblProductPrice = Convert.ToDouble(Double.Parse((string)Session["ProductPrice"], System.Globalization.NumberStyles.Currency)); intQuantity = Convert.ToInt32(Session["Quantity"]); // Replace any single-quote with two single-quote strProductName = Convert.ToString(Session["ProductName"]); // Connect to the database DatabaseMgmt objdbMgmt = new DatabaseMgmt(); //objdbMgmt.Connect(); // Check whether a shopping cart has been created. if (Session["ShopCartID"] == null) { // Create a new shopping cart //--------------------------- intShopperID = Convert.ToInt32(Session["ShopperID"]); strSqlCmd = "INSERT INTO ShopCart(ShopperID) VALUES(" + intShopperID + ")"; objdbMgmt.ExecuteNonQuery(strSqlCmd); // Get the new Shopping Cart ID strSqlCmd = "SELECT MAX(ShopCartID) AS maxShopCartID " + "FROM ShopCart WHERE ShopperID=" + intShopperID; // Save the Shopping Cart ID in a session variable Session["ShopCartID"] = objdbMgmt.ExecuteScalar(strSqlCmd); // Set AddItem flag to true - to add item to shopping cart. flgAddItem = true; } else { // Shopping cart already created //------------------------------- intShopCartID = Convert.ToInt32(Session["ShopCartID"]); // Retrieve selected product from shopping cart. System.Data.SqlClient.SqlDataReader objDataReader; strSqlCmd = "SELECT Quantity FROM ShopCartItem WHERE ProductID=" + intProductID + " AND ShopCartID=" + intShopCartID; objDataReader = objdbMgmt.ExecuteSelect(strSqlCmd); // Check whether selected item is already in shopping cart. // If yes, update order quantity instead of adding new item. if (objDataReader.HasRows) { // Close the data reader objDataReader.Close(); // update quantity of Shop Cart Item strSqlCmd = "UPDATE ShopCartItem SET Quantity = Quantity + " + intQuantity + " WHERE ProductID =" + intProductID + " AND ShopCartID =" + intShopCartID; objdbMgmt.ExecuteNonQuery(strSqlCmd); // Set AddItem flag to false - do not add item to cart flgAddItem = false; } else { // Close the data reader objDataReader.Close(); // Set AddItem flag to true - to add item to cart flgAddItem = true; } } // If it is a new item, add item into shopping cart. if (flgAddItem) { // Add Shop Cart Item to the database strSqlCmd = "INSERT INTO ShopCartitem (ShopCartID, Quantity, Price, Name, ProductID)" + " VALUES (" + Session["ShopCartId"] + "," + intQuantity + "," + dblProductPrice + ",'" + strProductName + "'," + intProductID + ")"; objdbMgmt.ExecuteNonQuery(strSqlCmd); } objdbMgmt.Close(); // Re-direct to ShopCart page for display of shopping cart Response.Redirect("ShopCart.aspx"); }