private void DeleteSupplierProductToolStripMenuItem_Click(object sender, EventArgs e) { // get the key of the current product in the data grid view int rowNum = productListGridView.CurrentCell.RowIndex; Products_Supplier ps = ProductSupplierList[rowNum]; DialogResult answer = MessageBox.Show($"Are you sure you want to delete{ps.ProductSupplierId} ?", "Confirmation Message", MessageBoxButtons.YesNo); if (answer == DialogResult.Yes) { using (ProductSuppliersDataContext dbContext = new ProductSuppliersDataContext()) { try { //Delete and update the grid view information Products_Supplier currentProductSupplier = dbContext.Products_Suppliers.SingleOrDefault(x => x.ProductSupplierId == ps.ProductSupplierId); dbContext.Products_Suppliers.DeleteOnSubmit(currentProductSupplier); dbContext.SubmitChanges(); ProductSupplierList.RemoveAt(rowNum); RefreshGridView(); } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } } } }
private void AddSupplierButton_Click(object sender, EventArgs e) { if (isAdd) { if ( Validator.IsInt32(productIDTextbox) && Validator.IsNonNegativeInt(productIDTextbox) && Validator.IsInt32(supplierIDtextbox) && Validator.IsNonNegativeInt(supplierIDtextbox) ) { try { using (ProductSuppliersDataContext dbContext = new ProductSuppliersDataContext()) { Products_Supplier newProduct_Supplier = new Products_Supplier // create product supplier using provided data { ProductId = int.Parse(productIDTextbox.Text), SupplierId = int.Parse(supplierIDtextbox.Text) }; // insert through data context object from the main form dbContext.Products_Suppliers.InsertOnSubmit(newProduct_Supplier); dbContext.SubmitChanges(); // submit to the database currentProductSupplier = newProduct_Supplier; } } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); return; } DialogResult = DialogResult.OK; } else // validation failed { return; } } else // it is Modify { if (Validator.IsInt32(productIDTextbox) && Validator.IsNonNegativeInt(productIDTextbox) && Validator.IsInt32(supplierIDtextbox) && Validator.IsNonNegativeInt(supplierIDtextbox) ) { try { using (ProductSuppliersDataContext dbContext = new ProductSuppliersDataContext()) { // get the product with Code from the current text box Products_Supplier ps = dbContext.Products_Suppliers.Single(p => p.ProductSupplierId == int.Parse(productSupplierIDTextbox.Text)); //MessageBox.Show("Testing concurrency: update or delete current record from SSMS and click OK"); if (ps != null) { // make changes by copying values from text boxes ps.ProductId = int.Parse(productIDTextbox.Text); ps.SupplierId = int.Parse(supplierIDtextbox.Text); //submit dbContext.SubmitChanges(); DialogResult = DialogResult.OK; } } } catch (ChangeConflictException) { MessageBox.Show("Another user changed or deleted the current record", "Concurrency Exception"); return; } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); return; } } else // validation failed { return; } } }