//Edit products private void btnEditProduct_Click(object sender, EventArgs e) { frmAddEditProducts addProductForm = new frmAddEditProducts(); addProductForm.addProduct = false; // get the key of the current product in the data grid view int rowNum = Convert.ToInt32(productDataGridView.CurrentCell.RowIndex); // index of the current row int ordernum = Convert.ToInt32(productDataGridView["dataGridViewTextBoxColumn12", rowNum].Value); // Column for productid using (TravelExpertDBDataContext db = new TravelExpertDBDataContext()) { addProductForm.currentProduct = (from p in db.Products where p.ProductId == ordernum select p).Single(); } DialogResult result = addProductForm.ShowDialog(); // display second form modal using (TravelExpertDBDataContext db = new TravelExpertDBDataContext()) { if (result == DialogResult.OK) // successful update { productDataGridView.DataSource = db.Products; // refresh grid MessageBox.Show("Product Name Edited Successfully"); } } }
//Edit supplier private void btnEditSupplier_Click(object sender, EventArgs e) { frmAddEditSuppliers addSupplierForm = new frmAddEditSuppliers(); addSupplierForm.addSupplier = false; // Get the key of the current supplier in the data grid view // Index of the current row int suprowNum = Convert.ToInt32(supplierDataGridView.CurrentCell.RowIndex); // Column for product Id int supordernum = Convert.ToInt32(supplierDataGridView["dataGridViewTextBoxColumn14", suprowNum].Value); using (TravelExpertDBDataContext db = new TravelExpertDBDataContext()) { addSupplierForm.currentSupplier = (from p in db.Suppliers where p.SupplierId == supordernum select p).Single(); } // Display second form model DialogResult result = addSupplierForm.ShowDialog(); using (TravelExpertDBDataContext db = new TravelExpertDBDataContext()) { // Update is successful if (result == DialogResult.OK) { // Refresh grid supplierDataGridView.DataSource = db.Suppliers; MessageBox.Show("Supplier Name Edited Successfully"); } } }
//Edit package private void btnEditPackage_Click(object sender, EventArgs e) { frmAddEditPackages addPackageForm = new frmAddEditPackages(); addPackageForm.addPackage = false; // get the key of the current package in the data grid view // Index of the current row int pkgrowNum = Convert.ToInt32(packageDataGridView.CurrentCell.RowIndex); // Column for package Id int pkgordernum = Convert.ToInt32(packageDataGridView["dataGridViewTextBoxColumn1", pkgrowNum].Value); using (TravelExpertDBDataContext db = new TravelExpertDBDataContext()) { addPackageForm.currentPackage = (from pk in db.Packages where pk.PackageId == pkgordernum select pk).Single(); } // Display second form modal DialogResult result = addPackageForm.ShowDialog(); using (TravelExpertDBDataContext db = new TravelExpertDBDataContext()) { // Update is successful if (result == DialogResult.OK) { // Refresh grid packageDataGridView.DataSource = db.Packages; //package_ProductsDataGridView.DataSource = db.Package_Products; MessageBox.Show("Package Edited Successfully"); } } }
//When form loads private void frmAddProductToPackage_Load(object sender, EventArgs e) { txtPackageID.Text = currentPackage.PackageId.ToString(); pnlProdSupIdForAddProduct.Visible = false; // populate the list of available products in the list using (TravelExpertDBDataContext db = new TravelExpertDBDataContext()) { var products = (from prod in db.Products select prod.ProdName).ToList(); prodNameListBox.DataSource = products; } }
//Edit supplier of product private void btnEditProductSupplier_Click(object sender, EventArgs e) { frmAddEditProductSuppliers addProductSupplierForm = new frmAddEditProductSuppliers(); addProductSupplierForm.addProductSupplier = false; using (TravelExpertDBDataContext db = new TravelExpertDBDataContext()) { // Index of the current row var prodsupid = (from m in db.Products_Suppliers join p in db.Products on m.ProductId equals p.ProductId where p.ProdName == products_SupplierDataGridView.SelectedRows.ToString() select p); var prods = (from m in db.Products_Suppliers join p in db.Suppliers on m.SupplierId equals p.SupplierId where p.SupName == products_SupplierDataGridView.SelectedRows.ToString() select p); int prodsuprowNum = Convert.ToInt32(products_SupplierDataGridView.CurrentCell.RowIndex); // Column for productsupplier Id int prosupordernum = Convert.ToInt32(products_SupplierDataGridView["dataGridViewTextBoxColumn8", prodsuprowNum].Value); addProductSupplierForm.cur = (from p in db.Products_Suppliers where p.ProductId == Convert.ToInt32(prodsupid) && p.SupplierId == Convert.ToInt32(prods) select p).Single(); } // Display second form model DialogResult result = addProductSupplierForm.ShowDialog(); using (TravelExpertDBDataContext db = new TravelExpertDBDataContext()) { // Update is successful if (result == DialogResult.OK) { // Refresh grid products_SupplierDataGridView.DataSource = db.Products_Suppliers; MessageBox.Show("Supplier Name Edited in Product Successfully"); } } }
// Save and validate data in Edit Product Form private void btnSaveProduct_Click(object sender, EventArgs e) { try { using (TravelExpertDBDataContext db = new TravelExpertDBDataContext()) { Product prod = new Product(); if (addProduct) { db.Products.InsertOnSubmit(prod); prod.ProdName = prodNameTextBox.Text; } else { // lambda expression is applied prod = db.Products.Single(o => o.ProductId == Convert.ToInt32(productIdTextBox.Text)); prod.ProdName = prodNameTextBox.Text; } //Validate product name // only need to validate if end date provided if (prod.ProdName == "") { // Changes are not submitted if data is obsolete MessageBox.Show("Product Name is required"); return; } db.SubmitChanges(); DialogResult = DialogResult.OK; } } // Catch concurrency exception 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()); } }
// Save and validate data in Edit Supplier Form private void btnSaveSupplier_Click(object sender, EventArgs e) { try { using (TravelExpertDBDataContext db = new TravelExpertDBDataContext()) { Supplier sup = new Supplier(); if (addSupplier) { db.Suppliers.InsertOnSubmit(sup); sup.SupName = supNameTextBox.Text; sup.SupplierId = Convert.ToInt32(supplierIdTextBox.Text); } else { // lambda expression is applied sup = db.Suppliers.Single(o => o.SupplierId == Convert.ToInt32(supplierIdTextBox.Text)); sup.SupName = supNameTextBox.Text; } //Validate supplier name if (sup.SupName == "") { // Do not submit changes if data is obsolete MessageBox.Show("Supplier Name is required"); return; } db.SubmitChanges(); DialogResult = DialogResult.OK; } } //Catch concurrency exception catch (ChangeConflictException) { MessageBox.Show("Another user changed or deleted the current record", "Concurrency Exception"); DialogResult = DialogResult.Retry; } catch (SqlException ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } }
//Add supplier private void btnAddSupplier_Click(object sender, EventArgs e) { frmAddEditSuppliers addSupplierForm = new frmAddEditSuppliers(); addSupplierForm.addSupplier = true; // Display second form model DialogResult result = addSupplierForm.ShowDialog(); using (TravelExpertDBDataContext db = new TravelExpertDBDataContext()) { // Update is successful if (result == DialogResult.OK) { // Refresh grid supplierDataGridView.DataSource = db.Suppliers; MessageBox.Show("New Supplier Added Successfully"); } } }
//Add package private void btnAddPackage_Click(object sender, EventArgs e) { frmAddEditPackages addPackageForm = new frmAddEditPackages(); addPackageForm.addPackage = true; // Display second form model DialogResult result = addPackageForm.ShowDialog(); using (TravelExpertDBDataContext db = new TravelExpertDBDataContext()) { // Update is successful if (result == DialogResult.OK) { // Refresh grid packageDataGridView.DataSource = db.Packages; MessageBox.Show("New Package Inserted Successfully"); //packages = db.Packages.ToList(); } } }
// Add products private void btnAddProduct_Click(object sender, EventArgs e) { frmAddEditProducts addProductForm = new frmAddEditProducts(); addProductForm.addProduct = true; // Display second form modal DialogResult result = addProductForm.ShowDialog(); using (TravelExpertDBDataContext db = new TravelExpertDBDataContext()) { // Update is successful if (result == DialogResult.OK) { // Refresh grid productDataGridView.DataSource = db.Products; products = db.Products.ToList(); MessageBox.Show("New Product Added Successfully"); } } }
//save product supplier private void btnSaveProductSupplier_Click(object sender, EventArgs e) { try { using (TravelExpertDBDataContext db = new TravelExpertDBDataContext()) { Products_Supplier pkg = new Products_Supplier(); if (addProductSupplier) { pkg.ProductId = Convert.ToInt32(productIdTextBox.Text); pkg.SupplierId = Convert.ToInt32(supplierIdTextBox.Text); db.Products_Suppliers.InsertOnSubmit(pkg); } else { // lambda expression is applied pkg = db.Products_Suppliers.Single(pk => pk.ProductSupplierId == Convert.ToInt32(textBox2.Text)); pkg.ProductId = Convert.ToInt32(productIdTextBox.Text); pkg.SupplierId = Convert.ToInt32(supplierIdTextBox.Text); } db.SubmitChanges(); DialogResult = DialogResult.OK; } } //Catch concurrency exception catch (ChangeConflictException) { MessageBox.Show("Another user changed or deleted the current record", "Concurrency Exception"); DialogResult = DialogResult.Retry; } catch (SqlException ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } }
//Add Supplier to product private void btnAddProductSupplier_Click(object sender, EventArgs e) { frmAddEditProductSuppliers addProductSupplierForm = new frmAddEditProductSuppliers(); addProductSupplierForm.addProductSupplier = true; // Display second form model DialogResult result = addProductSupplierForm.ShowDialog(); using (TravelExpertDBDataContext db = new TravelExpertDBDataContext()) { // Update is successful if (result == DialogResult.OK) { // Refresh grid Products_Supplier prosu = null; prosu = new Products_Supplier(); var prodsup = from ps in db.Products_Suppliers join sup in db.Suppliers on ps.SupplierId equals sup.SupplierId join p in db.Products on ps.ProductId equals p.ProductId into dbGroup select new { ProductSupplierID = ps.ProductSupplierId, ProductId = ps.Product.ProdName, SupplierId = sup.SupName, }; products_SupplierDataGridView.DataSource = prodsup.ToList(); MessageBox.Show("New Supplier Added to Product Successfully"); } } }
//Add Product to Package private void btnAddProductToPackage_Click(object sender, EventArgs e) { //int pack_id = Convert.ToInt32(cboPackageId.SelectedValue); int pkgrowNum = Convert.ToInt32(packageDataGridView.CurrentCell.RowIndex); // Column for package Id int pkgordernum = Convert.ToInt32(packageDataGridView["dataGridViewTextBoxColumn1", pkgrowNum].Value); using (TravelExpertDBDataContext db = new TravelExpertDBDataContext()) { Package curPack = (from p in db.Packages where p.PackageId == pkgordernum select p).Single(); frmAddProductToPackage secondform = new frmAddProductToPackage(); secondform.currentPackage = curPack; DialogResult result = secondform.ShowDialog(); if (result == DialogResult.OK)// successful Add { packageDataGridView.DataSource = db.Packages; MessageBox.Show("Product has been added to Package Successfully"); } } }
// Save and validate data in Add/Edit Package Form private void btnSavePackages_Click(object sender, EventArgs e) { if (IsValidData()) { try { using (TravelExpertDBDataContext db = new TravelExpertDBDataContext()) { Package pkg = new Package(); if (addPackage) { db.Packages.InsertOnSubmit(pkg); pkg.PkgName = pkgNameTextBox.Text; pkg.PkgAgencyCommission = Convert.ToDecimal(pkgAgencyCommissionTextBox.Text); pkg.PkgBasePrice = Convert.ToDecimal(pkgBasePriceTextBox.Text); pkg.PkgDesc = pkgDescTextBox.Text; pkg.PkgStartDate = Convert.ToDateTime(pkgStartDateDateTimePicker.Text); pkg.PkgEndDate = Convert.ToDateTime(pkgEndDateDateTimePicker.Text); } else { // lambda expression is applied pkg = db.Packages.Single(pk => pk.PackageId == Convert.ToInt32(packageIdTextBox.Text)); pkg.PkgName = pkgNameTextBox.Text; pkg.PkgAgencyCommission = Convert.ToDecimal(pkgAgencyCommissionTextBox.Text); pkg.PkgBasePrice = Convert.ToDecimal(pkgBasePriceTextBox.Text); pkg.PkgDesc = pkgDescTextBox.Text; pkg.PkgStartDate = Convert.ToDateTime(pkgStartDateDateTimePicker.Value); pkg.PkgEndDate = Convert.ToDateTime(pkgEndDateDateTimePicker.Value); } // Validate agency commision if (pkg.PkgAgencyCommission != null) { //Make sure that agency commission is less than base price if (pkg.PkgAgencyCommission > pkg.PkgBasePrice) { // Changes are not submitted if data is obsolete MessageBox.Show("Agency Commission must be less than Base Price", "Date Error"); return; } } // Validate start and end date if (pkg.PkgEndDate != null) { //validate that End date is after Start date if (pkg.PkgEndDate <= pkg.PkgStartDate) { // do not submit changes if data is obsolete MessageBox.Show("Package End Date must be later than Package Start Date", "Date Error"); return; } } db.SubmitChanges(); DialogResult = DialogResult.OK; } } //Catch concurrency exception catch (ChangeConflictException) { MessageBox.Show("Another user changed or deleted the current record", "Concurrency Exception"); DialogResult = DialogResult.Retry; } catch (SqlException ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } } }