// ------------Added by Wei Guang Yan---------------- // method of Updating suplier's name to database public void UpdateSupplier(string txtUpdate) { // Initialization for messagebox string message; string title = "Supplier Update"; MessageBoxButtons button = MessageBoxButtons.OK; MessageBoxIcon icon = MessageBoxIcon.Warning; // Excute updating int id = Convert.ToInt32(comboBox2.SelectedValue); bool flag = SuppliersDB.UpdateSupplier(id, txtUpdate); // Check whether updating is successful, and show message if (flag == true) { int index = comboBox2.SelectedIndex; comboBox2.DataSource = SuppliersDB.GetSuppliers(); comboBox2.DisplayMember = "SupName"; comboBox2.ValueMember = "SupplierId"; comboBox2.SelectedIndex = index; message = "The supplier is updated successfully!"; icon = MessageBoxIcon.None; } else { message = "Error: Fail to update supplier!\n Please try again or contact IT support."; icon = MessageBoxIcon.Error; } MessageBox.Show(message, title, button, icon); }
/// <summary> /// On accept button click /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnAccept_Click(object sender, EventArgs e) { if (IsValidData()) // first validate { if (addSupplier) { supplier = new Suppliers(); this.PutSupplierData(supplier); try { if (SuppliersDB.AddSuppliers(supplier)) { this.DialogResult = DialogResult.OK; } else { MessageBox.Show("There was an error adding that supplier", "Database Error"); this.DialogResult = DialogResult.Retry; } } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } } else { Suppliers newSupplier = new Suppliers(); newSupplier.SupplierId = supplier.SupplierId; this.PutSupplierData(newSupplier); try { if (!SuppliersDB.UpdateSupplier(supplier, newSupplier)) { MessageBox.Show("Another user has updated or " + "deleted that supplier.", "Database Error"); this.DialogResult = DialogResult.Retry; } else { supplier = newSupplier; this.DialogResult = DialogResult.OK; } } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } } } }
private void btnAdd_Click(object sender, EventArgs e) { if (IsValidData()) { if (addsupplier) // processing Add { supplier = new Suppliers(); this.PutSupplier(supplier); try { supplier.SupplierId = SuppliersDB.AddSupplier(supplier); this.DialogResult = DialogResult.OK; } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } } else // processing Modify { suppliers newSupplier = new suppliers { SupplierId = supplier.SupplierId }; this.PutSupplier(newSupplier); try { if (!SuppliersDB.UpdateSupplier(supplier, newSupplier)) { MessageBox.Show("Another user has updated or " + "deleted that supplier ID.", "Database Error"); this.DialogResult = DialogResult.Retry; } else { supplier = newSupplier; this.DialogResult = DialogResult.OK; } } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } } } }
// accept supplier UPDATE click private void btnAcceptEdit_Click(object sender, EventArgs e) { Suppliers currentSupplier = SuppliersDB.GetSupplierById((int)supId); Suppliers newSupplier = new Suppliers(); newSupplier.SupplierId = currentSupplier.SupplierId; this.PutSupplierData(newSupplier); if (currentSupplier.SupName == null) { MessageBox.Show("Fill in name before adding product", "Please check"); this.DialogResult = DialogResult.OK; } else if (Validator.IsPresent(txtSupName)) { try { if (!SuppliersDB.UpdateSupplier(currentSupplier, newSupplier)) { MessageBox.Show("Another user has updated or " + "deleted that Supplier.", "Database Error"); this.DialogResult = DialogResult.Retry; } else { supplier = newSupplier; MessageBox.Show("Supplier updated", "Success!"); this.DialogResult = DialogResult.OK; } } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } } this.Close(); }
private void btnUpdateSupplier_Click(object sender, EventArgs e) { try { if (Validator.IsPresent(supNameTextBox, "Supplier New Name")) { Suppliers updateSupplier = new Suppliers(); updateSupplier.SupplierId = Convert.ToInt64(supplierIdComboBox.SelectedValue); updateSupplier.SupName = supNameTextBox.Text.ToUpper().Trim(); if (SuppliersDB.UpdateSupplier(updateSupplier)) { MessageBox.Show("Supplier has been updated :)"); UpdateAllInfos(); suppliersDataGridView.DataSource = AllSuppliers; } } } //catch the other error and show the ex error message from db class catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } }
private void fourBtnSave_Click(object sender, EventArgs e) { if (fourTab.SelectedIndex == 1) // EDIT MODE { // get current Supplier obj var currentSupplier = SuppliersDB.GetSuppliers() .Find(s => s.SupplierId == Convert.ToInt32(fourTxtEditSuppId.Text)); // initialize new Suppliers obj Suppliers newSupplier; // do validation: no empty name, no duplicated name if (Validator.TBIsEmpty(fourTxtEditSuppName, "Supplier Name") || FindDuplicatedSupplierName(fourTxtEditSuppName.Text)) { // validation failed, rollback to old name, do nothing fourTxtEditSuppName.Text = currentSupplier.SupName; fourTxtEditSuppName.SelectAll(); return; } else { // validation passed, create new Suppliers obj newSupplier = new Suppliers { SupName = fourTxtEditSuppName.Text } }; // try update try { int rowsAffected = SuppliersDB.UpdateSupplier(currentSupplier, newSupplier); MessageBox.Show($"{rowsAffected} record was successfully updated.", "Congratulations"); } catch (Exception ex) { MessageBox.Show($"Cannot complete update due to error: {ex.Message}."); } } else if (fourTab.SelectedIndex == 2) // ADD MODE { // validate input: no empty name, no duplicated name if (!Validator.TBIsEmpty(fourTxtAddSuppName, "Supplier Name") && Validator.TBHasNonNegativeInt(fourTxtAddSuppId, "Supplier Id") && !FindDuplicatedSupplierName(fourTxtAddSuppName.Text)) { // validation passed, create new Suppliers obj var newSupplier = new Suppliers { SupName = fourTxtAddSuppName.Text, // PK SuppId is not auto-increment, need to assign an new id SupplierId = Convert.ToInt32(fourTxtAddSuppId.Text) }; // try to add to DB try { int id = SuppliersDB.AddSupplier(newSupplier); MessageBox.Show($"New supplier was successfully added, supplier id: {id}.", "Congratulations"); fourTxtAddSuppId.Clear(); fourTxtAddSuppName.Clear(); } catch (Exception ex) { MessageBox.Show($"Cannot add new supplier due to duplicated id. Error detail: {ex.Message}."); } } } }