public IActionResult OnPost() { NewProduct.Description = ProductDescription; NewProduct.Id = OldProduct.Id; int click = Int32.Parse(Request.Form["click"]); if (click == 1) { ProductsDB.UpdateProduct(NewProduct, _configuration); string currentShoppingCart = Request.Cookies["ShoppingCart"]; if (currentShoppingCart != null && currentShoppingCart != "") { string newShoppingCart = ProductsCookies.UpdateProduct(currentShoppingCart, NewProduct); var cookieOptions = new CookieOptions { Expires = DateTime.Now.AddDays(1) }; Response.Cookies.Append("ShoppingCart", newShoppingCart, cookieOptions); } return(RedirectToPage("/Index")); } else { return(RedirectToPage("/Index")); } }
private void btnEdit_Click(object sender, EventArgs e) { try { if (Validator.IsPresentCombo(productIdComboBox, "ID") && Validator.IsPresent(prodNameTextBox, "New Product Name") && Validator.IsCorrectLength(prodNameTextBox, "Name", 20) && Validator.IsNameValid(prodNameTextBox, "Name")) { Products findOldProduct = FindOldProduct(productIdComboBox); if (findOldProduct != null) { Products newProduct = productUpdate(prodNameTextBox, CreateObject(findOldProduct)); bool Success = ProductsDB.UpdateProduct(findOldProduct, newProduct); if (Success) { MessageBox.Show("Product is updated :)"); } UpdateAllInfos(); productsDataGridView.DataSource = AllProducts; } else { MessageBox.Show("Cant find Product ID"); } } else { MessageBox.Show("Please re-enter. "); } } //catch the other error and show the ex error message from db class catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } }
// btn SAVE: save edit (update) or save add (create) private void threeBtnSave_Click(object sender, EventArgs e) { if (threeTab.SelectedIndex == 1) // EDIT MODE { // get current displaying product obj var currentProd = ProductsDB.GetProducts(). SingleOrDefault(p => p.ProductId == Convert.ToInt32(threeTxtEditProdId.Text)); // initiate new product obj Products newProd; // validate: product name is empty or duplicated if (Validator.TBIsEmpty(threeTxtEditProdName, "Product Name") || FindDuplicatedProductName(threeTxtEditProdName.Text)) { // empty or same, do not perform update threeTxtEditProdName.Text = currentProd.ProdName; threeTxtEditProdName.SelectAll(); return; } else { // name is valid, create new Product obj newProd = new Products { ProdName = threeTxtEditProdName.Text } }; // try to perform update try { Console.WriteLine("Old prod name is: " + currentProd.ProdName); var rowsAffected = ProductsDB.UpdateProduct(currentProd, newProd); MessageBox.Show($"{rowsAffected} record was updated.", "Congratulations"); } catch (Exception ex) { MessageBox.Show(ex.Message); } } else if (threeTab.SelectedIndex == 2) // ADD MODE { // validate input, check if empty or have duplicated name if (!Validator.TBIsEmpty(threeTxtAddProdName, "Product Name") && !FindDuplicatedProductName(threeTxtAddProdName.Text)) { // validation passed, create new product using user input var newProd = new Products { ProdName = threeTxtAddProdName.Text }; // try to insert into DB try { var id = ProductsDB.AddProduct(newProd); MessageBox.Show($"New product {newProd.ProdName} is added, product id is: {id}.", "Congratulations"); threeTxtAddProdName.Clear(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } } }