private void addsupplierButton_Click(object sender, EventArgs e) { // make sure that a product is selected first before adding a product int productIndex = productComboBox.SelectedIndex; // make sure that a product is selected first before adding a product int SupplierIndex = supplierComboBox.SelectedIndex; // make sure that a course is selected if (productIndex == -1) { MessageBox.Show("Must select a product first"); return; } if (productIndex == -1) { MessageBox.Show("Must select a supplier first"); return; } { try { using (PackageDataContext dbContext = new PackageDataContext()) { Products_Supplier newProduct_Supplier = new Products_Supplier // create product supplier using provided data { ProductId = (int)productComboBox.SelectedValue, SupplierId = (int)supplierComboBox.SelectedValue }; // insert through data context object from the main form dbContext.Products_Suppliers.InsertOnSubmit(newProduct_Supplier); dbContext.SubmitChanges(); // submit to the database var newProduct_SupplierDB = (from ps in dbContext.Products_Suppliers orderby ps.ProductSupplierId descending select ps).FirstOrDefault(); newProduct_Supplier.ProductSupplierId = newProduct_SupplierDB.ProductSupplierId; ProductSupplierList.Add(newProduct_Supplier); var ProductName = (from p in dbContext.Products where p.ProductId == newProduct_Supplier.ProductId select p).FirstOrDefault(); var SupplierName = (from ps in dbContext.Suppliers where ps.SupplierId == newProduct_Supplier.SupplierId select ps).FirstOrDefault(); productSupplierListbox.Items.Add($"Product Name: {ProductName.ProdName}, " + $"Product Supplier: {SupplierName.SupName}"); productComboBox.SelectedIndex = -1; supplierComboBox.SelectedIndex = -1; } } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); return; } } }
private void deleteSupplierButton_Click(object sender, EventArgs e) { // make sure that someon is selected before displaying the information int index = productSupplierListbox.SelectedIndex; // make sure that a course is selected if (index == -1) { MessageBox.Show("Must select a course first"); return; } // issue a a confirmation dialog DialogResult result1 = MessageBox.Show($"Do you wish to delete " + $"{ProductSupplierList[index]} course?", "Confirmation Message", MessageBoxButtons.YesNo); if (result1 == DialogResult.Yes) { // remove the item from the database and //remove selected item from both the display the display list box // and the backiging store list also using (PackageDataContext dbContext = new PackageDataContext()) { try //use try and linq to delete the only select one. { Products_Supplier currentPS = (from p in dbContext.Products_Suppliers where p.ProductSupplierId == ProductSupplierList[index].ProductSupplierId select p).Single(); dbContext.Products_Suppliers.DeleteOnSubmit(currentPS); dbContext.SubmitChanges(); } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } } ProductSupplierList.RemoveAt(productSupplierListbox.SelectedIndex); productSupplierListbox.Items.RemoveAt(productSupplierListbox.SelectedIndex); } }
private void DeletePackageBtn_Click(object sender, EventArgs e) { int rowNum = packagesDataGridView.CurrentCell.RowIndex; int packageID = (int)packagesDataGridView["dataGridViewTextBoxColumn1", rowNum].Value; DialogResult answer = MessageBox.Show("Are you sure you want to delete " + packageID + "?", "Confirm", MessageBoxButtons.OKCancel); if (answer == DialogResult.OK) { using (PackageDataContext dbContext = new PackageDataContext()) { try //use try and linq to delete the only select one. { Package currentPackage = (from p in dbContext.Packages where p.PackageId == packageID select p).Single(); List <Booking> currentBookings = (from p in dbContext.Bookings where p.PackageId == packageID select p).ToList(); foreach (Booking bk in currentBookings) { dbContext.Bookings.DeleteOnSubmit(bk); dbContext.SubmitChanges(); } List <Packages_Products_Supplier> currentPPS = (from p in dbContext.Packages_Products_Suppliers where p.PackageId == packageID select p).ToList(); foreach (Packages_Products_Supplier pps in currentPPS) { dbContext.Packages_Products_Suppliers.DeleteOnSubmit(pps); dbContext.SubmitChanges(); } //dbContext.Packages_Products_Suppliers.DeleteOnSubmit(currentPPS); //dbContext.SubmitChanges(); dbContext.Packages.DeleteOnSubmit(currentPackage); dbContext.SubmitChanges(); RefreshGridView(); } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } } } //int rowNum = packagesDataGridView.CurrentCell.RowIndex; // index of the current row //int packID = (int)(packagesDataGridView["dataGridViewTextBoxColumn1", rowNum].Value); // Column for PackageID //DialogResult answer = MessageBox.Show("Are you sure you want to delete " + packID + "?", "Confirm", MessageBoxButtons.OKCancel); //if (answer == DialogResult.OK) //{ // using (PackageDataContext dbContext = new PackageDataContext()) // { // try //use try and linq to delete the only select one. // { // Package currentPackage = (from p in dbContext.Packages // where p.PackageId == packID // select p).Single(); // dbContext.Packages.DeleteOnSubmit(currentPackage); // dbContext.SubmitChanges(); // //refresh gridview // RefreshGridView(); // } // catch (Exception ex) // { // MessageBox.Show(ex.Message, ex.GetType().ToString()); // } // } //} }
private void SavePackageBtn_Click(object sender, EventArgs e) { if (isAdd) { if (// coded below in this form Validator.IsPresent(pkgNameTextBox) && Validator.IsCorrectLength(pkgNameTextBox, 50) && Validator.IsPresent(pkgDescTextBox) && Validator.IsCorrectLength(pkgDescTextBox, 200) && Validator.IsPresent(pkgBasePriceTextBox) && Validator.IsDecimal(pkgBasePriceTextBox) && Validator.IsNonNegativeDecimal(pkgBasePriceTextBox) && Validator.IsNotTooEarly(pkgStartDateDateTimePicker) && Validator.IsNotTooEarly(pkgEndDateDateTimePicker) ) // replace with data validation: all fields provided, code unique, // base price and Commission appropriate numeric type and non-negative // start date and end date later than today { nullable(); Package newPackage = new Package // create package using provided data { //PackageId = Convert.ToInt32(packageIdTextBox.Text.Trim()), PkgName = pkgNameTextBox.Text.Trim(), PkgDesc = pkgDescTextBox.Text.Trim(), PkgBasePrice = Convert.ToDecimal(pkgBasePriceTextBox.Text.Trim()), PkgStartDate = starttime, PkgEndDate = endtime, PkgAgencyCommission = commission, ImageFile = pkgNameTextBox.Text.Trim() + ".jpg" }; using (PackageDataContext dbContext = new PackageDataContext()) { // insert through data context object from the main form dbContext.Packages.InsertOnSubmit(newPackage); dbContext.SubmitChanges(); // submit to the database } DialogResult = DialogResult.OK; } else // validation failed { return; } } else if (isAddFullPackage) { if (// coded below in this form Validator.IsPresent(pkgNameTextBox) && Validator.IsCorrectLength(pkgNameTextBox, 50) && Validator.IsPresent(pkgDescTextBox) && Validator.IsCorrectLength(pkgDescTextBox, 200) && Validator.IsPresent(pkgBasePriceTextBox) && Validator.IsDecimal(pkgBasePriceTextBox) && Validator.IsNonNegativeDecimal(pkgBasePriceTextBox) && Validator.IsNotTooEarly(pkgStartDateDateTimePicker) && Validator.IsNotTooEarly(pkgEndDateDateTimePicker) ) // replace with data validation: all fields provided, code unique, // base price and Commission appropriate numeric type and non-negative // start date and end date later than today { try { using (PackageDataContext dbContext = new PackageDataContext()) { nullable(); Package newPackage = new Package // create package using provided data { //PackageId = Convert.ToInt32(packageIdTextBox.Text.Trim()), PkgName = pkgNameTextBox.Text.Trim(), PkgDesc = pkgDescTextBox.Text.Trim(), PkgBasePrice = Convert.ToDecimal(pkgBasePriceTextBox.Text.Trim()), PkgStartDate = starttime, PkgEndDate = endtime, PkgAgencyCommission = commission, ImageFile = pkgNameTextBox.Text.Trim() + ".jpg" }; dbContext.Packages.InsertOnSubmit(newPackage); dbContext.SubmitChanges(); // submit to the database foreach (Products_Supplier ps in ProductSupplierList) { Packages_Products_Supplier newPackage_Product_Supplier = new Packages_Products_Supplier // create product supplier using provided data { PackageId = newPackage.PackageId, ProductSupplierId = ps.ProductSupplierId }; // insert through data context object from the main form dbContext.Packages_Products_Suppliers.InsertOnSubmit(newPackage_Product_Supplier); dbContext.SubmitChanges(); // submit to the database } ProductSupplierList.Clear(); } } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); return; } } } else if (fullPackageModified)// it is Modify { nullable(); if ( Validator.IsPresent(pkgNameTextBox) && Validator.IsCorrectLength(pkgNameTextBox, 50) && Validator.IsPresent(pkgDescTextBox) && Validator.IsCorrectLength(pkgDescTextBox, 200) && Validator.IsPresent(pkgBasePriceTextBox) && Validator.IsDecimal(pkgBasePriceTextBox) && Validator.IsNonNegativeDecimal(pkgBasePriceTextBox) && Validator.IsNotTooEarly(pkgStartDateDateTimePicker) && Validator.IsNotTooEarly(pkgEndDateDateTimePicker) ) { try { using (PackageDataContext dbContext = new PackageDataContext()) { // get the Package with Code from the current text box Package pack = dbContext.Packages.Single(p => p.PackageId == Convert.ToInt32(packageIdTextBox.Text.Trim())); //Product prod = dbContext.Products.Single(p => p.ProdName == prodNameComboBox.Text.Trim()); //Supplier supp = dbContext.Suppliers.Single(s => s.SupName == supNameComboBox.Text.Trim()); //Products_Supplier proSup = dbContext.Products_Suppliers.Single(p => p.ProductId == prod[""]&&) if (pack != null) { // make changes by copying values from text boxes pack.PkgName = pkgNameTextBox.Text.Trim(); pack.PkgDesc = pkgDescTextBox.Text.Trim(); pack.PkgBasePrice = Convert.ToDecimal(pkgBasePriceTextBox.Text.Trim()); pack.PkgStartDate = starttime; pack.PkgEndDate = endtime; pack.PkgAgencyCommission = commission; pack.ImageFile = pkgNameTextBox.Text.Trim() + ".jpg"; //pack.PkgAgencyCommission = Convert.ToDecimal(pkgAgencyCommissionTextBox.Text.Trim()); // submit changes dbContext.SubmitChanges(); DialogResult = DialogResult.OK; int currentPackageID = pack.PackageId; List <Packages_Products_Supplier> newProduct_SupplierDB = (from ps in dbContext.Packages_Products_Suppliers where ps.PackageId == pack.PackageId select ps).ToList(); List <int> currentPSList = new List <int>(); foreach (Packages_Products_Supplier pps in newProduct_SupplierDB) { currentPSList.Add(pps.ProductSupplierId); } foreach (Products_Supplier ps in ProductSupplierList) { if (!currentPSList.Contains(ps.ProductSupplierId)) { Packages_Products_Supplier newPackage_Product_Supplier = new Packages_Products_Supplier // create product supplier using provided data { PackageId = pack.PackageId, ProductSupplierId = ps.ProductSupplierId }; // insert through data context object from the main form dbContext.Packages_Products_Suppliers.InsertOnSubmit(newPackage_Product_Supplier); dbContext.SubmitChanges(); // submit to the database } } ProductSupplierList.Clear(); } } } 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; return; } } else // it is Modify { nullable(); if ( Validator.IsPresent(pkgNameTextBox) && Validator.IsCorrectLength(pkgNameTextBox, 50) && Validator.IsPresent(pkgDescTextBox) && Validator.IsCorrectLength(pkgDescTextBox, 200) && Validator.IsPresent(pkgBasePriceTextBox) && Validator.IsDecimal(pkgBasePriceTextBox) && Validator.IsNonNegativeDecimal(pkgBasePriceTextBox) && Validator.IsNotTooEarly(pkgStartDateDateTimePicker) && Validator.IsNotTooEarly(pkgEndDateDateTimePicker) ) { try { using (PackageDataContext dbContext = new PackageDataContext()) { // get the Package with Code from the current text box Package pack = dbContext.Packages.Single(p => p.PackageId == Convert.ToInt32(packageIdTextBox.Text.Trim())); //Product prod = dbContext.Products.Single(p => p.ProdName == prodNameComboBox.Text.Trim()); //Supplier supp = dbContext.Suppliers.Single(s => s.SupName == supNameComboBox.Text.Trim()); //Products_Supplier proSup = dbContext.Products_Suppliers.Single(p => p.ProductId == prod[""]&&) if (pack != null) { // make changes by copying values from text boxes pack.PkgName = pkgNameTextBox.Text.Trim(); pack.PkgDesc = pkgDescTextBox.Text.Trim(); pack.PkgBasePrice = Convert.ToDecimal(pkgBasePriceTextBox.Text.Trim()); pack.PkgStartDate = starttime; pack.PkgEndDate = endtime; pack.PkgAgencyCommission = commission; pack.ImageFile = pkgNameTextBox.Text.Trim() + ".jpg"; //pack.PkgAgencyCommission = Convert.ToDecimal(pkgAgencyCommissionTextBox.Text.Trim()); // 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; return; } } DialogResult = DialogResult.OK; }