From 6c6de5e5056dc1e3ba5eadaf8d52d32eea120970 Mon Sep 17 00:00:00 2001 From: Spudnut2000 Date: Sat, 14 Jun 2025 22:19:24 -0500 Subject: [PATCH] RetrieveAddress helper method --- C969Project/AddUpdateCustomerForm.cs | 12 ++++++--- C969Project/Data/DatabaseHelper.cs | 39 ++++++++++++++++++++++++++++ C969Project/RecordsForm.cs | 1 + 3 files changed, 49 insertions(+), 3 deletions(-) diff --git a/C969Project/AddUpdateCustomerForm.cs b/C969Project/AddUpdateCustomerForm.cs index 45b0dc6..9da43a2 100644 --- a/C969Project/AddUpdateCustomerForm.cs +++ b/C969Project/AddUpdateCustomerForm.cs @@ -1,14 +1,18 @@ +using C969Project.Data.Models; + namespace C969Project; public partial class AddUpdateCustomerForm : Form { public enum OperationType { Add, Update } - public AddUpdateCustomerForm(OperationType operationType) + public AddUpdateCustomerForm(OperationType operationType, Customer? customer = null) { InitializeComponent(); if (operationType == OperationType.Add) InitAdd(); - else if (operationType == OperationType.Update) InitUpdate(); + else if (operationType == OperationType.Update && customer is not null) InitUpdate(customer); + else Close(); + } private void InitAdd() @@ -17,10 +21,12 @@ public partial class AddUpdateCustomerForm : Form label1.Text = "Add Customer"; } - private void InitUpdate() + private void InitUpdate(Customer customer) { Text = "Update Customer"; label1.Text = "Update Customer"; + + nameTextBox.Text = customer.CustomerName; } // name, {address, phone} diff --git a/C969Project/Data/DatabaseHelper.cs b/C969Project/Data/DatabaseHelper.cs index e0f4e74..402aaab 100644 --- a/C969Project/Data/DatabaseHelper.cs +++ b/C969Project/Data/DatabaseHelper.cs @@ -115,4 +115,43 @@ public static class DatabaseHelper throw; } } + + public static Address? RetrieveAddress(int addressId) + { + 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.address WHERE addressId = {addressId}"; + using MySqlCommand command = new MySqlCommand(query, connection); + using MySqlDataReader reader = command.ExecuteReader(); + + while (reader.Read()) + { + var addr = new Address() + { + Id = reader.GetInt32("addressId"), + Address1 = reader.GetString("address1"), + 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"), + }; + + return addr; + } + + return null; + } } \ No newline at end of file diff --git a/C969Project/RecordsForm.cs b/C969Project/RecordsForm.cs index fee2d15..9a7c860 100644 --- a/C969Project/RecordsForm.cs +++ b/C969Project/RecordsForm.cs @@ -13,6 +13,7 @@ public partial class RecordsForm : Form UpdateCustomersList(); AddButton.Click += (sender, args) => { ShowDialog(new AddUpdateCustomerForm(AddUpdateCustomerForm.OperationType.Add)); }; + ModifyButton.Click += (sender, args) => { ShowDialog(new AddUpdateCustomerForm(AddUpdateCustomerForm.OperationType.Update)); }; } public void UpdateCustomersList()