diff --git a/C969Project/AddUpdateCustomerForm.cs b/C969Project/AddUpdateCustomerForm.cs
index 9da43a2..dfd4ea0 100644
--- a/C969Project/AddUpdateCustomerForm.cs
+++ b/C969Project/AddUpdateCustomerForm.cs
@@ -1,32 +1,43 @@
+using C969Project.Data;
using C969Project.Data.Models;
namespace C969Project;
public partial class AddUpdateCustomerForm : Form
{
- public enum OperationType { Add, Update }
- public AddUpdateCustomerForm(OperationType operationType, Customer? customer = null)
+ public AddUpdateCustomerForm()
{
InitializeComponent();
-
- if (operationType == OperationType.Add) InitAdd();
- else if (operationType == OperationType.Update && customer is not null) InitUpdate(customer);
- else Close();
-
}
- private void InitAdd()
+ public void InitAdd()
{
Text = "Add Customer";
label1.Text = "Add Customer";
+
+ nameTextBox.Text = string.Empty;
+ addressTextBox.Text = string.Empty;
+ cityTextBox.Text = string.Empty;
+ zipTextBox.Text = string.Empty;
+ phoneTextBox.Text = string.Empty;
+
+ ShowDialog();
}
- private void InitUpdate(Customer customer)
+ public void InitUpdate(Customer customer)
{
Text = "Update Customer";
label1.Text = "Update Customer";
nameTextBox.Text = customer.CustomerName;
+ var addr = DatabaseHelper.RetrieveAddress(customer.AddressId);
+ addressTextBox.Text = addr.Address1;
+ var city = DatabaseHelper.RetrieveCity(addr.CityId);
+ cityTextBox.Text = city.CityName;
+ zipTextBox.Text = addr.PostalCode;
+ phoneTextBox.Text = addr.Phone;
+
+ ShowDialog();
}
// name, {address, phone}
diff --git a/C969Project/Data/DatabaseHelper.cs b/C969Project/Data/DatabaseHelper.cs
index 402aaab..ddea8a4 100644
--- a/C969Project/Data/DatabaseHelper.cs
+++ b/C969Project/Data/DatabaseHelper.cs
@@ -24,8 +24,10 @@ public static class DatabaseHelper
Console.WriteLine(ex.Message);
}
- string query = $"SELECT * FROM client_schedule.user WHERE userName = '{username}'";
+ string query = "SELECT * FROM client_schedule.user WHERE userName = @username";
using MySqlCommand command = new MySqlCommand(query, connection);
+ command.Parameters.AddWithValue("@username", username);
+
using MySqlDataReader reader = command.ExecuteReader();
while (reader.Read())
@@ -102,7 +104,7 @@ public static class DatabaseHelper
command.Parameters.AddWithValue("@lastUpdate", DateTime.UtcNow);
command.Parameters.AddWithValue("@lastUpdateBy", AppState.CurrentUser.Username);
- int rowsAffected = command.ExecuteNonQuery();
+ command.ExecuteNonQuery();
}
catch (MySqlException e)
{
@@ -115,6 +117,68 @@ public static class DatabaseHelper
throw;
}
}
+
+ public static void UpdateCustomer(Customer customer)
+ {
+ using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection"));
+ try
+ {
+ connection.Open();
+
+ string query =
+ "UPDATE customer SET customerName = @customerName, addressId = @addressId, active = @active, " +
+ "lastUpdate = @lastUpdate, lastUpdateBy = @lastUpdateBy WHERE customerId = @customerId";
+
+ using MySqlCommand command = new MySqlCommand(query, connection);
+
+ command.Parameters.AddWithValue("@customerName", customer.CustomerName);
+ command.Parameters.AddWithValue("@addressId", customer.AddressId);
+ command.Parameters.AddWithValue("@active", customer.Active);
+ command.Parameters.AddWithValue("@lastUpdate", DateTime.UtcNow);
+ command.Parameters.AddWithValue("@lastUpdateBy", AppState.CurrentUser.Username);
+ command.Parameters.AddWithValue("@customerId", customer.CustomerId);
+
+ command.ExecuteNonQuery();
+ }
+ catch (MySqlException e)
+ {
+ Console.WriteLine($"MySQL Error: {e.Message}");
+ throw;
+ }
+ catch (Exception ex)
+ {
+ Console.WriteLine($"General Error: {ex.Message}");
+ throw;
+ }
+ }
+
+ public static void DeleteCustomer(int customerId)
+ {
+ using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection"));
+ try
+ {
+ connection.Open();
+ string query = "DELETE FROM customer WHERE customerId = @customerId";
+ using MySqlCommand command = new MySqlCommand(query, connection);
+ command.Parameters.AddWithValue("@customerId", customerId);
+ command.ExecuteNonQuery();
+ }
+ catch (MySqlException e)
+ {
+ Console.WriteLine($"MySQL Error: {e.Message}");
+ throw;
+ }
+ catch (Exception ex)
+ {
+ Console.WriteLine($"General Error: {ex.Message}");
+ throw;
+ }
+ }
+
+ public static void DeleteCustomer(Customer customer)
+ {
+ DeleteCustomer(customer.CustomerId);
+ }
public static Address? RetrieveAddress(int addressId)
{
@@ -129,8 +193,10 @@ public static class DatabaseHelper
throw;
}
- string query = $"SELECT * FROM client_schedule.address WHERE addressId = {addressId}";
+ string query = "SELECT * FROM client_schedule.address WHERE addressId = @addressId";
using MySqlCommand command = new MySqlCommand(query, connection);
+ command.Parameters.AddWithValue("@addressId", addressId);
+
using MySqlDataReader reader = command.ExecuteReader();
while (reader.Read())
@@ -138,7 +204,7 @@ public static class DatabaseHelper
var addr = new Address()
{
Id = reader.GetInt32("addressId"),
- Address1 = reader.GetString("address1"),
+ Address1 = reader.GetString("address"),
Address2 = reader.GetString("address2"),
CityId = reader.GetInt32("cityId"),
PostalCode = reader.GetString("postalCode"),
@@ -154,4 +220,684 @@ public static class DatabaseHelper
return null;
}
+
+ public static List
RetrieveAllAddresses()
+ {
+ using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection"));
+ try
+ {
+ connection.Open();
+ }
+ catch (MySqlException e)
+ {
+ Console.WriteLine(e);
+ throw;
+ }
+
+ List addresses = new List();
+
+ using MySqlCommand command = new MySqlCommand("SELECT * FROM client_schedule.address", connection);
+ using MySqlDataReader reader = command.ExecuteReader();
+ while (reader.Read())
+ {
+ var addr = new Address()
+ {
+ Id = reader.GetInt32("addressId"),
+ Address1 = reader.GetString("address"),
+ Address2 = reader.GetString("address2"),
+ CityId = reader.GetInt32("cityId"),
+ PostalCode = reader.GetString("postalCode"),
+ Phone = reader.GetString("phone"),
+ CreateDate = reader.GetDateTime("createDate"),
+ CreatedBy = reader.GetString("createdBy"),
+ LastUpdate = reader.GetDateTime("lastUpdate"),
+ LastUpdateBy = reader.GetString("lastUpdateBy"),
+ };
+
+ addresses.Add(addr);
+ }
+
+ return addresses;
+ }
+
+ public static void AddAddress(Address address)
+ {
+ using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection"));
+ try
+ {
+ connection.Open();
+
+ string query =
+ "INSERT INTO address (addressId, address, address2, cityId, postalCode, phone, createDate, createdBy, lastUpdate, lastUpdateBy) " +
+ "VALUES (@addressId, @address1, @address2, @cityId, @postalCode, @phone, @createDate, @createdBy, @lastUpdate, @lastUpdateBy)";
+
+ using MySqlCommand command = new MySqlCommand(query, connection);
+
+ command.Parameters.AddWithValue("@addressId", address.Id);
+ command.Parameters.AddWithValue("@address1", address.Address1);
+ command.Parameters.AddWithValue("@address2", address.Address2);
+ command.Parameters.AddWithValue("@cityId", address.CityId);
+ command.Parameters.AddWithValue("@postalCode", address.PostalCode);
+ command.Parameters.AddWithValue("@phone", address.Phone);
+ command.Parameters.AddWithValue("@createDate", DateTime.UtcNow);
+ command.Parameters.AddWithValue("@createdBy", AppState.CurrentUser.Username);
+ command.Parameters.AddWithValue("@lastUpdate", DateTime.UtcNow);
+ command.Parameters.AddWithValue("@lastUpdateBy", AppState.CurrentUser.Username);
+
+ command.ExecuteNonQuery();
+ }
+ catch (MySqlException e)
+ {
+ Console.WriteLine($"MySQL Error: {e.Message}");
+ throw;
+ }
+ catch (Exception ex)
+ {
+ Console.WriteLine($"General Error: {ex.Message}");
+ throw;
+ }
+ }
+
+ public static void UpdateAddress(Address address)
+ {
+ using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection"));
+ try
+ {
+ connection.Open();
+
+ string query =
+ "UPDATE address SET address = @address1, address2 = @address2, cityId = @cityId, postalCode = @postalCode, " +
+ "phone = @phone, lastUpdate = @lastUpdate, lastUpdateBy = @lastUpdateBy WHERE addressId = @addressId";
+
+ using MySqlCommand command = new MySqlCommand(query, connection);
+
+ command.Parameters.AddWithValue("@address1", address.Address1);
+ command.Parameters.AddWithValue("@address2", address.Address2);
+ command.Parameters.AddWithValue("@cityId", address.CityId);
+ command.Parameters.AddWithValue("@postalCode", address.PostalCode);
+ command.Parameters.AddWithValue("@phone", address.Phone);
+ command.Parameters.AddWithValue("@lastUpdate", DateTime.UtcNow);
+ command.Parameters.AddWithValue("@lastUpdateBy", AppState.CurrentUser.Username);
+ command.Parameters.AddWithValue("@addressId", address.Id);
+
+ command.ExecuteNonQuery();
+ }
+ catch (MySqlException e)
+ {
+ Console.WriteLine($"MySQL Error: {e.Message}");
+ throw;
+ }
+ catch (Exception ex)
+ {
+ Console.WriteLine($"General Error: {ex.Message}");
+ throw;
+ }
+ }
+
+ public static void DeleteAddress(int addressId)
+ {
+ using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection"));
+ try
+ {
+ connection.Open();
+ string query = "DELETE FROM address WHERE addressId = @addressId";
+ using MySqlCommand command = new MySqlCommand(query, connection);
+ command.Parameters.AddWithValue("@addressId", addressId);
+ command.ExecuteNonQuery();
+ }
+ catch (MySqlException e)
+ {
+ Console.WriteLine($"MySQL Error: {e.Message}");
+ throw;
+ }
+ catch (Exception ex)
+ {
+ Console.WriteLine($"General Error: {ex.Message}");
+ throw;
+ }
+ }
+
+ public static void DeleteAddress(Address address)
+ {
+ DeleteAddress(address.Id);
+ }
+
+ public static Appointment? RetrieveAppointment(int appointmentId)
+ {
+ using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection"));
+ try
+ {
+ connection.Open();
+ }
+ catch (MySqlException e)
+ {
+ Console.WriteLine(e);
+ throw;
+ }
+
+ string query = "SELECT * FROM client_schedule.appointment WHERE appointmentId = @appointmentId";
+ using MySqlCommand command = new MySqlCommand(query, connection);
+ command.Parameters.AddWithValue("@appointmentId", appointmentId);
+
+ using MySqlDataReader reader = command.ExecuteReader();
+
+ while (reader.Read())
+ {
+ var appt = new Appointment()
+ {
+ AppointmentId = reader.GetInt32("appointmentId"),
+ CustomerId = reader.GetInt32("customerId"),
+ UserId = reader.GetInt32("userId"),
+ Title = reader.GetString("title"),
+ Description = reader.GetString("description"),
+ Location = reader.GetString("location"),
+ Contact = reader.GetString("contact"),
+ AppointmentType = reader.GetString("type"),
+ Url = reader.GetString("url"),
+ Start = reader.GetDateTime("start"),
+ End = reader.GetDateTime("end"),
+ CreateDate = reader.GetDateTime("createDate"),
+ CreatedBy = reader.GetString("createdBy"),
+ LastUpdate = reader.GetDateTime("lastUpdate"),
+ LastUpdateBy = reader.GetString("lastUpdateBy"),
+ };
+
+ return appt;
+ }
+
+ return null;
+ }
+
+ public static List RetrieveAllAppointments()
+ {
+ using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection"));
+ try
+ {
+ connection.Open();
+ }
+ catch (MySqlException e)
+ {
+ Console.WriteLine(e);
+ throw;
+ }
+
+ List appointments = new List();
+
+ using MySqlCommand command = new MySqlCommand("SELECT * FROM client_schedule.appointment", connection);
+ using MySqlDataReader reader = command.ExecuteReader();
+ while (reader.Read())
+ {
+ var appt = new Appointment()
+ {
+ AppointmentId = reader.GetInt32("appointmentId"),
+ CustomerId = reader.GetInt32("customerId"),
+ UserId = reader.GetInt32("userId"),
+ Title = reader.GetString("title"),
+ Description = reader.GetString("description"),
+ Location = reader.GetString("location"),
+ Contact = reader.GetString("contact"),
+ AppointmentType = reader.GetString("type"),
+ Url = reader.GetString("url"),
+ Start = reader.GetDateTime("start"),
+ End = reader.GetDateTime("end"),
+ CreateDate = reader.GetDateTime("createDate"),
+ CreatedBy = reader.GetString("createdBy"),
+ LastUpdate = reader.GetDateTime("lastUpdate"),
+ LastUpdateBy = reader.GetString("lastUpdateBy"),
+ };
+
+ appointments.Add(appt);
+ }
+
+ return appointments;
+ }
+
+ public static void AddAppointment(Appointment appointment)
+ {
+ using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection"));
+ try
+ {
+ connection.Open();
+
+ string query =
+ "INSERT INTO appointment (appointmentId, customerId, userId, title, description, location, contact, type, url, start, end, createDate, createdBy, lastUpdate, lastUpdateBy) " +
+ "VALUES (@appointmentId, @customerId, @userId, @title, @description, @location, @contact, @type, @url, @start, @end, @createDate, @createdBy, @lastUpdate, @lastUpdateBy)";
+
+ using MySqlCommand command = new MySqlCommand(query, connection);
+
+ command.Parameters.AddWithValue("@appointmentId", appointment.AppointmentId);
+ command.Parameters.AddWithValue("@customerId", appointment.CustomerId);
+ command.Parameters.AddWithValue("@userId", appointment.UserId);
+ command.Parameters.AddWithValue("@title", appointment.Title);
+ command.Parameters.AddWithValue("@description", appointment.Description);
+ command.Parameters.AddWithValue("@location", appointment.Location);
+ command.Parameters.AddWithValue("@contact", appointment.Contact);
+ command.Parameters.AddWithValue("@type", appointment.AppointmentType);
+ command.Parameters.AddWithValue("@url", appointment.Url);
+ command.Parameters.AddWithValue("@start", appointment.Start);
+ command.Parameters.AddWithValue("@end", appointment.End);
+ command.Parameters.AddWithValue("@createDate", DateTime.UtcNow);
+ command.Parameters.AddWithValue("@createdBy", AppState.CurrentUser.Username);
+ command.Parameters.AddWithValue("@lastUpdate", DateTime.UtcNow);
+ command.Parameters.AddWithValue("@lastUpdateBy", AppState.CurrentUser.Username);
+
+ command.ExecuteNonQuery();
+ }
+ catch (MySqlException e)
+ {
+ Console.WriteLine($"MySQL Error: {e.Message}");
+ throw;
+ }
+ catch (Exception ex)
+ {
+ Console.WriteLine($"General Error: {ex.Message}");
+ throw;
+ }
+ }
+
+ public static void UpdateAppointment(Appointment appointment)
+ {
+ using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection"));
+ try
+ {
+ connection.Open();
+
+ string query =
+ "UPDATE appointment SET customerId = @customerId, userId = @userId, title = @title, description = @description, " +
+ "location = @location, contact = @contact, type = @type, url = @url, start = @start, end = @end, " +
+ "lastUpdate = @lastUpdate, lastUpdateBy = @lastUpdateBy WHERE appointmentId = @appointmentId";
+
+ using MySqlCommand command = new MySqlCommand(query, connection);
+
+ command.Parameters.AddWithValue("@customerId", appointment.CustomerId);
+ command.Parameters.AddWithValue("@userId", appointment.UserId);
+ command.Parameters.AddWithValue("@title", appointment.Title);
+ command.Parameters.AddWithValue("@description", appointment.Description);
+ command.Parameters.AddWithValue("@location", appointment.Location);
+ command.Parameters.AddWithValue("@contact", appointment.Contact);
+ command.Parameters.AddWithValue("@type", appointment.AppointmentType);
+ command.Parameters.AddWithValue("@url", appointment.Url);
+ command.Parameters.AddWithValue("@start", appointment.Start);
+ command.Parameters.AddWithValue("@end", appointment.End);
+ command.Parameters.AddWithValue("@lastUpdate", DateTime.UtcNow);
+ command.Parameters.AddWithValue("@lastUpdateBy", AppState.CurrentUser.Username);
+ command.Parameters.AddWithValue("@appointmentId", appointment.AppointmentId);
+
+ command.ExecuteNonQuery();
+ }
+ catch (MySqlException e)
+ {
+ Console.WriteLine($"MySQL Error: {e.Message}");
+ throw;
+ }
+ catch (Exception ex)
+ {
+ Console.WriteLine($"General Error: {ex.Message}");
+ throw;
+ }
+ }
+
+ public static void DeleteAppointment(int appointmentId)
+ {
+ using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection"));
+ try
+ {
+ connection.Open();
+ string query = "DELETE FROM appointment WHERE appointmentId = @appointmentId";
+ using MySqlCommand command = new MySqlCommand(query, connection);
+ command.Parameters.AddWithValue("@appointmentId", appointmentId);
+ command.ExecuteNonQuery();
+ }
+ catch (MySqlException e)
+ {
+ Console.WriteLine($"MySQL Error: {e.Message}");
+ throw;
+ }
+ catch (Exception ex)
+ {
+ Console.WriteLine($"General Error: {ex.Message}");
+ throw;
+ }
+ }
+
+ public static void DeleteAppointment(Appointment appointment)
+ {
+ DeleteAppointment(appointment.AppointmentId);
+ }
+
+ public static City? RetrieveCity(int cityId)
+ {
+ using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection"));
+ try
+ {
+ connection.Open();
+ }
+ catch (MySqlException e)
+ {
+ Console.WriteLine(e);
+ throw;
+ }
+
+ string query = "SELECT * FROM client_schedule.city WHERE cityId = @cityId";
+ using MySqlCommand command = new MySqlCommand(query, connection);
+ command.Parameters.AddWithValue("@cityId", cityId);
+
+ using MySqlDataReader reader = command.ExecuteReader();
+
+ while (reader.Read())
+ {
+ var city = new City()
+ {
+ CityID = reader.GetInt32("cityId"),
+ CityName = reader.GetString("city"),
+ CountryID = reader.GetInt32("countryId"),
+ CreateDate = reader.GetDateTime("createDate"),
+ CreatedBy = reader.GetString("createdBy"),
+ LastUpdate = reader.GetDateTime("lastUpdate"),
+ LastUpdateBy = reader.GetString("lastUpdateBy"),
+ };
+
+ return city;
+ }
+
+ return null;
+ }
+
+ public static List RetrieveAllCities()
+ {
+ using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection"));
+ try
+ {
+ connection.Open();
+ }
+ catch (MySqlException e)
+ {
+ Console.WriteLine(e);
+ throw;
+ }
+
+ List cities = new List();
+
+ using MySqlCommand command = new MySqlCommand("SELECT * FROM client_schedule.city", connection);
+ using MySqlDataReader reader = command.ExecuteReader();
+ while (reader.Read())
+ {
+ var city = new City()
+ {
+ CityID = reader.GetInt32("cityId"),
+ CityName = reader.GetString("city"),
+ CountryID = reader.GetInt32("countryId"),
+ CreateDate = reader.GetDateTime("createDate"),
+ CreatedBy = reader.GetString("createdBy"),
+ LastUpdate = reader.GetDateTime("lastUpdate"),
+ LastUpdateBy = reader.GetString("lastUpdateBy"),
+ };
+
+ cities.Add(city);
+ }
+
+ return cities;
+ }
+
+ public static void AddCity(City city)
+ {
+ using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection"));
+ try
+ {
+ connection.Open();
+
+ string query =
+ "INSERT INTO city (cityId, city, countryId, createDate, createdBy, lastUpdate, lastUpdateBy) " +
+ "VALUES (@cityId, @cityName, @countryId, @createDate, @createdBy, @lastUpdate, @lastUpdateBy)";
+
+ using MySqlCommand command = new MySqlCommand(query, connection);
+
+ command.Parameters.AddWithValue("@cityId", city.CityID);
+ command.Parameters.AddWithValue("@cityName", city.CityName);
+ command.Parameters.AddWithValue("@countryId", city.CountryID);
+ command.Parameters.AddWithValue("@createDate", DateTime.UtcNow);
+ command.Parameters.AddWithValue("@createdBy", AppState.CurrentUser.Username);
+ command.Parameters.AddWithValue("@lastUpdate", DateTime.UtcNow);
+ command.Parameters.AddWithValue("@lastUpdateBy", AppState.CurrentUser.Username);
+
+ command.ExecuteNonQuery();
+ }
+ catch (MySqlException e)
+ {
+ Console.WriteLine($"MySQL Error: {e.Message}");
+ throw;
+ }
+ catch (Exception ex)
+ {
+ Console.WriteLine($"General Error: {ex.Message}");
+ throw;
+ }
+ }
+
+ public static void UpdateCity(City city)
+ {
+ using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection"));
+ try
+ {
+ connection.Open();
+
+ string query =
+ "UPDATE city SET city = @cityName, countryId = @countryId, " +
+ "lastUpdate = @lastUpdate, lastUpdateBy = @lastUpdateBy WHERE cityId = @cityId";
+
+ using MySqlCommand command = new MySqlCommand(query, connection);
+
+ command.Parameters.AddWithValue("@cityName", city.CityName);
+ command.Parameters.AddWithValue("@countryId", city.CountryID);
+ command.Parameters.AddWithValue("@lastUpdate", DateTime.UtcNow);
+ command.Parameters.AddWithValue("@lastUpdateBy", AppState.CurrentUser.Username);
+ command.Parameters.AddWithValue("@cityId", city.CityID);
+
+ command.ExecuteNonQuery();
+ }
+ catch (MySqlException e)
+ {
+ Console.WriteLine($"MySQL Error: {e.Message}");
+ throw;
+ }
+ catch (Exception ex)
+ {
+ Console.WriteLine($"General Error: {ex.Message}");
+ throw;
+ }
+ }
+
+ public static void DeleteCity(int cityId)
+ {
+ using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection"));
+ try
+ {
+ connection.Open();
+ string query = "DELETE FROM city WHERE cityId = @cityId";
+ using MySqlCommand command = new MySqlCommand(query, connection);
+ command.Parameters.AddWithValue("@cityId", cityId);
+ command.ExecuteNonQuery();
+ }
+ catch (MySqlException e)
+ {
+ Console.WriteLine($"MySQL Error: {e.Message}");
+ throw;
+ }
+ catch (Exception ex)
+ {
+ Console.WriteLine($"General Error: {ex.Message}");
+ throw;
+ }
+ }
+
+ public static void DeleteCity(City city)
+ {
+ DeleteCity(city.CityID);
+ }
+
+ public static Country? RetrieveCountry(int countryId)
+ {
+ using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection"));
+ try
+ {
+ connection.Open();
+ }
+ catch (MySqlException e)
+ {
+ Console.WriteLine(e);
+ throw;
+ }
+
+ string query = "SELECT * FROM client_schedule.country WHERE countryId = @countryId";
+ using MySqlCommand command = new MySqlCommand(query, connection);
+ command.Parameters.AddWithValue("@countryId", countryId);
+
+ using MySqlDataReader reader = command.ExecuteReader();
+
+ while (reader.Read())
+ {
+ var country = new Country()
+ {
+ CountryID = reader.GetInt32("countryId"),
+ CountryName = reader.GetString("country"),
+ CreateDate = reader.GetDateTime("createDate"),
+ CreatedBy = reader.GetString("createdBy"),
+ LastUpdate = reader.GetDateTime("lastUpdate"),
+ LastUpdateBy = reader.GetString("lastUpdateBy"),
+ };
+
+ return country;
+ }
+
+ return null;
+ }
+
+ public static List RetrieveAllCountries()
+ {
+ using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection"));
+ try
+ {
+ connection.Open();
+ }
+ catch (MySqlException e)
+ {
+ Console.WriteLine(e);
+ throw;
+ }
+
+ List countries = new List();
+
+ using MySqlCommand command = new MySqlCommand("SELECT * FROM client_schedule.country", connection);
+ using MySqlDataReader reader = command.ExecuteReader();
+ while (reader.Read())
+ {
+ var country = new Country()
+ {
+ CountryID = reader.GetInt32("countryId"),
+ CountryName = reader.GetString("country"),
+ CreateDate = reader.GetDateTime("createDate"),
+ CreatedBy = reader.GetString("createdBy"),
+ LastUpdate = reader.GetDateTime("lastUpdate"),
+ LastUpdateBy = reader.GetString("lastUpdateBy"),
+ };
+
+ countries.Add(country);
+ }
+
+ return countries;
+ }
+
+ public static void AddCountry(Country country)
+ {
+ using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection"));
+ try
+ {
+ connection.Open();
+
+ string query =
+ "INSERT INTO country (countryId, country, createDate, createdBy, lastUpdate, lastUpdateBy) " +
+ "VALUES (@countryId, @countryName, @createDate, @createdBy, @lastUpdate, @lastUpdateBy)";
+
+ using MySqlCommand command = new MySqlCommand(query, connection);
+
+ command.Parameters.AddWithValue("@countryId", country.CountryID);
+ command.Parameters.AddWithValue("@countryName", country.CountryName);
+ command.Parameters.AddWithValue("@createDate", DateTime.UtcNow);
+ command.Parameters.AddWithValue("@createdBy", AppState.CurrentUser.Username);
+ command.Parameters.AddWithValue("@lastUpdate", DateTime.UtcNow);
+ command.Parameters.AddWithValue("@lastUpdateBy", AppState.CurrentUser.Username);
+
+ command.ExecuteNonQuery();
+ }
+ catch (MySqlException e)
+ {
+ Console.WriteLine($"MySQL Error: {e.Message}");
+ throw;
+ }
+ catch (Exception ex)
+ {
+ Console.WriteLine($"General Error: {ex.Message}");
+ throw;
+ }
+ }
+
+ public static void UpdateCountry(Country country)
+ {
+ using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection"));
+ try
+ {
+ connection.Open();
+
+ string query =
+ "UPDATE country SET country = @countryName, " +
+ "lastUpdate = @lastUpdate, lastUpdateBy = @lastUpdateBy WHERE countryId = @countryId";
+
+ using MySqlCommand command = new MySqlCommand(query, connection);
+
+ command.Parameters.AddWithValue("@countryName", country.CountryName);
+ command.Parameters.AddWithValue("@lastUpdate", DateTime.UtcNow);
+ command.Parameters.AddWithValue("@lastUpdateBy", AppState.CurrentUser.Username);
+ command.Parameters.AddWithValue("@countryId", country.CountryID);
+
+ command.ExecuteNonQuery();
+ }
+ catch (MySqlException e)
+ {
+ Console.WriteLine($"MySQL Error: {e.Message}");
+ throw;
+ }
+ catch (Exception ex)
+ {
+ Console.WriteLine($"General Error: {ex.Message}");
+ throw;
+ }
+ }
+
+ public static void DeleteCountry(int countryId)
+ {
+ using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection"));
+ try
+ {
+ connection.Open();
+ string query = "DELETE FROM country WHERE countryId = @countryId";
+ using MySqlCommand command = new MySqlCommand(query, connection);
+ command.Parameters.AddWithValue("@countryId", countryId);
+ command.ExecuteNonQuery();
+ }
+ catch (MySqlException e)
+ {
+ Console.WriteLine($"MySQL Error: {e.Message}");
+ throw;
+ }
+ catch (Exception ex)
+ {
+ Console.WriteLine($"General Error: {ex.Message}");
+ throw;
+ }
+ }
+
+ public static void DeleteCountry(Country country)
+ {
+ DeleteCountry(country.CountryID);
+ }
}
\ No newline at end of file
diff --git a/C969Project/LoginForm.cs b/C969Project/LoginForm.cs
index 45e3499..348c39c 100644
--- a/C969Project/LoginForm.cs
+++ b/C969Project/LoginForm.cs
@@ -35,7 +35,7 @@ namespace C969Project
AppState.CurrentUser = usr;
var dash = new DashboardForm();
- dash.FormClosing += (o, args) => { this.Show(); };
+ dash.FormClosing += (o, args) => { Close(); };
dash.Show();
Hide();
}
diff --git a/C969Project/RecordsForm.cs b/C969Project/RecordsForm.cs
index 9a7c860..3a37b4e 100644
--- a/C969Project/RecordsForm.cs
+++ b/C969Project/RecordsForm.cs
@@ -6,15 +6,38 @@ namespace C969Project;
public partial class RecordsForm : Form
{
private List? _customers;
+ private AddUpdateCustomerForm _form = new();
public RecordsForm()
{
InitializeComponent();
UpdateCustomersList();
- AddButton.Click += (sender, args) => { ShowDialog(new AddUpdateCustomerForm(AddUpdateCustomerForm.OperationType.Add)); };
- ModifyButton.Click += (sender, args) => { ShowDialog(new AddUpdateCustomerForm(AddUpdateCustomerForm.OperationType.Update)); };
+ AddButton.Click += (sender, args) =>
+ {
+ _form.InitAdd();
+ };
+
+ ModifyButton.Click += (sender, args) =>
+ {
+ var selectedCustomer = _customers[CustomersDataGrid.CurrentCell.RowIndex];
+ _form.InitUpdate(selectedCustomer);
+ };
+
+ DeleteButton.Click += (sender, args) =>
+ {
+ var selectedCustomer = _customers[CustomersDataGrid.CurrentCell.RowIndex];
+ var result = MessageBox.Show("Are you sure you want to delete this record?", "Delete Record", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
+ if (result == DialogResult.OK)
+ {
+ _customers.Remove(selectedCustomer);
+ DatabaseHelper.DeleteCustomer(selectedCustomer);
+ UpdateCustomersList();
+ }
+ };
}
+
+
public void UpdateCustomersList()
{