private void ModifyPackageBtn_Click(object sender, EventArgs e) { /**/ // get the key of the current package in the data grid view int rowNum = packagesDataGridView.CurrentCell.RowIndex; // index of the current row int packID = (int)packagesDataGridView["dataGridViewTextBoxColumn1", rowNum].Value; // Column for PackageID Package currentPackage; using (PackageDataContext dbContext = new PackageDataContext()) { currentPackage = (from p in dbContext.Packages where p.PackageId == packID select p).Single(); } AddModifyPackage secondForm = new AddModifyPackage(); secondForm.isAdd = false; // it Modify secondForm.currentPackage = currentPackage; DialogResult result = secondForm.ShowDialog(); // display second form modal if (result == DialogResult.OK || result == DialogResult.Retry) // successful update or concurrency exception { RefreshGridView(); } }
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 fullPackageDetailBtn_Click(object sender, EventArgs e) { int rowNum = packagesDataGridView.CurrentCell.RowIndex; int packageID = (int)packagesDataGridView["dataGridViewTextBoxColumn1", rowNum].Value; Package currentPackage; using (PackageDataContext dbContext = new PackageDataContext()) { currentPackage = (from p in dbContext.Packages where p.PackageId == packageID select p).Single(); } AddModifyPackage amf = new AddModifyPackage(); amf.ReadOnly = true; //apf.currentPackage = PackageList[rowNum]; amf.currentPackage = currentPackage; DialogResult result = amf.ShowDialog(); if (result == DialogResult.OK) { RefreshGridView(); } DialogResult = DialogResult.OK; }
private void loadSupplierComboBox() { try { using (PackageDataContext dbContext = new PackageDataContext()) { supplierComboBox.DataSource = dbContext.Suppliers; supplierComboBox.DisplayMember = "SupName"; supplierComboBox.ValueMember = "SupplierId"; supplierComboBox.SelectedIndex = -1; } } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } }
private void loadProductComboBox() { try { using (PackageDataContext dbContext = new PackageDataContext()) { productComboBox.DataSource = dbContext.Products; productComboBox.DisplayMember = "ProdName"; productComboBox.ValueMember = "ProductId"; productComboBox.SelectedIndex = -1; } } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } }
private void MainForm_Load(object sender, EventArgs e) { // TODO: This line of code loads data into the 'travelExpertsDataSet.Packages' table. You can move, or remove it, as needed. using (PackageDataContext dbContext = new PackageDataContext()) { packagesDataGridView.DataSource = dbContext.Packages; } packagesDataGridView.BorderStyle = BorderStyle.None; packagesDataGridView.AlternatingRowsDefaultCellStyle.BackColor = Color.FromArgb(238, 239, 249); packagesDataGridView.CellBorderStyle = DataGridViewCellBorderStyle.SingleHorizontal; packagesDataGridView.DefaultCellStyle.SelectionBackColor = Color.DarkTurquoise; packagesDataGridView.DefaultCellStyle.SelectionForeColor = Color.WhiteSmoke; packagesDataGridView.BackgroundColor = Color.White; packagesDataGridView.EnableHeadersVisualStyles = false; packagesDataGridView.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.None; packagesDataGridView.ColumnHeadersDefaultCellStyle.BackColor = Color.FromArgb(20, 25, 72); packagesDataGridView.ColumnHeadersDefaultCellStyle.ForeColor = Color.White; }
private bool IsUniqueID(TextBox packageIdTextBox) { Package pack = null; using (PackageDataContext dbContext = new PackageDataContext()) { pack = (from p in dbContext.Packages where p.PackageId == Convert.ToInt32(packageIdTextBox.Text.Trim()) select p).SingleOrDefault(); //dbContext.Products.Single(p => p.ProductCode == productCodeTextBox.Text); if (pack != null) // there is another product with same code { MessageBox.Show("Package ID must be unique", "Entry Error"); return(false); } else { return(true); } } }
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 RefreshGridView() { PackageDataContext dbContext = new PackageDataContext(); packagesDataGridView.DataSource = dbContext.Packages; }
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; }
private void DisplayCurrentPackage() { if (ReadOnly) { // display current Product data packageIdTextBox.Text = currentPackage.PackageId.ToString(); pkgNameTextBox.Text = currentPackage.PkgName; pkgDescTextBox.Text = currentPackage.PkgDesc.ToString(); pkgBasePriceTextBox.Text = currentPackage.PkgBasePrice.ToString(); if (currentPackage.PkgStartDate is null) { pkgStartDateDateTimePicker.Value = DateTime.Now; } else { pkgStartDateDateTimePicker.Value = (DateTime)currentPackage.PkgStartDate; } if (currentPackage.PkgEndDate is null) { pkgEndDateDateTimePicker.Value = DateTime.Now; } else { pkgEndDateDateTimePicker.Value = (DateTime)currentPackage.PkgEndDate; } pkgAgencyCommissionTextBox.Text = currentPackage.PkgAgencyCommission.ToString(); try { using (PackageDataContext dbContext = new PackageDataContext()) { var product_Suppliers = from pps in dbContext.Packages_Products_Suppliers join pk in dbContext.Packages on pps.PackageId equals pk.PackageId join ps in dbContext.Products_Suppliers on pps.ProductSupplierId equals ps.ProductSupplierId join s in dbContext.Suppliers on ps.SupplierId equals s.SupplierId join p in dbContext.Products on ps.ProductId equals p.ProductId where pk.PackageId == currentPackage.PackageId select new { s.SupName, p.ProdName }; foreach (var ps in product_Suppliers) { productSupplierListbox.Items.Add($"Product Name: {ps.ProdName}, Product Supplier: {ps.SupName}"); } } } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } // Read Only text box packageIdTextBox.Enabled = false; pkgNameTextBox.Enabled = false; pkgStartDateDateTimePicker.Enabled = false; pkgEndDateDateTimePicker.Enabled = false; pkgDescTextBox.Enabled = false; pkgBasePriceTextBox.Enabled = false; pkgAgencyCommissionTextBox.Enabled = false; productSupplierListbox.Enabled = false; CancelBtn.Visible = false; SavePackageBtn.Text = "OK"; productNameLabel.Visible = false; supplierNameLabel.Visible = false; productNameLabel.Visible = false; addsupplierButton.Visible = false; deleteSupplierButton.Visible = false; productComboBox.Visible = false; supplierComboBox.Visible = false; } else if (fullPackageModified) { // display current Product data packageIdTextBox.Text = currentPackage.PackageId.ToString(); pkgNameTextBox.Text = currentPackage.PkgName; pkgDescTextBox.Text = currentPackage.PkgDesc.ToString(); pkgBasePriceTextBox.Text = currentPackage.PkgBasePrice.ToString(); if (currentPackage.PkgStartDate is null) { pkgStartDateDateTimePicker.Value = DateTime.Now; } else { pkgStartDateDateTimePicker.Value = (DateTime)currentPackage.PkgStartDate; } if (currentPackage.PkgEndDate is null) { pkgEndDateDateTimePicker.Value = DateTime.Now; } else { pkgEndDateDateTimePicker.Value = (DateTime)currentPackage.PkgEndDate; } pkgAgencyCommissionTextBox.Text = currentPackage.PkgAgencyCommission.ToString(); try { using (PackageDataContext dbContext = new PackageDataContext()) { var Product_Suppliers = from pps in dbContext.Packages_Products_Suppliers join pk in dbContext.Packages on pps.PackageId equals pk.PackageId join ps in dbContext.Products_Suppliers on pps.ProductSupplierId equals ps.ProductSupplierId join s in dbContext.Suppliers on ps.SupplierId equals s.SupplierId join p in dbContext.Products on ps.ProductId equals p.ProductId where pk.PackageId == currentPackage.PackageId select new { pps.ProductSupplierId, s.SupplierId, s.SupName, p.ProductId, p.ProdName }; foreach (var ps in Product_Suppliers) { productSupplierListbox.Items.Add($"Product Name: {ps.ProdName}, Product Supplier: {ps.SupName}"); Products_Supplier newProduct_Supplier = new Products_Supplier(); newProduct_Supplier.ProductSupplierId = ps.ProductSupplierId; newProduct_Supplier.SupplierId = ps.SupplierId; newProduct_Supplier.ProductId = ps.ProductId; ProductSupplierList.Add(newProduct_Supplier); } } } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } // Read Only text box } else { // display current Product data packageIdTextBox.Text = currentPackage.PackageId.ToString(); pkgNameTextBox.Text = currentPackage.PkgName; pkgDescTextBox.Text = currentPackage.PkgDesc.ToString(); pkgBasePriceTextBox.Text = currentPackage.PkgBasePrice.ToString(); if (currentPackage.PkgStartDate is null) { pkgStartDateDateTimePicker.Value = DateTime.Now; } else { pkgStartDateDateTimePicker.Value = (DateTime)currentPackage.PkgStartDate; } if (currentPackage.PkgEndDate is null) { pkgEndDateDateTimePicker.Value = DateTime.Now; } else { pkgEndDateDateTimePicker.Value = (DateTime)currentPackage.PkgEndDate; } pkgAgencyCommissionTextBox.Text = currentPackage.PkgAgencyCommission.ToString(); productSupplierLabel.Visible = false; productNameLabel.Visible = false; supplierNameLabel.Visible = false; productNameLabel.Visible = false; productSupplierListbox.Visible = false; addsupplierButton.Visible = false; deleteSupplierButton.Visible = false; productComboBox.Visible = false; supplierComboBox.Visible = false; this.Size = new System.Drawing.Size(450, 400); } }