Finish Add/Update Customers Form

This commit is contained in:
Spudnut2000 2025-06-19 12:26:24 -05:00
parent d26498cd3c
commit 8b504a9c79
6 changed files with 55 additions and 64 deletions

View File

@ -46,7 +46,7 @@ partial class AddUpdateCustomerForm
label7 = new System.Windows.Forms.Label(); label7 = new System.Windows.Forms.Label();
zipTextBox = new System.Windows.Forms.TextBox(); zipTextBox = new System.Windows.Forms.TextBox();
label8 = new System.Windows.Forms.Label(); label8 = new System.Windows.Forms.Label();
textBox1 = new System.Windows.Forms.TextBox(); countryTextBox = new System.Windows.Forms.TextBox();
SuspendLayout(); SuspendLayout();
// //
// label1 // label1
@ -182,13 +182,13 @@ partial class AddUpdateCustomerForm
label8.Text = "Country"; label8.Text = "Country";
label8.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; label8.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
// //
// textBox1 // countryTextBox
// //
textBox1.Location = new System.Drawing.Point(12, 299); countryTextBox.Location = new System.Drawing.Point(12, 299);
textBox1.Name = "textBox1"; countryTextBox.Name = "countryTextBox";
textBox1.Size = new System.Drawing.Size(218, 23); countryTextBox.Size = new System.Drawing.Size(218, 23);
textBox1.TabIndex = 14; countryTextBox.TabIndex = 14;
textBox1.Text = "US"; countryTextBox.Text = "US";
// //
// AddUpdateCustomerForm // AddUpdateCustomerForm
// //
@ -197,7 +197,7 @@ partial class AddUpdateCustomerForm
BackColor = System.Drawing.SystemColors.Control; BackColor = System.Drawing.SystemColors.Control;
ClientSize = new System.Drawing.Size(800, 450); ClientSize = new System.Drawing.Size(800, 450);
Controls.Add(label8); Controls.Add(label8);
Controls.Add(textBox1); Controls.Add(countryTextBox);
Controls.Add(label7); Controls.Add(label7);
Controls.Add(zipTextBox); Controls.Add(zipTextBox);
Controls.Add(label6); Controls.Add(label6);
@ -219,7 +219,7 @@ partial class AddUpdateCustomerForm
} }
private System.Windows.Forms.Label label8; private System.Windows.Forms.Label label8;
private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.TextBox countryTextBox;
private System.Windows.Forms.Label label6; private System.Windows.Forms.Label label6;
private System.Windows.Forms.TextBox cityTextBox; private System.Windows.Forms.TextBox cityTextBox;

View File

@ -9,6 +9,8 @@ namespace C969Project
{ {
private Customer? _currentCustomer; private Customer? _currentCustomer;
public event EventHandler UpdateCustomersList;
public AddUpdateCustomerForm() public AddUpdateCustomerForm()
{ {
InitializeComponent(); InitializeComponent();
@ -61,7 +63,8 @@ namespace C969Project
string.IsNullOrWhiteSpace(addressTextBox.Text) || string.IsNullOrWhiteSpace(addressTextBox.Text) ||
string.IsNullOrWhiteSpace(cityTextBox.Text) || string.IsNullOrWhiteSpace(cityTextBox.Text) ||
string.IsNullOrWhiteSpace(zipTextBox.Text) || string.IsNullOrWhiteSpace(zipTextBox.Text) ||
string.IsNullOrWhiteSpace(phoneTextBox.Text)) string.IsNullOrWhiteSpace(phoneTextBox.Text) ||
string.IsNullOrWhiteSpace(countryTextBox.Text))
{ {
MessageBox.Show("All fields must be filled out.", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); MessageBox.Show("All fields must be filled out.", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return; return;
@ -73,13 +76,11 @@ namespace C969Project
return; return;
} }
string currentUser = AppState.CurrentUser.Username; string currentUser = AppState.CurrentUser.Username;
try try
{ {
string countryName = countryTextBox.Text;
string countryName = "US";
int currentCountryId = DatabaseHelper.GetCountryId(countryName); int currentCountryId = DatabaseHelper.GetCountryId(countryName);
if (currentCountryId == 0) if (currentCountryId == 0)
{ {
@ -183,6 +184,8 @@ namespace C969Project
// TODO: Implement robust exception handling, logging, and user notification as per assignment A2b // TODO: Implement robust exception handling, logging, and user notification as per assignment A2b
MessageBox.Show($"An error occurred while saving the customer: {ex.Message}", "Database Error", MessageBoxButtons.OK, MessageBoxIcon.Error); MessageBox.Show($"An error occurred while saving the customer: {ex.Message}", "Database Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
} }
UpdateCustomersList?.Invoke(this, EventArgs.Empty);
} }
private void CancelButton_Click(object sender, EventArgs e) private void CancelButton_Click(object sender, EventArgs e)

View File

@ -13,7 +13,7 @@ public partial class DashboardForm : Form
CustomersButton.Click += (sender, args) => CustomersButton.Click += (sender, args) =>
{ {
_recordsForm.ShowDialog(); _recordsForm.ShowDialog();
_recordsForm.UpdateCustomersList(); _recordsForm.UpdateCustomersList(null, EventArgs.Empty);
}; };

View File

@ -1,5 +1,5 @@
using System; using System;
using System.Data; // For CommandType using System.Data;
using MySql.Data.MySqlClient; using MySql.Data.MySqlClient;
using C969Project.Data.Models; using C969Project.Data.Models;
@ -9,11 +9,6 @@ namespace C969Project.Data
{ {
private static string ConnectionString => AppSettings.GetSetting("ConnectionStrings", "DefaultConnection"); private static string ConnectionString => AppSettings.GetSetting("ConnectionStrings", "DefaultConnection");
public static void Initialize()
{
// Initialization logic if any
}
public static User? Login(string username, string password) public static User? Login(string username, string password)
{ {
using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection")); using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection"));
@ -47,6 +42,7 @@ namespace C969Project.Data
return null; return null;
} }
#region Customers
public static List<Customer> RetrieveCustomers() public static List<Customer> RetrieveCustomers()
{ {
using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection")); using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection"));
@ -84,11 +80,6 @@ namespace C969Project.Data
return customers; return customers;
} }
/// <summary>
/// Adds a new customer record to the database and returns the generated customerId.
/// </summary>
/// <param name="customer">The customer object to add.</param>
/// <returns>The ID of the newly added customer; otherwise, 0 if an error occurs.</returns>
public static int AddCustomer(Customer customer) public static int AddCustomer(Customer customer)
{ {
int customerId = 0; int customerId = 0;
@ -97,11 +88,10 @@ namespace C969Project.Data
{ {
connection.Open(); connection.Open();
// Removed customerId from INSERT statement as it's auto-incremented
string query = string query =
"INSERT INTO customer (customerName, addressId, active, createDate, createdBy, lastUpdate, lastUpdateBy) " + "INSERT INTO customer (customerName, addressId, active, createDate, createdBy, lastUpdate, lastUpdateBy) " +
"VALUES (@customerName, @addressId, @active, @createDate, @createdBy, CURRENT_TIMESTAMP, @lastUpdateBy); " + // Use CURRENT_TIMESTAMP for lastUpdate "VALUES (@customerName, @addressId, @active, @createDate, @createdBy, CURRENT_TIMESTAMP, @lastUpdateBy); " +
"SELECT LAST_INSERT_ID();"; // Get the newly generated ID "SELECT LAST_INSERT_ID();";
using MySqlCommand command = new MySqlCommand(query, connection); using MySqlCommand command = new MySqlCommand(query, connection);
@ -110,7 +100,7 @@ namespace C969Project.Data
command.Parameters.AddWithValue("@active", customer.Active); command.Parameters.AddWithValue("@active", customer.Active);
command.Parameters.AddWithValue("@createDate", DateTime.UtcNow); command.Parameters.AddWithValue("@createDate", DateTime.UtcNow);
command.Parameters.AddWithValue("@createdBy", AppState.CurrentUser.Username); command.Parameters.AddWithValue("@createdBy", AppState.CurrentUser.Username);
command.Parameters.AddWithValue("@lastUpdateBy", AppState.CurrentUser.Username); // lastUpdateBy is updated manually command.Parameters.AddWithValue("@lastUpdateBy", AppState.CurrentUser.Username);
object result = command.ExecuteScalar(); object result = command.ExecuteScalar();
if (result != null && result != DBNull.Value) if (result != null && result != DBNull.Value)
@ -140,14 +130,14 @@ namespace C969Project.Data
string query = string query =
"UPDATE customer SET customerName = @customerName, addressId = @addressId, active = @active, " + "UPDATE customer SET customerName = @customerName, addressId = @addressId, active = @active, " +
"lastUpdate = CURRENT_TIMESTAMP, lastUpdateBy = @lastUpdateBy WHERE customerId = @customerId"; // Use CURRENT_TIMESTAMP for lastUpdate "lastUpdate = CURRENT_TIMESTAMP, lastUpdateBy = @lastUpdateBy WHERE customerId = @customerId";
using MySqlCommand command = new MySqlCommand(query, connection); using MySqlCommand command = new MySqlCommand(query, connection);
command.Parameters.AddWithValue("@customerName", customer.CustomerName); command.Parameters.AddWithValue("@customerName", customer.CustomerName);
command.Parameters.AddWithValue("@addressId", customer.AddressId); command.Parameters.AddWithValue("@addressId", customer.AddressId);
command.Parameters.AddWithValue("@active", customer.Active); command.Parameters.AddWithValue("@active", customer.Active);
command.Parameters.AddWithValue("@lastUpdateBy", AppState.CurrentUser.Username); // lastUpdateBy is updated manually command.Parameters.AddWithValue("@lastUpdateBy", AppState.CurrentUser.Username);
command.Parameters.AddWithValue("@customerId", customer.CustomerId); command.Parameters.AddWithValue("@customerId", customer.CustomerId);
command.ExecuteNonQuery(); command.ExecuteNonQuery();
@ -191,6 +181,9 @@ namespace C969Project.Data
{ {
DeleteCustomer(customer.CustomerId); DeleteCustomer(customer.CustomerId);
} }
#endregion
#region Addresses
public static Address? RetrieveAddress(int addressId) public static Address? RetrieveAddress(int addressId)
{ {
@ -272,11 +265,6 @@ namespace C969Project.Data
return addresses; return addresses;
} }
/// <summary>
/// Adds a new address to the database and returns the generated addressId.
/// </summary>
/// <param name="address">The address object to add.</param>
/// <returns>The ID of the newly added address; otherwise, 0 if an error occurs.</returns>
public static int AddAddress(Address address) public static int AddAddress(Address address)
{ {
int addressId = 0; int addressId = 0;
@ -285,11 +273,10 @@ namespace C969Project.Data
{ {
connection.Open(); connection.Open();
// Removed addressId from INSERT statement as it's auto-incremented
string query = string query =
"INSERT INTO address (address, address2, cityId, postalCode, phone, createDate, createdBy, lastUpdate, lastUpdateBy) " + "INSERT INTO address (address, address2, cityId, postalCode, phone, createDate, createdBy, lastUpdate, lastUpdateBy) " +
"VALUES (@address1, @address2, @cityId, @postalCode, @phone, @createDate, @createdBy, CURRENT_TIMESTAMP, @lastUpdateBy); " + // Use CURRENT_TIMESTAMP for lastUpdate "VALUES (@address1, @address2, @cityId, @postalCode, @phone, @createDate, @createdBy, CURRENT_TIMESTAMP, @lastUpdateBy); " +
"SELECT LAST_INSERT_ID();"; // Get the newly generated ID "SELECT LAST_INSERT_ID();";
using MySqlCommand command = new MySqlCommand(query, connection); using MySqlCommand command = new MySqlCommand(query, connection);
@ -330,7 +317,7 @@ namespace C969Project.Data
string query = string query =
"UPDATE address SET address = @address1, address2 = @address2, cityId = @cityId, postalCode = @postalCode, " + "UPDATE address SET address = @address1, address2 = @address2, cityId = @cityId, postalCode = @postalCode, " +
"phone = @phone, lastUpdate = CURRENT_TIMESTAMP, lastUpdateBy = @lastUpdateBy WHERE addressId = @addressId"; // Use CURRENT_TIMESTAMP for lastUpdate "phone = @phone, lastUpdate = CURRENT_TIMESTAMP, lastUpdateBy = @lastUpdateBy WHERE addressId = @addressId";
using MySqlCommand command = new MySqlCommand(query, connection); using MySqlCommand command = new MySqlCommand(query, connection);
@ -384,6 +371,10 @@ namespace C969Project.Data
DeleteAddress(address.Id); DeleteAddress(address.Id);
} }
#endregion
#region Appointments
public static Appointment? RetrieveAppointment(int appointmentId) public static Appointment? RetrieveAppointment(int appointmentId)
{ {
using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection")); using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection"));
@ -587,6 +578,9 @@ namespace C969Project.Data
DeleteAppointment(appointment.AppointmentId); DeleteAppointment(appointment.AppointmentId);
} }
#endregion
#region Cities
public static City? RetrieveCity(int cityId) public static City? RetrieveCity(int cityId)
{ {
using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection")); using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection"));
@ -684,7 +678,6 @@ namespace C969Project.Data
} }
catch (Exception ex) catch (Exception ex)
{ {
// TODO: Implement robust exception handling, logging, and user notification as per assignment A2b/A3b.
Console.WriteLine($"Error getting city ID: {ex.Message}"); Console.WriteLine($"Error getting city ID: {ex.Message}");
} }
return cityId; return cityId;
@ -698,11 +691,10 @@ namespace C969Project.Data
{ {
connection.Open(); connection.Open();
// Removed cityId from INSERT statement as it's auto-incremented
string query = string query =
"INSERT INTO city (city, countryId, createDate, createdBy, lastUpdate, lastUpdateBy) " + "INSERT INTO city (city, countryId, createDate, createdBy, lastUpdate, lastUpdateBy) " +
"VALUES (@cityName, @countryId, @createDate, @createdBy, CURRENT_TIMESTAMP, @lastUpdateBy); " + // Use CURRENT_TIMESTAMP for lastUpdate "VALUES (@cityName, @countryId, @createDate, @createdBy, CURRENT_TIMESTAMP, @lastUpdateBy); " +
"SELECT LAST_INSERT_ID();"; // Get the newly generated ID "SELECT LAST_INSERT_ID();";
using MySqlCommand command = new MySqlCommand(query, connection); using MySqlCommand command = new MySqlCommand(query, connection);
@ -740,7 +732,7 @@ namespace C969Project.Data
string query = string query =
"UPDATE city SET city = @cityName, countryId = @countryId, " + "UPDATE city SET city = @cityName, countryId = @countryId, " +
"lastUpdate = CURRENT_TIMESTAMP, lastUpdateBy = @lastUpdateBy WHERE cityId = @cityId"; // Use CURRENT_TIMESTAMP for lastUpdate "lastUpdate = CURRENT_TIMESTAMP, lastUpdateBy = @lastUpdateBy WHERE cityId = @cityId";
using MySqlCommand command = new MySqlCommand(query, connection); using MySqlCommand command = new MySqlCommand(query, connection);
@ -790,7 +782,9 @@ namespace C969Project.Data
{ {
DeleteCity(city.CityID); DeleteCity(city.CityID);
} }
#endregion
#region Countries
public static Country? RetrieveCountry(int countryId) public static Country? RetrieveCountry(int countryId)
{ {
using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection")); using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection"));
@ -885,7 +879,6 @@ namespace C969Project.Data
} }
catch (Exception ex) catch (Exception ex)
{ {
// TODO: Implement robust exception handling, logging, and user notification as per assignment A2b/A3b.
Console.WriteLine($"Error getting country ID: {ex.Message}"); Console.WriteLine($"Error getting country ID: {ex.Message}");
} }
return countryId; return countryId;
@ -899,11 +892,10 @@ namespace C969Project.Data
{ {
connection.Open(); connection.Open();
// Removed countryId from INSERT statement as it's auto-incremented
string query = string query =
"INSERT INTO country (country, createDate, createdBy, lastUpdate, lastUpdateBy) " + "INSERT INTO country (country, createDate, createdBy, lastUpdate, lastUpdateBy) " +
"VALUES (@countryName, @createDate, @createdBy, CURRENT_TIMESTAMP, @lastUpdateBy); " + // Use CURRENT_TIMESTAMP for lastUpdate "VALUES (@countryName, @createDate, @createdBy, CURRENT_TIMESTAMP, @lastUpdateBy); " +
"SELECT LAST_INSERT_ID();"; // Get the newly generated ID "SELECT LAST_INSERT_ID();";
using MySqlCommand command = new MySqlCommand(query, connection); using MySqlCommand command = new MySqlCommand(query, connection);
@ -940,7 +932,7 @@ namespace C969Project.Data
string query = string query =
"UPDATE country SET country = @countryName, " + "UPDATE country SET country = @countryName, " +
"lastUpdate = CURRENT_TIMESTAMP, lastUpdateBy = @lastUpdateBy WHERE countryId = @countryId"; // Use CURRENT_TIMESTAMP for lastUpdate "lastUpdate = CURRENT_TIMESTAMP, lastUpdateBy = @lastUpdateBy WHERE countryId = @countryId";
using MySqlCommand command = new MySqlCommand(query, connection); using MySqlCommand command = new MySqlCommand(query, connection);
@ -989,5 +981,6 @@ namespace C969Project.Data
{ {
DeleteCountry(country.CountryID); DeleteCountry(country.CountryID);
} }
#endregion
} }
} }

View File

@ -4,18 +4,12 @@ namespace C969Project
{ {
internal static class Program internal static class Program
{ {
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread] [STAThread]
static void Main() static void Main()
{ {
AppSettings.Initialize(); AppSettings.Initialize();
Localization.Initialize(); Localization.Initialize();
DatabaseHelper.Initialize();
// To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize(); ApplicationConfiguration.Initialize();
Application.Run(new LoginForm()); Application.Run(new LoginForm());
} }

View File

@ -11,19 +11,20 @@ public partial class RecordsForm : Form
public RecordsForm() public RecordsForm()
{ {
InitializeComponent(); InitializeComponent();
UpdateCustomersList(); UpdateCustomersList(null, EventArgs.Empty);
AddButton.Click += (sender, args) => AddButton.Click += (sender, args) =>
{ {
_form.InitAdd(); _form.InitAdd();
_form.Closed += (o, args2) => { UpdateCustomersList(); }; _form.UpdateCustomersList += UpdateCustomersList;
}; };
ModifyButton.Click += (sender, args) => ModifyButton.Click += (sender, args) =>
{ {
if (_customers is null) return;
var selectedCustomer = _customers[CustomersDataGrid.CurrentCell.RowIndex]; var selectedCustomer = _customers[CustomersDataGrid.CurrentCell.RowIndex];
_form.InitUpdate(selectedCustomer); _form.InitUpdate(selectedCustomer);
_form.Closed += (o, args2) => { UpdateCustomersList(); }; _form.UpdateCustomersList += UpdateCustomersList;
}; };
DeleteButton.Click += (sender, args) => DeleteButton.Click += (sender, args) =>
@ -34,14 +35,14 @@ public partial class RecordsForm : Form
{ {
_customers.Remove(selectedCustomer); _customers.Remove(selectedCustomer);
DatabaseHelper.DeleteCustomer(selectedCustomer); DatabaseHelper.DeleteCustomer(selectedCustomer);
UpdateCustomersList(); UpdateCustomersList(null, EventArgs.Empty);
} }
}; };
} }
public void UpdateCustomersList() public void UpdateCustomersList(object? sender, EventArgs args)
{ {
if (_customers != null && _customers.Count > 0) _customers.Clear(); if (_customers != null && _customers.Count > 0) _customers.Clear();
_customers = DatabaseHelper.RetrieveCustomers(); _customers = DatabaseHelper.RetrieveCustomers();