protected void btnAddToCart_Click(object sender, EventArgs e) { //make sure that there are enough units in stock if (vldQuantity.IsValid) //<stretch>Should also have server side validation in case of multiple users/connections</stretch> { NorwinDBTableAdapters.CartTableAdapter cart = new NorwinDBTableAdapters.CartTableAdapter(); NorwinDBTableAdapters.ProductsTableAdapter products = new NorwinDBTableAdapters.ProductsTableAdapter(); //get input int numberToOrder = int.Parse(cbxQuantity.SelectedValue); float unitPrice = (float)products.GetUnitPriceByProdID(Convert.ToInt32(Request.QueryString[0])); float subTotal = (float)numberToOrder * unitPrice; //add the selected quantity of the item to the cart table in the DB cart.InsertCartItem(prodName, numberToOrder, prodID, custID, Session.SessionID, unitPrice, subTotal); products.UpdateInStock((short)(unitsInStock - numberToOrder), prodID); lsvProdDetails.DataSource = products.GetDataByProdID(Convert.ToInt32(Request.QueryString[0])); lsvProdDetails.DataBind(); lblItemAdded.Text = numberToOrder + " items added to order."; } else { lblItemAdded.Text = "..."; } }
protected void btnRemove_Click(object sender, EventArgs e) { custID = cbxCustomer.SelectedValue; //casting sender to a Button to make use of the CommandArgument property. Not sure if this is good practice, feels a little hacky. Button btnRm = (Button)sender; //Must pass it all as one string from the page, so it takes a few extra steps to split it up into the required types. string[] theArgs = btnRm.CommandArgument.Split(','); int productId = int.Parse(theArgs[0]); int cartItemQuantity = int.Parse(theArgs[1]); int cartItemId = int.Parse(theArgs[2]); int currentlyInStock = Convert.ToInt32(products.GetInStockByProdID(productId)); //delete the row from the db in the cart cart.removeCartItem(cartItemId); //refresh the view lsvCart.DataSource = cart.GetDataByCustID(custID); lsvCart.DataBind(); products.UpdateInStock((short)(cartItemQuantity + currentlyInStock), productId); }