/// <summary> /// Update accepted /// </summary> private void btnAccept_Click(object sender, EventArgs e) { newPkg = Validation(); if (newPkg == null) { return; } try { int success = PackageDB.UpdatePackage(oldPkg, newPkg); if (success == 1) { this.DialogResult = DialogResult.OK; } else if (success == 0) // there was a concurrency issue { this.DialogResult = DialogResult.Abort; } else { this.DialogResult = DialogResult.Cancel; } } catch (Exception ex) { MessageBox.Show("Error while editing package.\n" + ex.Message, ex.GetType().ToString()); } }
private void btnAccept_Click(object sender, EventArgs e) { bool success = PackageDB.UpdatePackage(oldPack, newPack); if (success) { this.DialogResult = DialogResult.OK; ManagementForm frm = new ManagementForm(); frm.Refresh(); } else { this.DialogResult = DialogResult.Retry; } }
//*****************SAVE BUTTON(Update and Add)********** private void btnSave_Click(object sender, EventArgs e) { ////Adding if (addPackage) { packageNow = new Packages(); this.PackageData(packageNow); //if (txtPkgName.Text == packageNow.PkgName) //{ // MessageBox.Show("There is already a existing package with this name, please try again"); // this.DialogResult = DialogResult.Retry; //} //else if (txtPkgName.Text != packageNow.PkgName) //only ELSE was giving the error message even if it wasn't equal(duplicate) //{ try { packageNow.PackgeId = PackageDB.AddPackage(packageNow); this.DialogResult = DialogResult.OK; // OK if Insert was successful } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); this.DialogResult = DialogResult.Retry; } //} } ////Update else { Packages packageNow = new Packages(); this.PackageData(packageNow); try { bool success = PackageDB.UpdatePackage(packageOld, packageNow); if (success) { this.DialogResult = DialogResult.OK; } } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); this.DialogResult = DialogResult.Retry; } } }
/// <summary> /// Katrina: Saves changes to add or modify /// Katrina: Uses validation to check for valid dates, present strings, and number values /// </summary> private void PkgSaveButton_Click(object sender, EventArgs e) { // check if valid before accepting changes -Katrina if (IsValidDates() && IsValidPkgData() && IsValidPrices()) { Console.WriteLine("Dates are valid"); if (addPackage) // if this is Add -Katrina { this.PutPackageData(); try { package.PackageId = PackageDB.AddPackage(package); this.DialogResult = DialogResult.OK; } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } } else // this is Modify -Katrina { // build Package object with the new data -Katrina // Package newPkg = new Package(); package.PackageId = currentPkg.PackageId; this.PutPackageData(); try // try to update -Katrina { if (!PackageDB.UpdatePackage(currentPkg, package)) // failed -Katrina { MessageBox.Show("Another user has updated or deleted current package", "Concurrency Error"); this.DialogResult = DialogResult.Retry; } else { currentPkg = package; this.DialogResult = DialogResult.OK; } } catch (Exception ex) { MessageBox.Show("Error while updating: " + ex.Message, ex.GetType().ToString()); } } } }
// Accept button to accept add or modify function private void btnAccept_Click(object sender, EventArgs e) { if (IsValidData()) { if (addPackage) { package = new Package(); this.PutPackageData(package); try { package.PackageId = PackageDB.AddPackage(package); this.DialogResult = DialogResult.OK; } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } } else // modify { Package newPackage = new Package(); newPackage.PackageId = package.PackageId; this.PutPackageData(newPackage); try { if (!PackageDB.UpdatePackage(package, newPackage)) { MessageBox.Show("Another user has updated or " + "deleted that package.", "Database Error"); this.DialogResult = DialogResult.Retry; } else // successfully updated { package = newPackage; this.DialogResult = DialogResult.OK; } } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } } } // end of validation } // end of accept button
private void btnSave_Click(object sender, EventArgs e) { if (InputIsNotNull() && CommissionIsValid() && EndDateIsValid()) { // build Package object with the new data Package updated = new Package(); updated.PackageId = selected.PackageId; updated.PkgName = pkgNameTextBox.Text; updated.PkgStartDate = DateTime.Parse(pkgStartDateDateTimePicker.Text); updated.PkgEndDate = DateTime.Parse(pkgEndDateDateTimePicker.Text); updated.PkgDesc = pkgDescTextBox.Text; updated.PkgBasePrice = decimal.Parse(pkgBasePriceTextBox.Text, NumberStyles.Currency); updated.PkgAgencyCommission = decimal.Parse(pkgAgencyCommissionTextBox.Text, NumberStyles.Currency); try // try to update { if (!PackageDB.UpdatePackage(selected, updated)) // DB concurrency error { MessageBox.Show("Another user has updated or " + "deleted current member", "Concurrency Error"); this.DialogResult = DialogResult.Retry; } else { selected = updated; this.DialogResult = DialogResult.OK; } } catch (Exception ex) { MessageBox.Show("Error while updating: " + ex.Message, ex.GetType().ToString()); } } }
private void btnUpdate_Click(object sender, EventArgs e) { string pkgName; decimal basePrice; int productId; int supplierId; if (Validator.IsPresent(pkgNameTextBox)) { pkgName = pkgNameTextBox.Text; // If Package Name already exists, get package object from database bool ValidPrice = true; bool DateValid = true; if (Validator.IsPresent(pkgBasePriceTextBox)) { basePrice = Convert.ToDecimal(pkgBasePriceTextBox.Text); SelectedPackage.PkgBasePrice = basePrice; SelectedPackage.PkgName = pkgName; if (string.IsNullOrWhiteSpace(pkgAgencyCommissionTextBox.Text)) { SelectedPackage.PkgAgencyCommission = null; } else { SelectedPackage.PkgAgencyCommission = Convert.ToDecimal(pkgAgencyCommissionTextBox.Text); } if (string.IsNullOrWhiteSpace(pkgDescTextBox.Text)) { SelectedPackage.PkgDesc = null; } else { SelectedPackage.PkgDesc = pkgDescTextBox.Text; } if (pkgStartDateDateTimePicker.Checked == false && pkgEndDateDateTimePicker.Checked == false) { SelectedPackage.PkgStartDate = null; SelectedPackage.PkgEndDate = null; } else if (pkgStartDateDateTimePicker.Checked == true && pkgEndDateDateTimePicker.Checked == false) { // Package End Date has not been provided yet SelectedPackage.PkgStartDate = pkgStartDateDateTimePicker.Value.Date; SelectedPackage.PkgEndDate = null; } else if (pkgStartDateDateTimePicker.Checked == false && pkgEndDateDateTimePicker.Checked == true) { MessageBox.Show("Must Provide a Valid Start Date"); DateValid = false; DialogResult = DialogResult.None; } else { if (DateTime.Compare(pkgStartDateDateTimePicker.Value, pkgEndDateDateTimePicker.Value) >= 0) { MessageBox.Show("Start Date must be earlier than End Date"); DateValid = false; DialogResult = DialogResult.None; } else { SelectedPackage.PkgStartDate = pkgStartDateDateTimePicker.Value.Date; SelectedPackage.PkgEndDate = pkgEndDateDateTimePicker.Value.Date; } } if (DateValid) { // Update package in database try { if (!PackageDB.UpdatePackage(OldPackage, SelectedPackage)) { MessageBox.Show("Another user has updated or " + "deleted that Package.", "Database Error"); DialogResult = DialogResult.Retry; PkgUpdated = false; } else { PkgUpdated = true; } } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } } } else { ValidPrice = false; DialogResult = DialogResult.None; } if (ValidPrice && DateValid && PkgUpdated) { productId = (int)prodNameComboBox.SelectedValue; supplierId = (int)supNameComboBox.SelectedValue; // Check if the combination of productId and supplierId already exists in database if (ProductsSupplierDB.GetProductsSupplierByProductIdAndSupplierId(productId, supplierId) == null) { // if doesn't exist in database, insert a new record ProductsSupplier prodSupps = new ProductsSupplier { ProductId = productId, SupplierId = supplierId }; // Insert into database int prodSuppId = ProductsSupplierDB.AddProductsSupplier(prodSupps); prodSupps.ProductSupplierId = prodSuppId; SelectedProductsSupplier = prodSupps; ProdSuppInserted = true; } else { SelectedProductsSupplier = ProductsSupplierDB.GetProductsSupplierByProductIdAndSupplierId(productId, supplierId); ProdSuppInserted = false; } // Update Packages_Products_Supplier table try { // If there already exists a same packageId and ProductSupplierId combination var pkgProdSuppTable = PkgProdSupps.SingleOrDefault(p => p.PackageId == SelectedPackage.PackageId && p.ProductSupplierId == SelectedProductsSupplier.ProductSupplierId); if (pkgProdSuppTable != null) { MessageBox.Show("Package: " + SelectedPackage.PkgName + " with \n" + "Product Name: " + prodNameComboBox.Text + "\nSupplier Name: " + supNameComboBox.Text + " \nalready exists", "Record Exists"); DialogResult = DialogResult.None; } else { // Verify against database PackagesProductsSuppliers pkgPS = PackagesProductsSuppliersDB.GetPackagesProductsSuppliersByPkgIdAndProductSupplierId(SelectedPackage.PackageId, SelectedProductsSupplier.ProductSupplierId); if (pkgPS == null) { SelectedPkgProdSupp.ProductSupplierId = SelectedProductsSupplier.ProductSupplierId; if (!PackagesProductsSuppliersDB.UpdatePackagesProductsSuppliers(OldPkgProdSupp, SelectedPkgProdSupp)) { MessageBox.Show("Another user has updated or " + "deleted that Package.", "Database Error"); DialogResult = DialogResult.Retry; } else { DialogResult = DialogResult.OK; } } else { MessageBox.Show("Package: " + SelectedPackage.PkgName + " with \n" + "Product Name: " + prodNameComboBox.Text + "\nSupplier Name: " + supNameComboBox.Text + " \nalready exists", "Record Exists"); SelectedPkgProdSupp = pkgPS; DialogResult = DialogResult.OK; } } } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } } else { DialogResult = DialogResult.None; } } else { DialogResult = DialogResult.None; } }
// update Package private void btnUpdate_Click(object sender, EventArgs e) { // check all input values if (IsValidData()) { // check if package end date is greater than start date if (txtPkgStart.Value > txtPkgEnd.Value || txtPkgStart.Value == txtPkgEnd.Value) { MessageBox.Show("End Date should be greater than Start Date. Please choose a valid date.", "Input Error"); } else { // check if price is greater than commission if (Convert.ToDouble(txtPkgCommission.Text) > Convert.ToDouble(txtPkgPrice.Text)) { MessageBox.Show("Commission cannot be greater than the Base Price. Please enter a valid amount.", "Input Error"); } else { // get current package from database if (cmbPackageId.Text == "") { return; } int packageId = Convert.ToInt32(cmbPackageId.Text); List <Package> oldPackageList = PackageDB.GetPackages(packageId); Package oldPackage = oldPackageList.First(); // set new values Package newPackage = new Package(); newPackage.PackageId = packageId; this.PutPackageData(newPackage); // save package try { package = newPackage; PackageDB.UpdatePackage(oldPackage, newPackage); // get current product suppliers List <int> productSupplierIds = new List <int>(); foreach (var product in ProductsInPackageDB.GetProductsFromPackage(packageId)) { productSupplierIds.Add(product.ProductSupplierId); } // delete products suppliers linked to package Packages_products_suppliersDB.Delete(packageId); // add products supliers to package List <ProductsInPackage> productsInPackages = (List <ProductsInPackage>)productsInPackageBindingSource.DataSource; foreach (var productsInPackage in productsInPackages) { Packages_products_suppliersDB.Add(packageId, productsInPackage.ProductSupplierId); } MessageBox.Show("Changes saved for Package ID " + packageId); tabPackageList.SelectTab(0); // go back to list view } catch (Exception ex) { MessageBox.Show(ex.Message); } } } } }