private void btnDeletePackage_Click(object sender, EventArgs e) { try { //select row var id = Convert.ToString(gvPackageList.CurrentRow.Cells[0].Value); //query database for record var package = _db.Packages.FirstOrDefault(p => p.PkgName == id); DialogResult dr = MessageBox.Show("Are You Sure You Want To Delete This Package?", "Delete", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning); if (dr == DialogResult.Yes) { //delete Product from table _db.Packages.Remove(package); _db.SaveChanges(); } PopulateGrid(); } catch (Exception ex) { MessageBox.Show($"Error: {ex.Message}"); } //gvPackageList.Refresh(); }
//button to delete a supplier private void btnDeleteSupplier_Click(object sender, EventArgs e) { try { //select row var id = Convert.ToString(gvSupplierList.CurrentRow.Cells[0].Value); //query database for record var supplier = _db.Suppliers.FirstOrDefault(s => s.SupName == id); DialogResult dr = MessageBox.Show("Are You Sure You Want To Delete This Supplier?", "Delete", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning); if (dr == DialogResult.Yes) { //delete Supplier from table _db.Suppliers.Remove(supplier); _db.SaveChanges(); } PopulateGrid(); } catch (Exception ex) { MessageBox.Show($"Error: {ex.Message}"); } }
private void btnSave_Click(object sender, EventArgs e) { //Validation on Save button click try { string supplier = txtSupplier.Text; var isValid = true; var errorMessage = ""; if (string.IsNullOrWhiteSpace(supplier)) { isValid = false; errorMessage += "Error: Please Enter a Supplier Name"; } if (isValid) { //Declare an object of the supplier to be added var supplierlisting = new Supplier(); if (isEditMode) { //If in edit mode, then get the ID and retrieve the record from the database and place //the result in the record object var id = int.Parse(lblSupplierId.Text); supplierlisting = _db.Suppliers.FirstOrDefault(s => s.SupplierId == id); } //Populate record object with values from the form supplierlisting.SupName = supplier; //If not in edit mode, then add the record object to the database if (!isEditMode) { _db.Suppliers.Add(supplierlisting); } //Save Changes made to the entity _db.SaveChanges(); _manageSupplierListing.PopulateGrid(); MessageBox.Show($"Supplier Name :{supplier} has been added"); Close(); } else { MessageBox.Show(errorMessage); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void btnSave_Click(object sender, EventArgs e) { try { string package = txtPackage.Text; var isValid = true; var errorMessage = ""; if (string.IsNullOrWhiteSpace(package)) { isValid = false; errorMessage += "Error: Please Enter a Package Name"; } if (isValid) { //Declare an object of the product to be added var packagelisting = new Package(); if (isEditMode) { //If in edit mode, then get the ID and retrieve the record from the database and place //the result in the record object var id = int.Parse(lblPackageId.Text); packagelisting = _db.Packages.FirstOrDefault(p => p.PackageId == id); } //Populate record object with values from the form packagelisting.PkgName = package; //If not in edit mode, then add the record object to the database if (!isEditMode) { _db.Packages.Add(packagelisting); } //Save Changes made to the entity _db.SaveChanges(); _managePackagesListing.PopulateGrid(); MessageBox.Show($"Package Name :{package} has been added"); Close(); } else { MessageBox.Show(errorMessage); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
//Delete Package private void btnDeletePackage_Click(object sender, EventArgs e) { if (MessageBox.Show("Delete Package?", "Delete Action", MessageBoxButtons.YesNo) == DialogResult.Yes) { try { var packageId = (int)dgvPackages.CurrentRow.Cells[0].Value; var package = _db.Packages.Where(p => p.PackageId == packageId).FirstOrDefault(); _db.Packages.Remove(package); _db.SaveChanges(); MessageBox.Show("Package Deleted!"); PopulateGrid(); } catch (Exception ex) { MessageBox.Show($"Error: {ex.Message}"); } } }
private void btnSaveChanges_Click(object sender, EventArgs e) { //Validations for Packages when Save Button is clicked try { if (string.IsNullOrWhiteSpace(txtPackageName.Text)) { MessageBox.Show("Package name can't be empty!"); return; } if (string.IsNullOrWhiteSpace(txtDescription.Text)) { MessageBox.Show("How would you describe the package?"); return; } if (dtpStartDate.Value > dtpEndDate.Value) { MessageBox.Show("Package End Date must be later than Package Start Date!"); return; } if (string.IsNullOrWhiteSpace(txtBasicPrice.Text)) { MessageBox.Show("Base Price cannot be empty?"); return; } if (string.IsNullOrWhiteSpace(txtAgencyCommision.Text)) { MessageBox.Show("Agency Commission cannot be empty!"); return; } decimal d = 0; if (!decimal.TryParse(txtBasicPrice.Text, out d)) { //Error MessageBox.Show("Error: Wrong format for Base Price!"); return; } if (!decimal.TryParse(txtAgencyCommision.Text, out d)) { //Error MessageBox.Show("Error: Wrong format for Agency Commission!"); return; } var basicPrice = decimal.Parse(txtBasicPrice.Text); var agencyCommision = decimal.Parse(txtAgencyCommision.Text); if (agencyCommision > basicPrice) { MessageBox.Show("Agency Commission can't be greater than Base Price!"); return; } Package newPackage = new Package(); if (packageTemp != null) { newPackage = _db.Packages.FirstOrDefault(p => p.PackageId == packageTemp.PackageId); } newPackage.PkgName = txtPackageName.Text; newPackage.PkgDesc = txtDescription.Text; newPackage.PkgStartDate = dtpStartDate.Value; newPackage.PkgEndDate = dtpEndDate.Value; newPackage.PkgBasePrice = decimal.Parse(txtBasicPrice.Text); newPackage.PkgAgencyCommission = decimal.Parse(txtAgencyCommision.Text); if (packageTemp == null) { // add package _db.Packages.Add(newPackage); _db.SaveChanges(); MessageBox.Show("Package added successfully!"); } else { //edit package MessageBox.Show("Package edited successfully!"); _db.SaveChanges(); } _managePackageListing.PopulateGrid(); _managePackageListing.dgvPackages.Update(); _managePackageListing.dgvPackages.Refresh(); this.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); MessageBox.Show($"Error: {ex.Message}"); } }