private void btnAdd_Click(object sender, EventArgs e) { if (lstProdSup.SelectedIndex < 0) // no selection { MessageBox.Show("Please select product supplier to add"); } else { Packages currentPackage = PackagesDB.GetPackageById((int)id); string i = lstProdSup.SelectedItem.ToString(); string[] s = i.Split('|'); int prodSupId = Int32.Parse(s[0].Trim()); Products_Suppliers selectedProductSupplier = Products_SuppliersDB.GetProductSupplierById(prodSupId); try { if (!Packages_Products_SuppliersDB.AddPackageProductSupplier(currentPackage.PackageId, selectedProductSupplier.ProductSupplierId)) { MessageBox.Show("Product added", "Success!"); this.DialogResult = DialogResult.OK; DisplayPackage(); } } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } } }
// sets up the form on load private void frmAddModifyPackages_Load(object sender, EventArgs e) { this.LoadProductComboBox(); // does the fiollowing if its a add if (add) { this.Text = "Add a Package"; lblTitle.Text = "Add Package Information"; cmbProductBName.SelectedIndex = -1; cmbSuppliers.DataSource = null; } //else does this for modify else { this.Text = "Modify a Package"; lblTitle.Text = "Modify Package Information"; cmbProductBName.SelectedIndex = -1; cmbSuppliers.DataSource = null; try { pack_prod_sup = Packages_Products_SuppliersDB.GetList(package.PackageId); this.DisplayPackages(); this.RedisplayList(); } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } } }
private void DeletePackage(Package pkg) { DialogResult result = MessageBox.Show("Delete the Package " + pkg.PkgName + "?", "Confirm Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (result == DialogResult.Yes) { // creates the other table objects List <Packages_Products_Suppliers> newpack_prod_sup = new List <Packages_Products_Suppliers>(); try // tries to delete the table records { // gets the data for the tasble records that need to be deleted newpack_prod_sup = Packages_Products_SuppliersDB.GetList(pkg.PackageId); // deletes each productsuppliers table record foreach (Packages_Products_Suppliers ps in newpack_prod_sup) { Packages_Products_SuppliersDB.DeletePackProdSup(ps); } // deletes the suppliercontacts record PackagesDB.DeletePackage(pkg); } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } RefreshPackages(); } }
// button the add the data to the DB private void btnAccept_Click(object sender, EventArgs e) { if (isValid()) { if (add) { package = this.createPackageObjects(); try { package.PackageId = PackagesDB.AddPackage(package); foreach (Packages_Products_Suppliers ps in pack_prod_sup) { ps.PackageId = package.PackageId; Packages_Products_SuppliersDB.AddPackProdSupp(ps); } this.DialogResult = DialogResult.OK; } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } } else { Package newPackage = new Package(); newPackage = this.createPackageObjects(); try { if (!PackagesDB.UpdatePackage(package, newPackage)) { MessageBox.Show("That Package has been updated or deleted already.", "Database Error"); this.DialogResult = DialogResult.Retry; } else { package = newPackage; this.DialogResult = DialogResult.OK; } } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } } } }
// button to delete a package from the DB private void btnDelete_Click(object sender, EventArgs e) { if (dgvPackages.SelectedRows.Count > 0) { // sets the object to delete int index = dgvPackages.SelectedRows[0].Index; package = packages[index]; // asks user if they are sure DialogResult result = MessageBox.Show("Delete the Package " + package.PkgName + "?", "Confirm Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (result == DialogResult.Yes) { // creates the other table objects List <Packages_Products_Suppliers> newpack_prod_sup = new List <Packages_Products_Suppliers>(); try // tries to delete the table records { // gets the data for the tasble records that need to be deleted newpack_prod_sup = Packages_Products_SuppliersDB.GetList(package.PackageId); // deletes each productsuppliers table record foreach (Packages_Products_Suppliers ps in newpack_prod_sup) { Packages_Products_SuppliersDB.DeletePackProdSup(ps); } // deltes the suppliercontacts record PackagesDB.DeletePackage(package); // deltes the supplier record // redisplays the dgv packages = PackagesDB.GetAll(); refreshDGV(); } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } } } else { MessageBox.Show("Please select a Product"); } }
private void btnDelete_Click(object sender, EventArgs e) { int index = lstPackProdSupp.SelectedIndex; string i = lstPackProdSupp.SelectedItem.ToString(); string[] s = i.Split('|'); int pid = Int32.Parse(s[0].Trim()); //Packages_Products_Suppliers currentPackage = Packages_Products_SuppliersDB.GetPackageIds if (pid < 1) // no selection { MessageBox.Show("Please select product supplier to delete"); } else // user selected a product to delete { if (type == "View") { Products_Suppliers pps = Products_SuppliersDB.GetProductSupplierById(pid); // selected product DialogResult answer = MessageBox.Show("Are you sure to delete " + pps.ProdName + "?", "Please Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (answer == DialogResult.Yes) { //delete selected package product supplier try { if (!Packages_Products_SuppliersDB.DeletePackageProductSupplier((int)id, pps.ProductSupplierId)) { MessageBox.Show("Another user has updated or deleted " + "that product.", "Database Error"); } else { currentProductSupplierIds.RemoveAt(index - 1); } } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } // remove from the current product supplier list DisplayCurrentPackageProductSupplierData(); } } } }
private void DisplayCurrentPackageProductSupplierData() { if (productSupplierIds != null) // if we have product suppliers to display { lstProductSupplierId.Items.Clear(); //start with empty list box lstProductSupplierId.Items.Add("Id " + ": " + "Product Name" + ", " + "Supplier Name"); foreach (int id in productSupplierIds) { Products_Suppliers ps = Products_SuppliersDB.GetProductSupplierById(id); lstProductSupplierId.Items.Add(ps); } } else // null this product does not exist - need to refresh combo box { packageIds = Packages_Products_SuppliersDB.GetPackageIds(); } }
private void DisplayPackage() { currentProductSupplierIds = Packages_Products_SuppliersDB.GetProductSupplierIds((int)id); Packages currentPackage = PackagesDB.GetPackageById((int)id); txtPackageId.Text = currentPackage.PackageId.ToString(); txtPkgName.Text = currentPackage.PkgName; txtPkgDesc.Text = currentPackage.PkgDesc; txtBasePrice.Text = currentPackage.PkgBasePrice.ToString(); DisplayCurrentPackageProductSupplierData(); if (currentPackage.PkgStartDate == null) { dtpStartDate.Text = ""; } else { DateTime startDate = (DateTime)currentPackage.PkgStartDate; dtpStartDate.Text = startDate.ToShortDateString(); } if (currentPackage.PkgEndDate == null) { dtpEndDate.Text = ""; } else { DateTime endDate = (DateTime)currentPackage.PkgEndDate; dtpEndDate.Text = endDate.ToShortDateString(); } if (currentPackage.PkgAgencyCommission.Equals(null)) { txtCommission.Text = ""; } else { txtCommission.Text = Convert.ToDecimal(currentPackage.PkgAgencyCommission).ToString(); } }
// removes a product supplier from the list private void btnDelete_Click(object sender, EventArgs e) { if (dgvProducts.SelectedRows.Count > 0) { if (add) { pack_prod_sup.RemoveAt(this.dgvProducts.SelectedRows[0].Index); RedisplayList(); } else { DialogResult result = MessageBox.Show("Are you sure you want to delete this Product in the Package?", "Confirm Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (result == DialogResult.Yes) { int index = this.dgvProducts.SelectedRows[0].Index; try { if (!Packages_Products_SuppliersDB.DeletePackProdSup(pack_prod_sup[index])) { MessageBox.Show("That product has been updated or deleted already.", "Database Error"); } else { pack_prod_sup = Packages_Products_SuppliersDB.GetList(package.PackageId); this.RedisplayList(); } } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } } } } else { MessageBox.Show("Please select a Product"); } }
// button to add a product supplier to the package private void btnProduct_Click(object sender, EventArgs e) { if (cmbProductBName.IsPresent()) { if (add) { Packages_Products_Suppliers product = new Packages_Products_Suppliers(); product.ProductSupplierId = (int)cmbSuppliers.SelectedValue; product.ProdName = cmbProductBName.Text; product.SupName = cmbSuppliers.Text; pack_prod_sup.Add(product); RedisplayList(); cmbProductBName.SelectedIndex = -1; cmbSuppliers.SelectedIndex = -1; } else { Packages_Products_Suppliers product = new Packages_Products_Suppliers(); product.ProductSupplierId = (int)cmbSuppliers.SelectedValue; product.ProdName = cmbProductBName.Text; product.SupName = cmbSuppliers.Text; product.PackageId = package.PackageId; try { Packages_Products_SuppliersDB.AddPackProdSupp(product); pack_prod_sup = Packages_Products_SuppliersDB.GetList(package.PackageId); } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } RedisplayList(); cmbProductBName.SelectedIndex = -1; } } }
private void btnDelete_Click(object sender, EventArgs e) { int index = lstPackProdSupp.SelectedIndex; // index of the selected product supplier if (index < 1) // no selection { MessageBox.Show("Please select product supplier to delete"); } else // user selected a product to delete { //Products_Suppliers pps = Products_SuppliersDB.GetProductSupplierById(currentProductSupplierIds[index-1]); // selected product DialogResult answer = MessageBox.Show("Are you sure to delete " + currentProductSupplierIds[index - 1] + "?", "Please Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (answer == DialogResult.Yes) { //delete selected package product supplier try { if (!Packages_Products_SuppliersDB.DeletePackageProductSupplier(package.PackageId, pps.ProductSupplierId)) { MessageBox.Show("Another user has updated or deleted " + "that product.", "Database Error"); } else { currentProductSupplierIds.RemoveAt(index - 1); } } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } // remove from the current product supplier list DisplayCurrentPackageProductSupplierData(); } } }
private void cboTableNames_SelectedIndexChanged(object sender, EventArgs e) { string tableName = cboTableNames.SelectedValue.ToString(); if (tableName == "Products") { pnlName.Visible = true; pnlName2.Visible = false; lblId.Text = "Product Id"; productIds = ProductsDB.GetProductsIds(); if (productIds.Count > 0) // if there are prodcuts { cboId.DataSource = productIds; cboId.SelectedIndex = 0; // triggers SelectedIndexChanged } else // no members { MessageBox.Show("There are no products. " + "Add some products in the database, and restart the application ", "Empty Load"); Application.Exit(); } DisplayProductData(); } else if (tableName == "Suppliers") { lblId.Text = "Supplier Id"; supplierIds = SuppliersDB.GetSuppliersIds(); pnlName.Visible = true; pnlName2.Visible = false; if (supplierIds.Count > 0) // if there are suppliers { cboId.DataSource = supplierIds; cboId.SelectedIndex = 0; // triggers SelectedIndexChanged } else // no members { MessageBox.Show("There are no suppliers. " + "Add some supplier in the database, and restart the application ", "Empty Load"); Application.Exit(); } DisplaySupplierData(); } else if (tableName == "Products_Suppliers") { lblId.Text = "ProductSupplier Id"; lblName.Text = "Product Name"; lblName2.Text = "Supplier Name"; pnlName.Visible = true; pnlName2.Visible = true; productSupplierIds = Products_SuppliersDB.GetProductSupplierIds(); if (productSupplierIds.Count > 0) // if there are suppliers { cboId.DataSource = productSupplierIds; cboId.SelectedIndex = 0; // triggers SelectedIndexChanged } else // no members { MessageBox.Show("There are no product_suppliers. " + "Add some product_supplier in the database, and restart the application ", "Empty Load"); Application.Exit(); } DisplayProductSupplierData(); } else if (tableName == "Packages_Products_Suppliers") { lblId.Text = "Package Id"; packageIds = Packages_Products_SuppliersDB.GetPackageIds(); pnlName.Visible = false; pnlName2.Visible = false; if (packageIds.Count > 0) // if there are packages { cboId.DataSource = packageIds; cboId.SelectedIndex = 0; // triggers SelectedIndexChanged } else // no members { MessageBox.Show("There are no packages with products_suppliers. " + "Add some packages with product_supplier in the database, and restart the application ", "Empty Load"); Application.Exit(); } } else if (tableName == "Packages") { lblId.Text = "Package Id"; pnlName.Visible = true; pnlName2.Visible = false; packageIds = PackagesDB.GetPackageIds(); DisplayPackages(); if (packageIds.Count > 0) // if there are suppliers { cboId.DataSource = packageIds; cboId.SelectedIndex = 0; // triggers SelectedIndexChanged } else // no members { MessageBox.Show("There are no packages. " + "Add some packages in the database, and restart the application ", "Empty Load"); Application.Exit(); } } }
private void btnUpdate_Click(object sender, EventArgs e) { string tableName = cboTableNames.SelectedValue.ToString(); if (tableName == "Products") { frmAddUpdateProducts updateProductForm = new frmAddUpdateProducts(); updateProductForm.addProduct = false; updateProductForm.product = currentProd; DialogResult result = updateProductForm.ShowDialog(); if (result == DialogResult.OK) { currentProd = updateProductForm.product; this.DisplayProductData(); } else if (result == DialogResult.Retry) { currentProd = ProductsDB.GetProductById(currentProd.ProductId); if (currentProd != null) { this.DisplayProductData(); } } } else if (tableName == "Suppliers") { frmAddUpdateSuppliers updateSupplierForm = new frmAddUpdateSuppliers(); updateSupplierForm.addSupplier = false; updateSupplierForm.supplier = currentSupplier; DialogResult result = updateSupplierForm.ShowDialog(); if (result == DialogResult.OK) { currentSupplier = updateSupplierForm.supplier; this.DisplaySupplierData(); } else if (result == DialogResult.Retry) { currentSupplier = SuppliersDB.GetSupplierById(currentSupplier.SupplierId); if (currentSupplier != null) { this.DisplaySupplierData(); } } } else if (tableName == "Products_Suppliers") { frmAddUpdateProductSupplier updateProductSupplierForm = new frmAddUpdateProductSupplier(); updateProductSupplierForm.addProductSupplier = false; updateProductSupplierForm.productSupplier = currentProductSupplier; DialogResult result = updateProductSupplierForm.ShowDialog(); if (result == DialogResult.OK) { this.DisplayCurrentProductSupplierData(); this.DisplayProductSupplierData(); } else if (result == DialogResult.Retry) { currentProductSupplier = Products_SuppliersDB.GetProductSupplierById(currentProductSupplier.ProductSupplierId); if (currentProductSupplier != null) { this.DisplayCurrentProductSupplierData(); } this.DisplayProductSupplierData(); } } else if (tableName == "Packages") { frmAddUpdatePackages updatePackageForm = new frmAddUpdatePackages(); updatePackageForm.addPackage = false; updatePackageForm.package = currentPackage; updatePackageForm.currentProductSupplierIds = Packages_Products_SuppliersDB.GetProductSupplierIds(currentPackage.PackageId); DialogResult result = updatePackageForm.ShowDialog(); if (result == DialogResult.OK) { this.DisplayPackages(); DisplayCurrentPackageProductSupplierData(); } else if (result == DialogResult.Retry) { currentPackage = PackagesDB.GetPackageById(currentPackage.PackageId); if (currentPackage != null) { DisplayCurrentPackageProductSupplierData(); } this.DisplayPackages(); } } }
private void cboId_SelectedIndexChanged(object sender, EventArgs e) { string tableName = cboTableNames.SelectedValue.ToString(); if (tableName == "Products") { int selectedID = (int)cboId.SelectedValue; lblName.Text = "Product Name"; try { currentProd = ProductsDB.GetProductById(selectedID); DisplayCurrentProductData(); } catch (Exception ex) { MessageBox.Show("Error while retrieving product with selected ID: " + ex.Message, ex.GetType().ToString()); } } else if (tableName == "Suppliers") { int selectedID = (int)cboId.SelectedValue; lblName.Text = "Supplier Name"; try { currentSupplier = SuppliersDB.GetSupplierById(selectedID); DisplayCurrentSupplierData(); } catch (Exception ex) { MessageBox.Show("Error while retrieving supplier with selected ID: " + ex.Message, ex.GetType().ToString()); } } else if (tableName == "Products_Suppliers") { int selectedID = (int)cboId.SelectedValue; try { currentProductSupplier = Products_SuppliersDB.GetProductSupplierById(selectedID); DisplayCurrentProductSupplierData(); } catch (Exception ex) { MessageBox.Show("Error while retrieving product supplier with selected ID: " + ex.Message, ex.GetType().ToString()); } } else if (tableName == "Packages_Products_Suppliers") { int selectedID = (int)cboId.SelectedValue; try { productSupplierIds = Packages_Products_SuppliersDB.GetProductSupplierIds(selectedID); DisplayCurrentPackageProductSupplierData(); } catch (Exception ex) { MessageBox.Show("Error while retrieving package product supplier with selected ID: " + ex.Message, ex.GetType().ToString()); } } else if (tableName == "Packages") { int selectedID = (int)cboId.SelectedValue; lblName.Text = "Package Name"; try { productSupplierIds = Packages_Products_SuppliersDB.GetProductSupplierIds(selectedID); DisplayCurrentPackageProductSupplierData(); } catch (Exception ex) { MessageBox.Show("Error while retrieving package product supplier with selected ID: " + ex.Message, ex.GetType().ToString()); } try { currentPackage = PackagesDB.GetPackageById(selectedID); txtName.Text = currentPackage.PkgName; } catch (Exception ex) { MessageBox.Show("Error while retrieving package with selected ID: " + ex.Message, ex.GetType().ToString()); } } }
private void button4_Click(object sender, EventArgs e) { if (string.IsNullOrWhiteSpace(textBox7.Text)) { MessageBox.Show("Please Enter a Package Name !", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } if (dateTimePicker1.Value >= dateTimePicker2.Value) { MessageBox.Show("Package End Date must be later than Package Start Date !", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } if (string.IsNullOrWhiteSpace(textBox9.Text)) { MessageBox.Show("Please Enter Package Description !", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } if (string.IsNullOrWhiteSpace(textBox11.Text)) { MessageBox.Show("Please Enter Base Price !", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } if (string.IsNullOrWhiteSpace(textBox10.Text)) { MessageBox.Show("Please Enter Agency Commission !", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } if (Convert.ToInt32(textBox10.Text) > Convert.ToInt32(textBox11.Text)) { MessageBox.Show("Agency Commission cannot be greater than the Base Price !", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } if (list3_ToDB.Count == 0) { MessageBox.Show("Please choose products for this package !", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } int newID = PackagesDB.AddPackageForForm2(textBox7.Text, dateTimePicker1.Value, dateTimePicker2.Value, textBox9.Text, Convert.ToDecimal(textBox11.Text.ToString()), Convert.ToDecimal(textBox10.Text.ToString())); if (newID == -1) { MessageBox.Show("Error adding Package."); } else { foreach (var ps in list3_ToDB) { int psid = Products_SuppliersDB.GetPSID_By_P_SID(ps.ProductId, ps.SupplierId); if (!Packages_Products_SuppliersDB.AddPPS(newID, psid)) { MessageBox.Show("Error adding Packages_Products_Suppliers."); } } } listBox3.DataSource = null; list3.Clear(); list3_ToDB.Clear(); textBox7.Text = ""; textBox9.Text = ""; textBox11.Text = ""; textBox10.Text = ""; }