ability to add and update customers
This commit is contained in:
parent
a6da14e24f
commit
d26498cd3c
25
C969Project/AddUpdateCustomerForm.Designer.cs
generated
25
C969Project/AddUpdateCustomerForm.Designer.cs
generated
@ -45,6 +45,8 @@ partial class AddUpdateCustomerForm
|
||||
cityTextBox = new System.Windows.Forms.TextBox();
|
||||
label7 = new System.Windows.Forms.Label();
|
||||
zipTextBox = new System.Windows.Forms.TextBox();
|
||||
label8 = new System.Windows.Forms.Label();
|
||||
textBox1 = new System.Windows.Forms.TextBox();
|
||||
SuspendLayout();
|
||||
//
|
||||
// label1
|
||||
@ -170,12 +172,32 @@ partial class AddUpdateCustomerForm
|
||||
zipTextBox.Size = new System.Drawing.Size(218, 23);
|
||||
zipTextBox.TabIndex = 12;
|
||||
//
|
||||
// label8
|
||||
//
|
||||
label8.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)0));
|
||||
label8.Location = new System.Drawing.Point(236, 299);
|
||||
label8.Name = "label8";
|
||||
label8.Size = new System.Drawing.Size(63, 23);
|
||||
label8.TabIndex = 15;
|
||||
label8.Text = "Country";
|
||||
label8.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
|
||||
//
|
||||
// textBox1
|
||||
//
|
||||
textBox1.Location = new System.Drawing.Point(12, 299);
|
||||
textBox1.Name = "textBox1";
|
||||
textBox1.Size = new System.Drawing.Size(218, 23);
|
||||
textBox1.TabIndex = 14;
|
||||
textBox1.Text = "US";
|
||||
//
|
||||
// AddUpdateCustomerForm
|
||||
//
|
||||
AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
|
||||
AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
|
||||
BackColor = System.Drawing.SystemColors.Control;
|
||||
ClientSize = new System.Drawing.Size(800, 450);
|
||||
Controls.Add(label8);
|
||||
Controls.Add(textBox1);
|
||||
Controls.Add(label7);
|
||||
Controls.Add(zipTextBox);
|
||||
Controls.Add(label6);
|
||||
@ -196,6 +218,9 @@ partial class AddUpdateCustomerForm
|
||||
PerformLayout();
|
||||
}
|
||||
|
||||
private System.Windows.Forms.Label label8;
|
||||
private System.Windows.Forms.TextBox textBox1;
|
||||
|
||||
private System.Windows.Forms.Label label6;
|
||||
private System.Windows.Forms.TextBox cityTextBox;
|
||||
private System.Windows.Forms.Label label7;
|
||||
|
@ -1,13 +1,19 @@
|
||||
using System;
|
||||
using System.Windows.Forms;
|
||||
using C969Project.Data;
|
||||
using C969Project.Data.Models;
|
||||
|
||||
namespace C969Project;
|
||||
|
||||
public partial class AddUpdateCustomerForm : Form
|
||||
namespace C969Project
|
||||
{
|
||||
public partial class AddUpdateCustomerForm : Form
|
||||
{
|
||||
private Customer? _currentCustomer;
|
||||
|
||||
public AddUpdateCustomerForm()
|
||||
{
|
||||
InitializeComponent();
|
||||
saveButton.Click += SaveButton_Click;
|
||||
cancelButton.Click += CancelButton_Click;
|
||||
}
|
||||
|
||||
public void InitAdd()
|
||||
@ -20,6 +26,7 @@ public partial class AddUpdateCustomerForm : Form
|
||||
cityTextBox.Text = string.Empty;
|
||||
zipTextBox.Text = string.Empty;
|
||||
phoneTextBox.Text = string.Empty;
|
||||
_currentCustomer = null;
|
||||
|
||||
ShowDialog();
|
||||
}
|
||||
@ -29,16 +36,159 @@ public partial class AddUpdateCustomerForm : Form
|
||||
Text = "Update Customer";
|
||||
label1.Text = "Update Customer";
|
||||
|
||||
_currentCustomer = customer;
|
||||
|
||||
nameTextBox.Text = customer.CustomerName;
|
||||
var addr = DatabaseHelper.RetrieveAddress(customer.AddressId);
|
||||
if (addr != null)
|
||||
{
|
||||
addressTextBox.Text = addr.Address1;
|
||||
var city = DatabaseHelper.RetrieveCity(addr.CityId);
|
||||
if (city != null)
|
||||
{
|
||||
cityTextBox.Text = city.CityName;
|
||||
}
|
||||
zipTextBox.Text = addr.PostalCode;
|
||||
phoneTextBox.Text = addr.Phone;
|
||||
}
|
||||
|
||||
ShowDialog();
|
||||
}
|
||||
|
||||
// name, {address, phone}
|
||||
private void SaveButton_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(nameTextBox.Text) ||
|
||||
string.IsNullOrWhiteSpace(addressTextBox.Text) ||
|
||||
string.IsNullOrWhiteSpace(cityTextBox.Text) ||
|
||||
string.IsNullOrWhiteSpace(zipTextBox.Text) ||
|
||||
string.IsNullOrWhiteSpace(phoneTextBox.Text))
|
||||
{
|
||||
MessageBox.Show("All fields must be filled out.", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!System.Text.RegularExpressions.Regex.IsMatch(phoneTextBox.Text.Trim(), @"^[\d-]+$"))
|
||||
{
|
||||
MessageBox.Show("Phone number can only contain digits and dashes.", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
string currentUser = AppState.CurrentUser.Username;
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
string countryName = "US";
|
||||
int currentCountryId = DatabaseHelper.GetCountryId(countryName);
|
||||
if (currentCountryId == 0)
|
||||
{
|
||||
Country newCountry = new Country
|
||||
{
|
||||
CountryName = countryName,
|
||||
CreateDate = DateTime.UtcNow,
|
||||
CreatedBy = currentUser,
|
||||
LastUpdate = DateTime.UtcNow,
|
||||
LastUpdateBy = currentUser
|
||||
};
|
||||
currentCountryId = DatabaseHelper.AddCountry(newCountry);
|
||||
}
|
||||
|
||||
int currentCityId = DatabaseHelper.GetCityId(cityTextBox.Text.Trim(), currentCountryId);
|
||||
if (currentCityId == 0)
|
||||
{
|
||||
City newCity = new City
|
||||
{
|
||||
CityName = cityTextBox.Text.Trim(),
|
||||
CountryID = currentCountryId,
|
||||
CreateDate = DateTime.UtcNow,
|
||||
CreatedBy = currentUser,
|
||||
LastUpdate = DateTime.UtcNow,
|
||||
LastUpdateBy = currentUser
|
||||
};
|
||||
currentCityId = DatabaseHelper.AddCity(newCity);
|
||||
}
|
||||
|
||||
Address addressToSave;
|
||||
int finalAddressId;
|
||||
|
||||
if (Text == "Add Customer")
|
||||
{
|
||||
addressToSave = new Address
|
||||
{
|
||||
Address1 = addressTextBox.Text.Trim(),
|
||||
Address2 = "",
|
||||
CityId = currentCityId,
|
||||
PostalCode = zipTextBox.Text.Trim(),
|
||||
Phone = phoneTextBox.Text.Trim(),
|
||||
CreateDate = DateTime.UtcNow,
|
||||
CreatedBy = currentUser,
|
||||
LastUpdate = DateTime.UtcNow,
|
||||
LastUpdateBy = currentUser
|
||||
};
|
||||
finalAddressId = DatabaseHelper.AddAddress(addressToSave);
|
||||
}
|
||||
else
|
||||
{
|
||||
Address? existingAddress = DatabaseHelper.RetrieveAddress(_currentCustomer.AddressId);
|
||||
if (existingAddress == null)
|
||||
{
|
||||
MessageBox.Show("Could not find existing address for update.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
return;
|
||||
}
|
||||
|
||||
addressToSave = existingAddress;
|
||||
addressToSave.Address1 = addressTextBox.Text.Trim();
|
||||
addressToSave.Address2 = "";
|
||||
addressToSave.CityId = currentCityId;
|
||||
addressToSave.PostalCode = zipTextBox.Text.Trim();
|
||||
addressToSave.Phone = phoneTextBox.Text.Trim();
|
||||
addressToSave.LastUpdateBy = currentUser;
|
||||
|
||||
DatabaseHelper.UpdateAddress(addressToSave);
|
||||
finalAddressId = addressToSave.Id;
|
||||
}
|
||||
|
||||
Customer customerToSave;
|
||||
if (Text == "Add Customer")
|
||||
{
|
||||
customerToSave = new Customer
|
||||
{
|
||||
CustomerName = nameTextBox.Text.Trim(),
|
||||
AddressId = finalAddressId,
|
||||
Active = 1,
|
||||
CreateDate = DateTime.UtcNow,
|
||||
CreatedBy = currentUser,
|
||||
LastUpdate = DateTime.UtcNow,
|
||||
LastUpdateBy = currentUser
|
||||
};
|
||||
DatabaseHelper.AddCustomer(customerToSave);
|
||||
}
|
||||
else
|
||||
{
|
||||
customerToSave = _currentCustomer;
|
||||
customerToSave.CustomerName = nameTextBox.Text.Trim();
|
||||
customerToSave.AddressId = finalAddressId;
|
||||
customerToSave.LastUpdateBy = currentUser;
|
||||
|
||||
DatabaseHelper.UpdateCustomer(customerToSave);
|
||||
}
|
||||
|
||||
MessageBox.Show("Customer saved successfully!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||
DialogResult = DialogResult.OK;
|
||||
Close();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
|
||||
private void CancelButton_Click(object sender, EventArgs e)
|
||||
{
|
||||
DialogResult = DialogResult.Cancel;
|
||||
Close();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,15 +1,17 @@
|
||||
using C969Project.Data.Models;
|
||||
using System;
|
||||
using System.Data; // For CommandType
|
||||
using MySql.Data.MySqlClient;
|
||||
using C969Project.Data.Models;
|
||||
|
||||
namespace C969Project.Data;
|
||||
|
||||
public static class DatabaseHelper
|
||||
namespace C969Project.Data
|
||||
{
|
||||
|
||||
public static class DatabaseHelper
|
||||
{
|
||||
private static string ConnectionString => AppSettings.GetSetting("ConnectionStrings", "DefaultConnection");
|
||||
|
||||
public static void Initialize()
|
||||
{
|
||||
|
||||
// Initialization logic if any
|
||||
}
|
||||
|
||||
public static User? Login(string username, string password)
|
||||
@ -82,29 +84,39 @@ public static class DatabaseHelper
|
||||
return customers;
|
||||
}
|
||||
|
||||
public static void AddCustomer(Customer customer)
|
||||
/// <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)
|
||||
{
|
||||
int customerId = 0;
|
||||
using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection"));
|
||||
try
|
||||
{
|
||||
connection.Open();
|
||||
|
||||
// Removed customerId from INSERT statement as it's auto-incremented
|
||||
string query =
|
||||
"INSERT INTO customer (customerId, customerName, addressId, active, createDate, createdBy, lastUpdate, lastUpdateBy) " +
|
||||
"VALUES (@customerId, @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
|
||||
"SELECT LAST_INSERT_ID();"; // Get the newly generated ID
|
||||
|
||||
using MySqlCommand command = new MySqlCommand(query, connection);
|
||||
|
||||
command.Parameters.AddWithValue("@customerId", customer.CustomerId);
|
||||
command.Parameters.AddWithValue("@customerName", customer.CustomerName);
|
||||
command.Parameters.AddWithValue("@addressId", customer.AddressId);
|
||||
command.Parameters.AddWithValue("@active", customer.Active);
|
||||
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.Parameters.AddWithValue("@lastUpdateBy", AppState.CurrentUser.Username); // lastUpdateBy is updated manually
|
||||
|
||||
command.ExecuteNonQuery();
|
||||
object result = command.ExecuteScalar();
|
||||
if (result != null && result != DBNull.Value)
|
||||
{
|
||||
customerId = Convert.ToInt32(result);
|
||||
}
|
||||
}
|
||||
catch (MySqlException e)
|
||||
{
|
||||
@ -116,6 +128,7 @@ public static class DatabaseHelper
|
||||
Console.WriteLine($"General Error: {ex.Message}");
|
||||
throw;
|
||||
}
|
||||
return customerId;
|
||||
}
|
||||
|
||||
public static void UpdateCustomer(Customer customer)
|
||||
@ -127,15 +140,14 @@ public static class DatabaseHelper
|
||||
|
||||
string query =
|
||||
"UPDATE customer SET customerName = @customerName, addressId = @addressId, active = @active, " +
|
||||
"lastUpdate = @lastUpdate, lastUpdateBy = @lastUpdateBy WHERE customerId = @customerId";
|
||||
"lastUpdate = CURRENT_TIMESTAMP, lastUpdateBy = @lastUpdateBy WHERE customerId = @customerId"; // Use CURRENT_TIMESTAMP for lastUpdate
|
||||
|
||||
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("@lastUpdateBy", AppState.CurrentUser.Username); // lastUpdateBy is updated manually
|
||||
command.Parameters.AddWithValue("@customerId", customer.CustomerId);
|
||||
|
||||
command.ExecuteNonQuery();
|
||||
@ -260,20 +272,27 @@ public static class DatabaseHelper
|
||||
return addresses;
|
||||
}
|
||||
|
||||
public static void AddAddress(Address address)
|
||||
/// <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)
|
||||
{
|
||||
int addressId = 0;
|
||||
using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection"));
|
||||
try
|
||||
{
|
||||
connection.Open();
|
||||
|
||||
// Removed addressId from INSERT statement as it's auto-incremented
|
||||
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)";
|
||||
"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
|
||||
"SELECT LAST_INSERT_ID();"; // Get the newly generated ID
|
||||
|
||||
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);
|
||||
@ -281,10 +300,13 @@ public static class DatabaseHelper
|
||||
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();
|
||||
object result = command.ExecuteScalar();
|
||||
if (result != null && result != DBNull.Value)
|
||||
{
|
||||
addressId = Convert.ToInt32(result);
|
||||
}
|
||||
}
|
||||
catch (MySqlException e)
|
||||
{
|
||||
@ -296,6 +318,7 @@ public static class DatabaseHelper
|
||||
Console.WriteLine($"General Error: {ex.Message}");
|
||||
throw;
|
||||
}
|
||||
return addressId;
|
||||
}
|
||||
|
||||
public static void UpdateAddress(Address address)
|
||||
@ -307,7 +330,7 @@ public static class DatabaseHelper
|
||||
|
||||
string query =
|
||||
"UPDATE address SET address = @address1, address2 = @address2, cityId = @cityId, postalCode = @postalCode, " +
|
||||
"phone = @phone, lastUpdate = @lastUpdate, lastUpdateBy = @lastUpdateBy WHERE addressId = @addressId";
|
||||
"phone = @phone, lastUpdate = CURRENT_TIMESTAMP, lastUpdateBy = @lastUpdateBy WHERE addressId = @addressId"; // Use CURRENT_TIMESTAMP for lastUpdate
|
||||
|
||||
using MySqlCommand command = new MySqlCommand(query, connection);
|
||||
|
||||
@ -316,7 +339,6 @@ public static class DatabaseHelper
|
||||
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);
|
||||
|
||||
@ -639,28 +661,62 @@ public static class DatabaseHelper
|
||||
return cities;
|
||||
}
|
||||
|
||||
public static void AddCity(City city)
|
||||
public static int GetCityId(string cityName, int countryId)
|
||||
{
|
||||
int cityId = 0;
|
||||
try
|
||||
{
|
||||
using (MySqlConnection connection = new MySqlConnection(ConnectionString))
|
||||
{
|
||||
connection.Open();
|
||||
string query = "SELECT cityId FROM city WHERE city = @cityName AND countryId = @countryId";
|
||||
using (MySqlCommand cmd = new MySqlCommand(query, connection))
|
||||
{
|
||||
cmd.Parameters.AddWithValue("@cityName", cityName);
|
||||
cmd.Parameters.AddWithValue("@countryId", countryId);
|
||||
object result = cmd.ExecuteScalar();
|
||||
if (result != null && result != DBNull.Value)
|
||||
{
|
||||
cityId = Convert.ToInt32(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
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}");
|
||||
}
|
||||
return cityId;
|
||||
}
|
||||
|
||||
public static int AddCity(City city)
|
||||
{
|
||||
int cityId = 0;
|
||||
using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection"));
|
||||
try
|
||||
{
|
||||
connection.Open();
|
||||
|
||||
// Removed cityId from INSERT statement as it's auto-incremented
|
||||
string query =
|
||||
"INSERT INTO city (cityId, city, countryId, createDate, createdBy, lastUpdate, lastUpdateBy) " +
|
||||
"VALUES (@cityId, @cityName, @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
|
||||
"SELECT LAST_INSERT_ID();"; // Get the newly generated ID
|
||||
|
||||
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();
|
||||
object result = command.ExecuteScalar();
|
||||
if (result != null && result != DBNull.Value)
|
||||
{
|
||||
cityId = Convert.ToInt32(result);
|
||||
}
|
||||
}
|
||||
catch (MySqlException e)
|
||||
{
|
||||
@ -672,6 +728,7 @@ public static class DatabaseHelper
|
||||
Console.WriteLine($"General Error: {ex.Message}");
|
||||
throw;
|
||||
}
|
||||
return cityId;
|
||||
}
|
||||
|
||||
public static void UpdateCity(City city)
|
||||
@ -683,13 +740,12 @@ public static class DatabaseHelper
|
||||
|
||||
string query =
|
||||
"UPDATE city SET city = @cityName, countryId = @countryId, " +
|
||||
"lastUpdate = @lastUpdate, lastUpdateBy = @lastUpdateBy WHERE cityId = @cityId";
|
||||
"lastUpdate = CURRENT_TIMESTAMP, lastUpdateBy = @lastUpdateBy WHERE cityId = @cityId"; // Use CURRENT_TIMESTAMP for lastUpdate
|
||||
|
||||
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);
|
||||
|
||||
@ -807,27 +863,60 @@ public static class DatabaseHelper
|
||||
return countries;
|
||||
}
|
||||
|
||||
public static void AddCountry(Country country)
|
||||
public static int GetCountryId(string countryName)
|
||||
{
|
||||
int countryId = 0;
|
||||
try
|
||||
{
|
||||
using (MySqlConnection connection = new MySqlConnection(ConnectionString))
|
||||
{
|
||||
connection.Open();
|
||||
string query = "SELECT countryId FROM country WHERE country = @countryName";
|
||||
using (MySqlCommand cmd = new MySqlCommand(query, connection))
|
||||
{
|
||||
cmd.Parameters.AddWithValue("@countryName", countryName);
|
||||
object result = cmd.ExecuteScalar();
|
||||
if (result != null && result != DBNull.Value)
|
||||
{
|
||||
countryId = Convert.ToInt32(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
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}");
|
||||
}
|
||||
return countryId;
|
||||
}
|
||||
|
||||
public static int AddCountry(Country country)
|
||||
{
|
||||
int countryId = 0;
|
||||
using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection"));
|
||||
try
|
||||
{
|
||||
connection.Open();
|
||||
|
||||
// Removed countryId from INSERT statement as it's auto-incremented
|
||||
string query =
|
||||
"INSERT INTO country (countryId, country, createDate, createdBy, lastUpdate, lastUpdateBy) " +
|
||||
"VALUES (@countryId, @countryName, @createDate, @createdBy, @lastUpdate, @lastUpdateBy)";
|
||||
"INSERT INTO country (country, createDate, createdBy, lastUpdate, lastUpdateBy) " +
|
||||
"VALUES (@countryName, @createDate, @createdBy, CURRENT_TIMESTAMP, @lastUpdateBy); " + // Use CURRENT_TIMESTAMP for lastUpdate
|
||||
"SELECT LAST_INSERT_ID();"; // Get the newly generated ID
|
||||
|
||||
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();
|
||||
object result = command.ExecuteScalar();
|
||||
if (result != null && result != DBNull.Value)
|
||||
{
|
||||
countryId = Convert.ToInt32(result);
|
||||
}
|
||||
}
|
||||
catch (MySqlException e)
|
||||
{
|
||||
@ -839,6 +928,7 @@ public static class DatabaseHelper
|
||||
Console.WriteLine($"General Error: {ex.Message}");
|
||||
throw;
|
||||
}
|
||||
return countryId;
|
||||
}
|
||||
|
||||
public static void UpdateCountry(Country country)
|
||||
@ -850,12 +940,11 @@ public static class DatabaseHelper
|
||||
|
||||
string query =
|
||||
"UPDATE country SET country = @countryName, " +
|
||||
"lastUpdate = @lastUpdate, lastUpdateBy = @lastUpdateBy WHERE countryId = @countryId";
|
||||
"lastUpdate = CURRENT_TIMESTAMP, lastUpdateBy = @lastUpdateBy WHERE countryId = @countryId"; // Use CURRENT_TIMESTAMP for lastUpdate
|
||||
|
||||
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);
|
||||
|
||||
@ -900,4 +989,5 @@ public static class DatabaseHelper
|
||||
{
|
||||
DeleteCountry(country.CountryID);
|
||||
}
|
||||
}
|
||||
}
|
@ -16,12 +16,14 @@ public partial class RecordsForm : Form
|
||||
AddButton.Click += (sender, args) =>
|
||||
{
|
||||
_form.InitAdd();
|
||||
_form.Closed += (o, args2) => { UpdateCustomersList(); };
|
||||
};
|
||||
|
||||
ModifyButton.Click += (sender, args) =>
|
||||
{
|
||||
var selectedCustomer = _customers[CustomersDataGrid.CurrentCell.RowIndex];
|
||||
_form.InitUpdate(selectedCustomer);
|
||||
_form.Closed += (o, args2) => { UpdateCustomersList(); };
|
||||
};
|
||||
|
||||
DeleteButton.Click += (sender, args) =>
|
||||
|
Loading…
Reference in New Issue
Block a user