private void btnDeletePackage_Click(object sender, EventArgs e) { int pkgID = Convert.ToInt32(lvPackages.Items[lvPackages.SelectedItems[0].Index].Text); DialogResult result = MessageBox.Show("Are you sure you want to delete this Product?", "Confirm Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (result == DialogResult.Yes) { bool exists = false; bool attempt = false; if (!exists) // if it does not exist, do this { try { // attempt = add product to package method, taking product supplier id and package id as arguments attempt = PackagesDB.DeletePackage(pkgID); //reload the packages list by getting new list and clearing the old one and adding new items to listview List <Packages> packages = PackagesDB.GetPackages(); lvPackages.Items.Clear(); int i = 0; foreach (Packages package in packages) { lvPackages.Items.Add(package.PackageID.ToString()); lvPackages.Items[i].SubItems.Add(package.PkgName.ToString()); lvPackages.Items[i].SubItems.Add(package.PkgStartDate.ToString()); lvPackages.Items[i].SubItems.Add(package.PkgEndDate.ToString()); lvPackages.Items[i].SubItems.Add(package.PkgDesc.ToString()); lvPackages.Items[i].SubItems.Add(package.PkgBasePrice.ToString()); lvPackages.Items[i].SubItems.Add(package.PkgAgencyCommission.ToString()); i += 1; } } catch (SqlException ex) { MessageBox.Show("Error deleting package\n" + ex); } if (attempt) { MessageBox.Show("Successfully deleted"); FrmPackages.ActiveForm.Refresh(); btnAddProd.Visible = false; lvPkgDetails.Items.Clear(); } } else { MessageBox.Show("Package is already deleted"); } } }
// add package button event private void btnAddPack_Click(object sender, EventArgs e) { bool exists = false; // validator checks input if its present, and a non negative decimal if (Validator.IsPresent(txtPkgName) && Validator.IsPresent(txtPkgDesc) && Validator.IsPresent(txtPkgBasePrice) && Validator.IsPresent(txtPkgAgencyComm) && Validator.NonNegativeDecimal(txtPkgBasePrice) && Validator.NonNegativeDecimal(txtPkgAgencyComm)) { // new packages list retrieved with getpackages method List <Packages> packages = PackagesDB.GetPackages(); foreach (Packages package in packages) { if (package.PkgName == txtPkgName.Text) { exists = true; } } if (!exists) // if it does not exist then execute this block to add package { pkg = new Packages(); this.PutPkgData(pkg); if (pkg.PkgBasePrice <= pkg.PkgAgencyCommission) { MessageBox.Show("Commission cannot be greater than Base price!"); return; } else { try { pkg.PackageID = PackagesDB.AddPackage(pkg); this.DialogResult = DialogResult.OK; } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } finally { this.Close(); } } } else { MessageBox.Show("A package of a similar name exists in database"); } } }
private void FrmPackages_Activated(object sender, EventArgs e) // when the form is activated (loaded or brought back to after adding) write data to list { List <Packages> packages = PackagesDB.GetPackages(); int i = 0; // start count at 0 lvPackages.Items.Clear(); foreach (Packages package in packages) { lvPackages.Items.Add(package.PackageID.ToString()); lvPackages.Items[i].SubItems.Add(package.PkgName.ToString()); lvPackages.Items[i].SubItems.Add(package.PkgStartDate.ToString()); lvPackages.Items[i].SubItems.Add(package.PkgEndDate.ToString()); lvPackages.Items[i].SubItems.Add(package.PkgDesc.ToString()); lvPackages.Items[i].SubItems.Add(package.PkgBasePrice.ToString("c")); lvPackages.Items[i].SubItems.Add(package.PkgAgencyCommission.ToString("c")); i += 1; } }