//this will take from the textboxes and put it into the object private void PutPackageData(Package package) { try { if (Validator1.IsProvided(txtPkgName, "Package Name") && Validator1.IsProvided(txtPkgDesc, "Package Description") && Validator1.IsNonNegativeDecimal(txtPkgBasePrice, "Package Base Price") && Validator1.IsNonNegativeDecimal(txtPkgAgencyCommission, "Agency Commission")) { package.PkgName = txtPkgName.Text; if (dtStartDate.Value.Date >= dtEndDate.Value.Date) { MessageBox.Show("Package start date cannot be later than end date"); } else if (dtStartDate.Value.ToString() == "" || dtEndDate.Value.ToString() == "") { MessageBox.Show("Please enter start and end date"); } else if (Convert.ToDecimal(txtPkgBasePrice.Text) < Convert.ToDecimal(txtPkgAgencyCommission.Text)) { MessageBox.Show("Agency Comission cannot be more than Package Base price!"); } else { //Assigning all the given values to the package object package.PkgStartDate = dtStartDate.Value.ToString("yyyy-MM-dd"); package.PkgEndDate = dtEndDate.Value.ToString("yyyy-MM-dd"); package.PkgDesc = txtPkgDesc.Text; package.PkgBasePrice = Convert.ToDecimal(txtPkgBasePrice.Text); package.PkgAgencyCommission = Convert.ToDecimal(txtPkgAgencyCommission.Text); if (cboProdList.SelectedIndex < 00000000) { selectedProduct = null; product.ProductId = selectedProduct; } else { selectedProduct = (int)cboProdList.SelectedItem; product.ProductId = selectedProduct; } package.PackageId = PackageDB.AddNewPackage(package); this.DialogResult = DialogResult.OK; txtPackageId.Text = Convert.ToString(package.PackageId); MessageBox.Show("Package has been saved"); } } } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } }
//Saving the Data in database private void btnSave_Click(object sender, EventArgs e) { bool updated = false; //if Supplier button is clicked if (btnSupClicked) { //Validating Supplier Table if (Validator1.IsProvidedCombo(cBName, "Supplier Name")) { Supplier newSup = new Supplier(); newSup.SupName = cBName.Text; //for new supplier name updated = SuppliersDB.UpdateSupplier(newSup, SingleSup); if (updated) { MessageBox.Show("Supplier Updated"); } else { MessageBox.Show("Supplier not Updated. Please try again."); } } } //If Products button is clicked if (btnProdClicked) { //Validating Product Table if (Validator1.IsProvidedCombo(cBName, "Product Name")) { Product NewProd = new Product(); NewProd.ProdName = cBName.Text; //for new Product name updated = ProductsDB.UpdateProducts(NewProd, SingleProd); if (updated) { MessageBox.Show("Products Updated"); } else { MessageBox.Show("Products not Updated. Please try again"); } } } if (btnProdSupClicked)// this does not work yet { //Validating Product-Supplier table if (Validator1.IsProvidedCombo(cbProdID, "Product Id") && Validator1.IsProvidedCombo(cbSupID, "Supplier Id")) { ProdSupplier newProdSupplier = new ProdSupplier(); newProdSupplier.ProductId = Convert.ToInt32(cbProdID.SelectedValue); newProdSupplier.SupplierId = Convert.ToInt32(cbSupID.SelectedValue); updated = ProdSuppliersDB.UpdateProdSupplier(newProdSupplier, SingleProdSup); if (updated) { MessageBox.Show("Product_Supplier is Updated"); } else { MessageBox.Show("Product_Supplier is not Updated. Please try again"); } } } if (btnPackClicked) // Otherwise just load Packages { //Validating data for Packages if (Validator1.IsProvidedCombo(cBName, "Package Name") && Validator1.IsProvided(txtDesc, "Describtion") && Validator1.IsProvided(txtBasePrice, "Base Price") && Validator1.IsProvided(txtAgencyComm, "Agency Commission")) { Package NewPackage = new Package(); NewPackage.PkgName = cBName.Text; //Start Date cannnot be greater then End Date if (dTPStartDate.Value.Date >= dTPEndDate.Value.Date) { MessageBox.Show("Package start date cannot be later than end date"); } //Agency commission cannot be greater then base price else if (Convert.ToDecimal(txtBasePrice.Text.Replace("$", "")) < (Convert.ToDecimal(txtAgencyComm.Text.Replace("$", "")))) { MessageBox.Show("Agency Comission cannot be more than Package Base price!"); } else { NewPackage.PkgStartDate = dTPStartDate.Value.ToString("yyyy-MM-dd"); NewPackage.PkgEndDate = dTPEndDate.Value.ToString("yyyy-MM-dd"); NewPackage.PkgDesc = txtDesc.Text; NewPackage.PkgBasePrice = Convert.ToDecimal(txtBasePrice.Text.Replace("$", "")); NewPackage.PkgAgencyCommission = Convert.ToDecimal(txtAgencyComm.Text.Replace("$", "")); updated = PackageDB.UpdatePackage(NewPackage, SinglePkg); } if (updated) { MessageBox.Show("Package is Updated"); } else { MessageBox.Show("Package is not Updated, Please try again."); } } } }