// btn to delete a record private void btnDelete_Click(object sender, EventArgs e) { if (dgvSuppliers.SelectedRows.Count > 0) { // sets the object to delete int index = dgvSuppliers.SelectedRows[0].Index; suplierContact = suppliersContacts[index]; // asks user if they are sure DialogResult result = MessageBox.Show("Delete the supplier " + suplierContact.SupConCompany + "?", "Confirm Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (result == DialogResult.Yes) { // creates the other table objects Supplier supplier = new Supplier(); List <Products_Suppliers> products_suppliers = new List <Products_Suppliers>(); try // tries to delete the table records { // gets the data for the tasble records that need to be deleted supplier = SupplierDB.GetSupplier(suplierContact.SupplierId); products_suppliers = Products_SuppliersDB.GetAllProdSupOnID(suplierContact.SupplierId); // deletes each productsuppliers table record foreach (Products_Suppliers ps in products_suppliers) { Products_SuppliersDB.DeleteProdSup(ps); } // deltes the suppliercontacts record SupplierContactsDB.DeleteSup(suplierContact); // deltes the supplier record SupplierDB.DeleteSup(supplier); // redisplays the dgv suppliersContacts = SupplierContactsDB.listSuppliers(); refreshDGV(); } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } } } else { MessageBox.Show("Please select a Product"); } }
// button to delete fa product from the list of products private void btnDelete_Click(object sender, EventArgs e) { if (dgvProducts.SelectedRows.Count > 0) { // for adding if (add) { productSuppliers.RemoveAt(this.dgvProducts.SelectedRows[0].Index); RedisplayList(); } //for modifing else { DialogResult result = MessageBox.Show("Are you sure you want to delete this Product offered by Supplier?", "Confirm Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (result == DialogResult.Yes) { int index = this.dgvProducts.SelectedRows[0].Index; try { if (!Products_SuppliersDB.DeleteProdSup(productSuppliers[index])) { MessageBox.Show("That product has been updated or deleted already.", "Database Error"); } else { productSuppliers = Products_SuppliersDB.GetAllProdSupOnID(supplierContact.SupplierId); this.RedisplayList(); } } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } } } } else { MessageBox.Show("Please select a Product"); } }
private void DeleteSupplier(Supplier sp) { SupplierContacts suplierContact = SupplierContactsDB.GetSupplierbySupID(sp.SupplierId); // asks user if they are sure DialogResult result = MessageBox.Show("Delete the supplier " + suplierContact.SupConCompany + "?", "Confirm Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (result == DialogResult.Yes) { // creates the other table objects Supplier supplier = new Supplier(); List <Products_Suppliers> products_suppliers = new List <Products_Suppliers>(); try // tries to delete the table records { // gets the data for the tasble records that need to be deleted supplier = SupplierDB.GetSupplier(suplierContact.SupplierId); products_suppliers = Products_SuppliersDB.GetAllProdSupOnID(suplierContact.SupplierId); // deletes each productsuppliers table record foreach (Products_Suppliers ps in products_suppliers) { Products_SuppliersDB.DeleteProdSup(ps); } // deletes the suppliercontacts record SupplierContactsDB.DeleteSup(suplierContact); // deletes the supplier record SupplierDB.DeleteSup(supplier); } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } RefreshSuppliers(); } }
// button to add a product to the supplier private void btnProduct_Click(object sender, EventArgs e) { if (cmbProductBName.IsPresent()) { // for adding a supplier if (add) { Products_Suppliers product = new Products_Suppliers(); product.productid = (int)cmbProductBName.SelectedValue; product.productName = cmbProductBName.Text; productSuppliers.Add(product); RedisplayList(); cmbProductBName.SelectedIndex = -1; } //for modifing a supplier else { Products_Suppliers product = new Products_Suppliers(); product.productid = (int)cmbProductBName.SelectedValue; product.productName = cmbProductBName.Text; product.supplierid = supplier.SupplierId; try { Products_SuppliersDB.AddProdSupp(product); productSuppliers = Products_SuppliersDB.GetAllProdSupOnID(supplierContact.SupplierId); } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } RedisplayList(); cmbProductBName.SelectedIndex = -1; } } }
// sets up the form on load private void frmAddModifySupplier_Load(object sender, EventArgs e) { this.LoadProductComboBox(); // does the fiollowing if its a add if (this.add) { // find the next availble supplierid and suppliercontactid supplierContact.SupplierContactId = SupplierContactsDB.GetNextAvailableID(); txtSCID.Text = supplierContact.SupplierContactId.ToString(); supplierContact.SupplierId = SupplierDB.GetNextAvailableID(); supplier.SupplierId = SupplierDB.GetNextAvailableID(); this.Text = "Add a Supplier"; lblTitle.Text = "Add Supplier Information"; cmbProductBName.SelectedIndex = -1; } //else does this for modify else { this.Text = "Modify a Supplier"; lblTitle.Text = "Modify Supplier Information"; try { supplier = SupplierDB.GetSupplier(supplierContact.SupplierId); productSuppliers = Products_SuppliersDB.GetAllProdSupOnID(supplierContact.SupplierId); this.DisplaySupplier(); this.RedisplayList(); cmbProductBName.SelectedIndex = -1; } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } } }