private void btnAdd_Click(object sender, EventArgs e) { if (loggedInAgt == null) { mainForm.btnSignIn_Click(null, null); } else { // declare suppliers List variable and instantiate new List<Supplier> object List <Supplier> suppliers = new List <Supplier>(); // assign suppliers to return of GetSuppliers method call suppliers = SupplierDB.GetSuppliers(); // validate input if (Validator.IsPresent(txtSupplierName)) { if ((txtSupplierName.Text.Length > 255)) { MessageBox.Show("The Supplier Name is too long (>255). Please adjust."); return; } // text boxes validated // now check that the entered Supplier Name does not already exist in the database foreach (Supplier supp in suppliers) { if (txtSupplierName.Text != "" && supp.SupName == txtSupplierName.Text) { MessageBox.Show("The Supplier Name already exists in the database"); return; } } // business logic here is to determine the largest SupplierId in the database // and then assign this value + 1, to the new Supplier entry // Find largest SupplierID int maxId = SupplierDB.FindMaxSupplierId(suppliers); // create Supplier object to be added with new Id Supplier supplier = new Supplier(); supplier.SupplierId = ++maxId; supplier.SupName = txtSupplierName.Text; // problem below because Add Supplier should not return Id bool result = SupplierDB.AddSupplier(supplier); // assign suppliers to return of GetSuppliers method call List <Supplier> newList = SupplierDB.GetSuppliers(); suppliers = newList.OrderBy(s => s.SupplierId).ToList(); // find index of new object in the sorted list int indSorted = SupplierDB.FindIndexofId(suppliers, supplier.SupplierId); // int numSuppliers = suppliers.Count(); DisplaySuppliers(indSorted); } } }
private void btnAdd_Click(object sender, EventArgs e) { supplier = new Supplier(); this.inputSupplierData(supplier); try { supplier.SupplierID = SupplierDB.AddSupplier(supplier); this.DialogResult = DialogResult.OK; } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } }
//user pressed accept button private void btnAccept_Click(object sender, EventArgs e) { if (ValidData()) //method to validate information { if (addSupplier) //adding a new value { supplier = new Supplier(); this.PutSupplierData(supplier); try { if (SupplierDB.AddSupplier(supplier)) { this.DialogResult = DialogResult.OK; } } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } } else //modify supplier { Supplier newSupplier = new Supplier(); //new supplier newSupplier.SupplierId = supplier.SupplierId; this.PutSupplierData(newSupplier); try { if (!SupplierDB.UpdateSupplier(supplier, newSupplier)) { MessageBox.Show("Another user has updated or deleted " + supplier.SupName, "Database Error"); this.DialogResult = DialogResult.Retry; } else { //supplier = newSupplier; this.DialogResult = DialogResult.OK; } } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } } } }
/// <summary> /// Angelito: Save based on add or modify /// </summary> private void SupplierSaveButton_Click(object sender, EventArgs e) { if (addSupplier) { this.PutSupplierData(); try { supplier.SupplierId = SupplierDB.AddSupplier(supplier); this.DialogResult = DialogResult.OK; } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } } else // this is Modify { // build Supplier object with the new data //Package newPkg = new Package(); supplier.SupplierId = currentSup.SupplierId; this.PutSupplierData(); try // try to update { if (!SupplierDB.UpdateSupplier(currentSup, supplier)) //failed { MessageBox.Show("Another user has updated or deleted current supplier", "Concurrency Error"); this.DialogResult = DialogResult.Retry; } else { currentSup = supplier; this.DialogResult = DialogResult.OK; } } catch (Exception ex) { MessageBox.Show("Error while updating: " + ex.Message, ex.GetType().ToString()); } } }
//Call add function private void btnSave_Click(object sender, EventArgs e) { int integer; if (txtSupplierID.Text == "") //if no SupplierID is entered { //display error MessageBox.Show("Please fill in all fields."); txtSupplierID.Focus(); } else if (txtSupName.Text == "") //if no Supplier name is entered { //display error message MessageBox.Show("Please fill in all fields."); txtSupName.Focus(); } else if (!Int32.TryParse(txtSupplierID.Text, out integer))//validate that SupplierID is an integer { MessageBox.Show("Please enter a valid Supplier ID."); txtSupName.Focus(); } else { if (btnAdd.Text == "Add") { txtSearch.Text = ""; Supplier supplier = new Supplier(); supplier.SupplierId = Convert.ToInt32(txtSupplierID.Text); supplier.Name = txtSupName.Text; try { if (SupplierDB.AddSupplier(supplier)) { MessageBox.Show("Supplier added successfully."); updateListView(SupplierDB.GetSuppliers()); clearForm(); pnlAddUpdate.Visible = false; } else { MessageBox.Show("Supplier not added successfully."); } } catch (SqlException ex) { if (ex.Message.StartsWith("Violation of PRIMARY KEY")) { MessageBox.Show("Supplier with this ID already exists. Enter unique ID."); txtSupplierID.Focus(); } else { MessageBox.Show(ex.Message); } } } else if (btnAdd.Text == "Update") { if (UpdateSupplier(Convert.ToInt32(selectedSupplierID))) { MessageBox.Show("Record updated successfully."); clearForm(); selectedSupName = ""; selectedSupplierID = ""; } else { MessageBox.Show("Record was not updated."); } } } }
//click the Save button to save the new data or updated data private void btnSaveSup_Click(object sender, EventArgs e) { List <Supplier> suppliers; suppliers = SupplierDB.GetAllSuppliers(); // get the lastest suppliers' data Product removeSupProd = new Product(); List <Product> newSuppliedProds = new List <Product>(); ProductSupplier supProd = new ProductSupplier(); //validate the cmbSupName if (cmbSupId.Text == "") { lblSupMessage.Text = "Note:Please enter a supplier name."; } else { if (tabSupplierAccessMode == AccessMode.Edit) { sup.SupName = cmbSupId.Text; sup.SupplierId = Convert.ToInt32(txtSupplierId.Text); try { if (!SupplierDB.UpdateSupplier(oldSup, sup)) { MessageBox.Show("Another user has updated or deleted that supplier.", "Database Error"); } else { lblSupMessage.Text = "Note:The supplier updated."; refreshCmbSupIdItems(); oldSup = sup.CopySupplier(); btnSaveSup.Enabled = false; } } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } } else if (tabSupplierAccessMode == AccessMode.Add) { sup.SupplierId = SupplierDB.GetNewSupplierId(); //create a new supplierId sup.SupName = cmbSupId.Text; //get the entered name try { SupplierDB.AddSupplier(sup); // once a new supplier's data is inserted into the Suppliers table txtSupplierId.Text = sup.SupplierId.ToString(); //display the new supplierId btnDeleteSup.Enabled = true; // let the Delete button enabled btnAddSuppliedProd.Enabled = true; // let the supplied products can be edited btnRemoveSuppliedProd.Enabled = true; // let the supplied products can be edited lblSupMessage.Text = "Note:The supplier added."; refreshCmbSupIdItems(); btnSaveSup.Enabled = false; tabSupplierAccessMode = AccessMode.Edit; } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } } } }