diff --git a/TestWins/Form1.cs b/TestWins/Form1.cs index fcefc88..5a3b3ab 100644 --- a/TestWins/Form1.cs +++ b/TestWins/Form1.cs @@ -1,43 +1,94 @@ -using System.Drawing.Text; using TestWins.Controller; +using TestWins.Model; -namespace TestWins; - -public partial class Form1 : Form +namespace TestWins { - //business - - private readonly StudentController controller = new StudentController(); - public Form1() + public partial class Form1 : Form { - InitializeComponent(); - loadData(); - } + private readonly StudentController controller = new StudentController(); + private DataGridViewRow? selectedRow; - private void loadData() - { - dataGridView1.DataSource = controller.getAll(); - } + public Form1() + { + InitializeComponent(); + loadData(); + } - private void btnAdd_Click(object sender, EventArgs e) - { + private void loadData() + { + dataGridView1.DataSource = controller.getAll(); + } - } + private void btnAdd_Click(object sender, EventArgs e) + { + if (!ValidateFields()) return; + var success = controller.add(GetStudent()); + ShowResult(success, "Added"); + ClearAndRefresh(); + } - private void btnUpdate_Click(object sender, EventArgs e) - { + private void btnUpdate_Click(object sender, EventArgs e) + { + if (selectedRow == null) { MessageBox.Show("Select a row!"); return; } + if (!ValidateFields()) return; + var success = controller.update(GetStudent()); + ShowResult(success, "Updated"); + ClearAndRefresh(); + } - } + private void btnDelete_Click(object sender, EventArgs e) + { + if (selectedRow == null) { MessageBox.Show("Select a row!"); return; } + if (MessageBox.Show("Delete?", "Confirm", MessageBoxButtons.YesNo) == DialogResult.No) return; + + var success = controller.delete(Convert.ToInt32(selectedRow.Cells["Id"].Value)); + ShowResult(success, "Deleted"); + ClearAndRefresh(); + } - private void btnDelete_Click(object sender, EventArgs e) - { + private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) + { + if (e.RowIndex < 0) return; + selectedRow = dataGridView1.Rows[e.RowIndex]; + PopulateFields(); + } + // Helpers + private bool ValidateFields() => + !string.IsNullOrWhiteSpace(txtName.Text) && !string.IsNullOrWhiteSpace(txtCourse.Text); + private Student GetStudent() => new() + { + Id = int.TryParse(txtId.Text, out int id) ? id : 0, + Name = txtName.Text, + Course = txtCourse.Text, + Age = int.TryParse(txtAge.Text, out int age) ? age : 0 + }; - } + private void PopulateFields() + { + txtId.Text = selectedRow.Cells["Id"].Value?.ToString() ?? ""; + txtName.Text = selectedRow.Cells["Name"].Value?.ToString() ?? ""; + txtCourse.Text = selectedRow.Cells["Course"].Value?.ToString() ?? ""; + txtAge.Text = selectedRow.Cells["Age"].Value?.ToString() ?? ""; + } - private void dataGridView1_CellClick(object sender, EventArgs e) - { + private void ClearAndRefresh() + { + ClearFields(); + loadData(); + } + + private void ClearFields() + { + txtId.Clear(); txtName.Clear(); txtCourse.Clear(); txtAge.Clear(); + selectedRow = null; + } + private void ShowResult(bool success, string action) + { + var msg = success ? $" {action} successfully!" : $" Failed to {action}!"; + MessageBox.Show(msg, success ? "Success" : "Error"); + } } } diff --git a/TestWins/model/ConnectionSql.cs b/TestWins/model/ConnectionSql.cs index 1b1bdea..3bac3a3 100644 --- a/TestWins/model/ConnectionSql.cs +++ b/TestWins/model/ConnectionSql.cs @@ -1,22 +1,67 @@ -namespace TestWins.Model; - -//dotnet add package MySql.Data using MySql.Data.MySqlClient; +using System; -public class ConnectionSql +namespace TestWins.Model { - private readonly string _connectionString = "server=localhost;database=student;uid=root;pwd=root"; - private MySqlConnection _conn; - - public MySqlConnection connectSql() + public class ConnectionSql { + private readonly string _connectionString = "server=localhost;database=student;uid=root;pwd=root"; + private MySqlConnection _conn; - Console.WriteLine("Connecting to DB"); + public MySqlConnection ConnectSql() + { + try + { + Console.WriteLine("Connecting to DB..."); + + if (_conn == null || _conn.State == System.Data.ConnectionState.Closed) + { + _conn = new MySqlConnection(_connectionString); + _conn.Open(); + } - _conn = new MySqlConnection(_connectionString); + if (_conn.State == System.Data.ConnectionState.Open) + { + Console.WriteLine(" Database connection successful"); + return _conn; + } + else + { + Console.WriteLine(" Database connection failed"); + return null; + } + } + catch (MySqlException ex) + { + Console.WriteLine($" Database connection error: {ex.Message}"); + return null; + } + catch (Exception ex) + { + Console.WriteLine($" Unexpected error: {ex.Message}"); + return null; + } + } - Console.WriteLine(_conn == null ? "Datbase Connection Failed" : "Database connection successful"); + public void CloseConnection() + { + try + { + if (_conn != null && _conn.State == System.Data.ConnectionState.Open) + { + _conn.Close(); + Console.WriteLine("Connection closed"); + } + } + catch (Exception ex) + { + Console.WriteLine($"Error closing connection: {ex.Message}"); + } + } - return _conn; + public MySqlConnection GetConnection() + { + return ConnectSql(); + } } -} \ No newline at end of file +}