//user clicks on Add product private void btnAdd_Click(object sender, EventArgs e) { var addModifyProductForm = new frmAddorModify() { AddProduct = true // initializer }; DialogResult result = addModifyProductForm.ShowDialog(); //display model if (result == DialogResult.OK) // user clicked Accept on the second form { try { selectedProduct = addModifyProductForm.Product; // record product from the second // form as the current product context.Products.Add(selectedProduct); context.SaveChanges(); DisplayProducts(); } catch (DbUpdateException ex) { HandleDatabaseError(ex); } catch (Exception ex) { HandleGeneralError(ex); } } }
//user clicks on the modify button to update existing record private void btnModify_Click(object sender, EventArgs e) { var addModifyProductForm = new frmAddorModify() { // object initializer AddProduct = false, Product = selectedProduct }; DialogResult result = addModifyProductForm.ShowDialog(); // display modal if (result == DialogResult.OK) // user clicked OK on the second form { try { selectedProduct = addModifyProductForm.Product; // new data context.SaveChanges(); DisplayProducts(); } catch (DbUpdateException ex) { HandleDatabaseError(ex); } catch (Exception ex) { HandleGeneralError(ex); } } //ModifyProduct(); }