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 update_Click(object sender, EventArgs e) { int rowNum = productDataGridView.CurrentCell.RowIndex; // index of the current row int prodCode = (int)productDataGridView["dataGridViewTextBoxColumn1", rowNum].Value; // Column for ProductCode Product currentproduct; using (productsDataContext dbContext = new productsDataContext()) { currentproduct = (from p in dbContext.Products where p.ProductId == prodCode select p).Single(); } frmProductAddModfy newform = new frmProductAddModfy(); newform.isAdd = false; newform.currentProduct = currentproduct; DialogResult result = newform.ShowDialog(); if (result == DialogResult.OK) { RefreshProducts(); } }
public void RefreshProducts() { productsDataContext db = new productsDataContext(); productBindingSource.DataSource = db.Products; }