// ------------Added by Wei Guang Yan---------------- // Method of inserting suplier's name to database public void AddSupplier(string txtAdd) { // Innitialize for messagebox string message; string title = "New Product Insert"; MessageBoxButtons button = MessageBoxButtons.OK; MessageBoxIcon icon; // Generate new supplier Id based on max id in database int id = SuppliersDB.GetMaxId() + 1; // Add new item to database bool flag = SuppliersDB.AddSupplier(id, txtAdd); // Check whether adding excution is successful, and show message if (flag == true) { comboBox2.DataSource = SuppliersDB.GetSuppliers(); comboBox2.DisplayMember = "SupName"; comboBox2.ValueMember = "SupplierId"; comboBox2.SelectedIndex = comboBox2.Items.Count - 1; txtAddItem.Text = ""; message = "The supplier is added successfully!"; icon = MessageBoxIcon.None; } else { message = "Error: Fail to add new supplier!\n Please try again or contact IT supports."; icon = MessageBoxIcon.Error; } MessageBox.Show(message, title, button, icon); }
private void btnSaveSupp_Click(object sender, EventArgs e) { try { if (Validator.IsPresent(txtNewSupp, "New Supplier Name")) { Suppliers newSupplier = new Suppliers(); newSupplier.SupName = txtNewSupp.Text; newSupplier.SupplierId = AllSuppliers.Max(s => s.SupplierId) + 1; if (SuppliersDB.AddSupplier(newSupplier)) { 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 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()); } } } }
private void btnAddSupplier_Click(object sender, EventArgs e) { supplier = new Suppliers(); this.PutSupplierData(supplier); try { supplier.SupplierId = SuppliersDB.AddSupplier(supplier); MessageBox.Show("Supplier added", "Success!"); this.DialogResult = DialogResult.OK; } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } this.Close(); }
private void btnUpdate_Click(object sender, EventArgs e) { // valid supplier id if (!Validator.IsValidSupplierId(supplierId.Text, out string result)) { MessageBox.Show(result, "Supplier Error"); return; } // supplier name required as per Jolanta if (string.IsNullOrEmpty(supplierName.Text)) { MessageBox.Show("Supplier Name is required.", "Supplier Error"); return; } Supplier supplier = new Supplier() { SupplierId = Int32.Parse(supplierId.Text), SupName = supplierName.Text }; try { SuppliersDB.AddSupplier(supplier); this.DialogResult = DialogResult.OK; } catch (SqlException ex) { if (ex.Number == 2627) // supplier id already exists { MessageBox.Show("Supplier ID already exists.", "ID Error"); } } catch (Exception ex) { MessageBox.Show("Error while adding supplier.\n\n" + ex.Message, ex.GetType().ToString()); } }
//adds the input to the table private void AddButton_Click(object sender, EventArgs e) { if (isValid()) { supp = new Supplier(); this.buildSupplier(supp); try { if (SuppliersDB.AddSupplier(supp)) { MessageBox.Show("Supplier added successfully.", "Success"); clearContent(); } else { MessageBox.Show("Failed to add the suplier. Please try again.", "Database Error"); clearContent(); } } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } } }
private void btnAdd_Click(object sender, EventArgs e) { if (Validator.IsPresent(txtSupName)) //&& !Validator.UserInputDataExists(txtSupName)) { supplier = new Suppliers(); supplier.SupName = txtSupName.Text; // add SupID code ... called ADD function txtSupName.Text = supplier.SupName; supplier.SupplierId = SuppliersDB.AddSupplier(supplier); LoadComboAndGrid(); } else { MessageBox.Show("Type in Supplier Name if none exists"); this.DialogResult = DialogResult.Retry; } txtSupName.Text = ""; }
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}."); } } } }