private void btnSave_Click(object sender, EventArgs e) { try { using (TravelExpertDataContext dataContext = new TravelExpertDataContext()) { Products_Supplier prodSupplier = null; if (isModify)// MODIFY ITEM { prodSupplier = (from m in dataContext.Products_Suppliers where m.ProductSupplierId == Convert.ToInt32(txtproductSupplierId.Text) select m).Single(); prodSupplier.ProductId = Convert.ToInt32(productIdComboBox.SelectedItem); prodSupplier.SupplierId = Convert.ToInt32(supplierIdcomboBox.SelectedItem); } else // ADD NEW ITEM { prodSupplier = new Products_Supplier { ProductId = Convert.ToInt32(productIdComboBox.SelectedItem), SupplierId = Convert.ToInt32(supplierIdcomboBox.SelectedItem) };// object initializer syntax dataContext.Products_Suppliers.InsertOnSubmit(prodSupplier); } dataContext.SubmitChanges(); MessageBox.Show("Changes have been saved", "Data update"); DialogResult = DialogResult.OK; } } catch (SqlException ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } }
private void productIdComboBox_SelectedIndexChanged(object sender, EventArgs e) { using (TravelExpertDataContext dataContext = new TravelExpertDataContext()) { var selectedValue = (from m in dataContext.Products where m.ProductId == Convert.ToInt32(productIdComboBox.SelectedValue) select m).Single(); prodNameTextBox.Text = selectedValue.ProdName; } }
private void supplierIdcomboBox_SelectedIndexChanged(object sender, EventArgs e) { using (TravelExpertDataContext dataContext = new TravelExpertDataContext()) { var selectedValue = (from m in dataContext.Suppliers where m.SupplierId == Convert.ToInt32(supplierIdcomboBox.SelectedValue) select m).Single(); supNameTextBox.Text = selectedValue.SupName; } }
private void frmAddModifyProdSuppltbl_Load(object sender, EventArgs e) { using (TravelExpertDataContext dataContext = new TravelExpertDataContext()) { productIdList = (from m in dataContext.Products select m.ProductId).ToList(); productIdComboBox.DataSource = productIdList; productIdComboBox.Text = "Select Product ID"; supplierIdList = (from m in dataContext.Suppliers select m.SupplierId).ToList(); supplierIdcomboBox.DataSource = supplierIdList; supplierIdcomboBox.Text = "Select Supplier ID"; } if (isModify) { txtproductSupplierId.Text = activeProdSuppId.ProductSupplierId.ToString(); productIdComboBox.Text = activeProdSuppId.ProductId.ToString(); supplierIdcomboBox.Text = activeProdSuppId.SupplierId.ToString(); } }
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; } } }