From faaa3b455d7051fd78e729e8739d4f545e1d0c66 Mon Sep 17 00:00:00 2001 From: xJuswa1 Date: Sun, 29 Mar 2026 11:09:02 +0800 Subject: [PATCH 1/2] Refactor connection method and improve error handling --- TestWins/model/ConnectionSql.cs | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/TestWins/model/ConnectionSql.cs b/TestWins/model/ConnectionSql.cs index 1b1bdea..90e3fc5 100644 --- a/TestWins/model/ConnectionSql.cs +++ b/TestWins/model/ConnectionSql.cs @@ -6,17 +6,25 @@ namespace TestWins.Model; public class ConnectionSql { private readonly string _connectionString = "server=localhost;database=student;uid=root;pwd=root"; - private MySqlConnection _conn; + private MySqlConnection _conn; - public MySqlConnection connectSql() + public MySqlConnection ConnectSql() { + try + { + Console.WriteLine("Connecting to DB..."); - Console.WriteLine("Connecting to DB"); + _conn = new MySqlConnection(_connectionString); + _conn.Open(); - _conn = new MySqlConnection(_connectionString); + Console.WriteLine("Database connection successful"); - Console.WriteLine(_conn == null ? "Datbase Connection Failed" : "Database connection successful"); - - return _conn; + return _conn; + } + catch (Exception ex) + { + Console.WriteLine("Database connection failed: " + ex.Message); + return null; + } } -} \ No newline at end of file +} From 23e95d44f14ac6609856d0012659ef79ea905787 Mon Sep 17 00:00:00 2001 From: xJuswa1 Date: Sun, 29 Mar 2026 11:19:15 +0800 Subject: [PATCH 2/2] Update Form1.cs --- TestWins/Form1.cs | 93 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 85 insertions(+), 8 deletions(-) diff --git a/TestWins/Form1.cs b/TestWins/Form1.cs index fcefc88..2c526eb 100644 --- a/TestWins/Form1.cs +++ b/TestWins/Form1.cs @@ -1,43 +1,120 @@ -using System.Drawing.Text; using TestWins.Controller; namespace TestWins; public partial class Form1 : Form { - //business - private readonly StudentController controller = new StudentController(); + public Form1() { InitializeComponent(); - loadData(); + LoadData(); } - private void loadData() + private void LoadData() { - dataGridView1.DataSource = controller.getAll(); + try + { + dataGridView1.DataSource = controller.getAll(); + } + catch (Exception ex) + { + MessageBox.Show("Error loading data: " + ex.Message); + } + } + + private Students GetStudentFromForm(bool includeId = false) + { + Students s = new Students() + { + Name = txtName.Text, + Course = txtCourse.Text + }; + + if (int.TryParse(txtAge.Text, out int age)) + s.Age = age; + else + throw new Exception("Invalid Age"); + + if (includeId) + { + if (int.TryParse(txtId.Text, out int id)) + s.Id = id; + else + throw new Exception("Invalid ID"); + } + + return s; + } + + private void ClearFields() + { + txtId.Clear(); + txtName.Clear(); + txtAge.Clear(); + txtCourse.Clear(); } private void btnAdd_Click(object sender, EventArgs e) { + try + { + var student = GetStudentFromForm(); + controller.insert(student); + LoadData(); + ClearFields(); + } + catch (Exception ex) + { + MessageBox.Show("Add failed: " + ex.Message); + } } private void btnUpdate_Click(object sender, EventArgs e) { + try + { + var student = GetStudentFromForm(true); + controller.update(student); + LoadData(); + ClearFields(); + } + catch (Exception ex) + { + MessageBox.Show("Update failed: " + ex.Message); + } } private void btnDelete_Click(object sender, EventArgs e) { + try + { + if (!int.TryParse(txtId.Text, out int id)) + throw new Exception("Invalid ID"); + controller.delete(id); - + LoadData(); + ClearFields(); + } + catch (Exception ex) + { + MessageBox.Show("Delete failed: " + ex.Message); + } } - private void dataGridView1_CellClick(object sender, EventArgs e) + private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { + if (e.RowIndex < 0) return; + + var row = dataGridView1.Rows[e.RowIndex]; + txtId.Text = row.Cells["Id"].Value?.ToString(); + txtName.Text = row.Cells["Name"].Value?.ToString(); + txtAge.Text = row.Cells["Age"].Value?.ToString(); + txtCourse.Text = row.Cells["Course"].Value?.ToString(); } }