private void save_Click(object sender, EventArgs e) { if (isAdd) { if (Validator.IsPresent(prodNameTextBox) == true) { Product newProduct = new Product // create product using provided data { ProdName = prodNameTextBox.Text }; using (productsDataContext db = new productsDataContext()) { try { // insert through data context object from the main form db.Products.InsertOnSubmit(newProduct); db.SubmitChanges(); // submit to the database } catch (Exception ex) { MessageBox.Show(prodNameTextBox.Text + " is already there"); } } DialogResult = DialogResult.OK; } } else { if (Validator.IsPresent(prodNameTextBox) == true) { using (productsDataContext dbContext = new productsDataContext()) { // get the product with id from the current text box Product prod = dbContext.Products.Single(p => p.ProductId == currentProduct.ProductId); if (prod != null) { // make changes by copying values from text boxes prod.ProdName = prodNameTextBox.Text; try { // submit changes dbContext.SubmitChanges(); DialogResult = DialogResult.OK; } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } } } } }
private void btnSave_Click(object sender, EventArgs e) { if (isAdd) { if (Validator.IsPresent(productCodeTextBox) && Validator.IsCorrectLength(productCodeTextBox, 10) && IsUniqueCode(productCodeTextBox) && // coded below in this form Validator.IsPresent(descriptionTextBox) && Validator.IsCorrectLength(descriptionTextBox, 50) && Validator.IsPresent(unitPriceTextBox) && Validator.IsDecimal(unitPriceTextBox) && Validator.IsNonNegativeDecimal(unitPriceTextBox) && Validator.IsPresent(onHandQuantityTextBox) && Validator.IsInt32(onHandQuantityTextBox) && Validator.IsNonNegativeInt(onHandQuantityTextBox) ) // replace with data validation: all fields provided, code unique, // price and quantity appropriate numeric type and non-negative { Product newProduct = new Product // create product using provided data { ProductCode = productCodeTextBox.Text, Description = descriptionTextBox.Text, UnitPrice = Convert.ToDecimal(unitPriceTextBox.Text), OnHandQuantity = Convert.ToInt32(onHandQuantityTextBox.Text) }; using (ProductsDataContext dbContext = new ProductsDataContext()) { // insert through data context object from the main form dbContext.Products.InsertOnSubmit(newProduct); dbContext.SubmitChanges(); // submit to the database } DialogResult = DialogResult.OK; } else // validation failed { DialogResult = DialogResult.Cancel; } } else // it is Modify { if (Validator.IsPresent(descriptionTextBox) && Validator.IsCorrectLength(descriptionTextBox, 50) && Validator.IsPresent(unitPriceTextBox) && Validator.IsDecimal(unitPriceTextBox) && Validator.IsNonNegativeDecimal(unitPriceTextBox) && Validator.IsPresent(onHandQuantityTextBox) && Validator.IsInt32(onHandQuantityTextBox) && Validator.IsNonNegativeInt(onHandQuantityTextBox) ) { try { using (ProductsDataContext dbContext = new ProductsDataContext()) { // get the product with Code from the current text box Product prod = dbContext.Products.Single(p => p.ProductCode == productCodeTextBox.Text); //MessageBox.Show("Testing concurrency: update or delete current record from SSMS and click OK"); if (prod != null) { // make changes by copying values from text boxes prod.Description = descriptionTextBox.Text; prod.UnitPrice = Convert.ToDecimal(unitPriceTextBox.Text); prod.OnHandQuantity = Convert.ToInt32(onHandQuantityTextBox.Text); // submit changes dbContext.SubmitChanges(); DialogResult = DialogResult.OK; } } } catch (ChangeConflictException) { MessageBox.Show("Another user changed or deleted the current record", "Concurrency Exception"); DialogResult = DialogResult.Retry; } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } } else // validation failed { DialogResult = DialogResult.Cancel; } } }
private void btnSave_Click(object sender, EventArgs e) { if (isAdd) { if (Validator.IsProvided(txtProdName, "Product Name") && Validator.isNotNumeric(txtProdName, "Product Name") && Validator.IsCorrectLength(txtProdName, 50) // the number 50 is the # of characters (length) ) // data validation: all fields provided, code unique, // price and quantity appropriate numeric type and non-negative { Product newProduct = new Product // create product using provided data { ProdName = txtProdName.Text, }; // object initializer using (TravelExpertDataContext dbContext = new TravelExpertDataContext()) { // insert through data context object from the main form dbContext.Products.InsertOnSubmit(newProduct); dbContext.SubmitChanges(); // submit to the database } DialogResult = DialogResult.OK; } else // validation failed { DialogResult = DialogResult.Cancel; } } else // it is Modify { if (Validator.IsProvided(txtProdName, "Product Name") && Validator.isNotNumeric(txtProdName, "Product Name") && Validator.IsCorrectLength(txtProdName, 50) ) { try { using (TravelExpertDataContext dbContext = new TravelExpertDataContext()) { // get the product with Code from the current text box Product prod = dbContext.Products.Single(p => p.ProductId == Convert.ToInt32(txtProdId.Text)); if (prod != null) { // make changes by copying values from text boxes prod.ProdName = txtProdName.Text; //****check this code*** // submit changes dbContext.SubmitChanges(); DialogResult = DialogResult.OK; } } } catch (ChangeConflictException) { MessageBox.Show("Another user changed or deleted the current record", "Concurrency Exception"); DialogResult = DialogResult.Retry; } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } } else // validation failed { DialogResult = DialogResult.Cancel; } } }