// create new package private void btnSaveNewPackage_Click(object sender, EventArgs e) { // check all input values if (IsValidDataForCreate()) { // check if package end date is greater than start date if (txtPkgStart2.Value > txtPkgEnd2.Value || txtPkgStart2.Value == txtPkgEnd2.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(txtPkgCommission2.Text) > Convert.ToDouble(txtPkgPrice2.Text)) { MessageBox.Show("Commission cannot be greater than the Base Price. Please enter a valid amount.", "Input Error"); } else { try { // set new values var test = newProductPackageBindingSource.DataSource; Package newPackage = new Package(); this.NewPackageData(newPackage); // save package int packageId = PackageDB.AddPackage(newPackage); List <ProductsInPackage> productsInPackages = (List <ProductsInPackage>)newProductPackageBindingSource.DataSource; foreach (var productsInPackage in productsInPackages) { Packages_products_suppliersDB.Add(packageId, productsInPackage.ProductSupplierId); } MessageBox.Show("New package added succesfully"); ClearControls(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } } } }
// 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); } } } } }