private void AddCustomer_Click(object sender, EventArgs e) { try { string customerName = CustomerNameTextBox.Text; int addressId = AddressDao.GetAddressIdByString(Address1TextBox.Text); // create the address if one doesn't exist if (addressId == -1) { var newAddress = AddressDao.CreateAddress(Address1TextBox.Text, Address2TextBox.Text, Convert.ToInt32(CitySelectBox.SelectedValue), ZipTextBox.Text, PhoneTextBox.Text, DateTime.Now, DateTime.Now, LoginForm.username, LoginForm.username); bool added = AddressDao.SaveNewAddress(newAddress); if (added) { addressId = AddressDao.GetAddressIdByString(Address1TextBox.Text); } } if (customerName.Trim() != "" && addressId > 0) { var customer = CustomerDao.CreateCustomer(customerName, addressId, LoginForm.username); bool created = CustomerDao.SaveNewCustomer(customer); // clear out options CustomerNameTextBox.Text = null; Address1TextBox.Text = null; Address2TextBox.Text = null; PhoneTextBox.Text = null; ZipTextBox.Text = null; if (created == true) { SetDefaults(); MessageBox.Show($"Success! You have successfully added {customerName}."); } else { MessageBox.Show($"Something seems to have gone wrong, {customerName} was not created."); } } else { MessageBox.Show("Please make sure both a name and address are selected."); } } catch (Exception err) { MessageBox.Show("Couldn't create customer because: " + err); } }
private void ApplyChangesButton_Click(object sender, EventArgs e) { string updatedName = RenameTextBox.Text; int customerId = Convert.ToInt32(EditCustomerComboBox.SelectedValue); int updatedAddress = AddressDao.GetAddressIdByString(EditAddressComboBox.SelectedValue.ToString()); if (updatedName.Trim() != "" && updatedAddress >= 0 && customerId >= 0) { Data.Models.Customer cust = CustomerDao.GetCustomerById(customerId); var modifiedCustomer = CustomerDao.ApplyCustomerChanges(cust, updatedName, updatedAddress, 1, DateTime.Now, LoginForm.username); var updated = CustomerDao.UpdateCustomer(modifiedCustomer); if (updated == true) { MessageBox.Show($"Success! {updatedName} was updated."); SetDefaults(); } else { MessageBox.Show($"Sorry, {updatedName} was not updated properly."); } } }