From eedd367c938ec3479a9c48985b12d220450376d0 Mon Sep 17 00:00:00 2001 From: jannysins <93581889+jannysins@users.noreply.github.com> Date: Sun, 29 Mar 2026 19:34:33 +0800 Subject: [PATCH 1/5] Updated sql connection Updated connectSql method to return a new MySqlConnection object directly instead of a class member. --- TestWins/model/ConnectionSql.cs | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/TestWins/model/ConnectionSql.cs b/TestWins/model/ConnectionSql.cs index 1b1bdea..bb077f9 100644 --- a/TestWins/model/ConnectionSql.cs +++ b/TestWins/model/ConnectionSql.cs @@ -1,22 +1,14 @@ namespace TestWins.Model; -//dotnet add package MySql.Data using MySql.Data.MySqlClient; public class ConnectionSql { private readonly string _connectionString = "server=localhost;database=student;uid=root;pwd=root"; - private MySqlConnection _conn; public MySqlConnection connectSql() { - - Console.WriteLine("Connecting to DB"); - - _conn = new MySqlConnection(_connectionString); - - Console.WriteLine(_conn == null ? "Datbase Connection Failed" : "Database connection successful"); - - return _conn; + // Return a fresh connection object. The 'using' blocks in your repository will handle opening and disposing it. + return new MySqlConnection(_connectionString); } -} \ No newline at end of file +} From 66218b78bd4a876dd77be704194e7d941da059a8 Mon Sep 17 00:00:00 2001 From: jannysins <93581889+jannysins@users.noreply.github.com> Date: Sun, 29 Mar 2026 19:35:43 +0800 Subject: [PATCH 2/5] Fixed sql queries Refactored SQL queries in StudentRepository to explicitly define columns and corrected parameter usage in delete method. --- TestWins/repository/StudentRepository.cs | 29 +++++++++--------------- 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/TestWins/repository/StudentRepository.cs b/TestWins/repository/StudentRepository.cs index a823e74..68dd474 100644 --- a/TestWins/repository/StudentRepository.cs +++ b/TestWins/repository/StudentRepository.cs @@ -1,25 +1,22 @@ -//Imports using MySql.Data.MySqlClient; using TestWins.Model; namespace TestWins.Repoository; - public class StudentRepository { - private readonly ConnectionSql _db = new ConnectionSql(); - //Create CRUD functionalities public void create(Student student) { - using var conn = _db.connectSql(); //Connection - conn.Open(); //Open Connection - - string query = "INSERT INTO students VALUES(@name, @age, @course)"; + using var conn = _db.connectSql(); + conn.Open(); + // Explicitly defining columns prevents errors if the DB schema order changes + string query = "INSERT INTO students (studentId, name, age, course) VALUES (@id, @name, @age, @course)"; using var cmd = new MySqlCommand(query, conn); + cmd.Parameters.AddWithValue("@id", student.studentId); cmd.Parameters.AddWithValue("@name", student.Name); cmd.Parameters.AddWithValue("@age", student.age); cmd.Parameters.AddWithValue("@course", student.course); @@ -30,7 +27,6 @@ public void create(Student student) public List getAll() { var list = new List(); - using var conn = _db.connectSql(); conn.Open(); @@ -48,16 +44,15 @@ public List getAll() course = reader.GetString(3), }); } - return list; } + public void update(Student student) { using var conn = _db.connectSql(); conn.Open(); - string query = "UPDATE students SET name = @name, age=@age, course=@course where studentId = @ID"; - + string query = "UPDATE students SET name = @name, age=@age, course=@course WHERE studentId = @id"; using var cmd = new MySqlCommand(query, conn); cmd.Parameters.AddWithValue("@id", student.studentId); @@ -66,20 +61,18 @@ public void update(Student student) cmd.Parameters.AddWithValue("@course", student.course); cmd.ExecuteNonQuery(); - } public void delete(string id) { using var conn = _db.connectSql(); - conn.Open(); string query = "DELETE FROM students WHERE studentId = @id"; - using var cmd = new MySqlCommand(query, conn); - + + // FIX: The parameter was missing here! + cmd.Parameters.AddWithValue("@id", id); cmd.ExecuteNonQuery(); } - -} \ No newline at end of file +} From 7fe7af5d57b10041004a7b6623dfebbef8773cee Mon Sep 17 00:00:00 2001 From: jannysins <93581889+jannysins@users.noreply.github.com> Date: Sun, 29 Mar 2026 19:37:11 +0800 Subject: [PATCH 3/5] adding functionalities on the buttons --- TestWins/Form1.cs | 90 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 85 insertions(+), 5 deletions(-) diff --git a/TestWins/Form1.cs b/TestWins/Form1.cs index fcefc88..17cc29d 100644 --- a/TestWins/Form1.cs +++ b/TestWins/Form1.cs @@ -1,13 +1,13 @@ using System.Drawing.Text; using TestWins.Controller; +using TestWins.Model; namespace TestWins; public partial class Form1 : Form { - //business - private readonly StudentController controller = new StudentController(); + public Form1() { InitializeComponent(); @@ -19,25 +19,105 @@ private void loadData() dataGridView1.DataSource = controller.getAll(); } + private void ClearFields() + { + txtStudentId.Clear(); + txtName.Clear(); + txtAge.Clear(); + txtCourse.Clear(); + } + private void btnAdd_Click(object sender, EventArgs e) { + try + { + var student = new Student + { + studentId = txtStudentId.Text, + Name = txtName.Text, + age = int.Parse(txtAge.Text), + course = txtCourse.Text + }; + controller.createStudent(student); + MessageBox.Show("Student added successfully!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); + + loadData(); + ClearFields(); + } + catch (Exception ex) + { + MessageBox.Show($"Error adding student: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + } } private void btnUpdate_Click(object sender, EventArgs e) { + try + { + if (string.IsNullOrWhiteSpace(txtStudentId.Text)) + { + MessageBox.Show("Please select a student to update.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); + return; + } + var student = new Student + { + studentId = txtStudentId.Text, + Name = txtName.Text, + age = int.Parse(txtAge.Text), + course = txtCourse.Text + }; + + controller.update(student); + MessageBox.Show("Student updated successfully!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); + + loadData(); + ClearFields(); + } + catch (Exception ex) + { + MessageBox.Show($"Error updating student: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + } } private void btnDelete_Click(object sender, EventArgs e) { + try + { + if (string.IsNullOrWhiteSpace(txtStudentId.Text)) + { + MessageBox.Show("Please select a student to delete.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); + return; + } - - + var confirmResult = MessageBox.Show("Are you sure to delete this student?", "Confirm Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question); + if (confirmResult == DialogResult.Yes) + { + controller.delete(txtStudentId.Text); + MessageBox.Show("Student deleted successfully!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); + + loadData(); + ClearFields(); + } + } + catch (Exception ex) + { + MessageBox.Show($"Error deleting student: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + } } private void dataGridView1_CellClick(object sender, EventArgs e) { - + // Ensure the user clicked a valid row (not the headers) + if (e.RowIndex >= 0) + { + DataGridViewRow row = dataGridView1.Rows[e.RowIndex]; + + txtStudentId.Text = row.Cells["studentId"].Value?.ToString(); + txtName.Text = row.Cells["Name"].Value?.ToString(); + txtAge.Text = row.Cells["age"].Value?.ToString(); + txtCourse.Text = row.Cells["course"].Value?.ToString(); + } } } From 6873606c2dd0f7cf7a99774898c3ca9a2921a180 Mon Sep 17 00:00:00 2001 From: jannysins <93581889+jannysins@users.noreply.github.com> Date: Sun, 29 Mar 2026 19:42:53 +0800 Subject: [PATCH 4/5] funtionalities of the buttons --- TestWins/Form1.cs | 109 +++++++++++++--------------------------------- 1 file changed, 31 insertions(+), 78 deletions(-) diff --git a/TestWins/Form1.cs b/TestWins/Form1.cs index 17cc29d..826bf5e 100644 --- a/TestWins/Form1.cs +++ b/TestWins/Form1.cs @@ -1,4 +1,5 @@ using System.Drawing.Text; +using System.Xml.Linq; using TestWins.Controller; using TestWins.Model; @@ -6,8 +7,10 @@ namespace TestWins; public partial class Form1 : Form { + //business + private readonly StudentController controller = new StudentController(); - + public Form1() { InitializeComponent(); @@ -19,105 +22,55 @@ private void loadData() dataGridView1.DataSource = controller.getAll(); } - private void ClearFields() - { - txtStudentId.Clear(); - txtName.Clear(); - txtAge.Clear(); - txtCourse.Clear(); - } - private void btnAdd_Click(object sender, EventArgs e) { - try + var student = new Student { - var student = new Student - { - studentId = txtStudentId.Text, - Name = txtName.Text, - age = int.Parse(txtAge.Text), - course = txtCourse.Text - }; + Id = int.Parse(txtId.Text), + Name = txtName.Text, + Course = txtCourse.Text, + Year = int.Parse(txtYear.Text) + }; - controller.createStudent(student); - MessageBox.Show("Student added successfully!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); - - loadData(); - ClearFields(); - } - catch (Exception ex) - { - MessageBox.Show($"Error adding student: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); - } + controller.create(student); + loadData(); } private void btnUpdate_Click(object sender, EventArgs e) { - try + var student = new Student { - if (string.IsNullOrWhiteSpace(txtStudentId.Text)) - { - MessageBox.Show("Please select a student to update.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); - return; - } + Id = int.Parse(txtId.Text), + Name = txtName.Text, + Course = txtCourse.Text, + Year = int.Parse(txtYear.Text) + }; - var student = new Student - { - studentId = txtStudentId.Text, - Name = txtName.Text, - age = int.Parse(txtAge.Text), - course = txtCourse.Text - }; - - controller.update(student); - MessageBox.Show("Student updated successfully!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); - - loadData(); - ClearFields(); - } - catch (Exception ex) - { - MessageBox.Show($"Error updating student: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); - } + controller.update(student); + loadData(); } private void btnDelete_Click(object sender, EventArgs e) { - try - { - if (string.IsNullOrWhiteSpace(txtStudentId.Text)) - { - MessageBox.Show("Please select a student to delete.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); - return; - } + int id = int.Parse(txtId.Text); - var confirmResult = MessageBox.Show("Are you sure to delete this student?", "Confirm Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question); - if (confirmResult == DialogResult.Yes) - { - controller.delete(txtStudentId.Text); - MessageBox.Show("Student deleted successfully!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); - - loadData(); - ClearFields(); - } - } - catch (Exception ex) - { - MessageBox.Show($"Error deleting student: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); - } + + + controller.delete(id); + loadData(); } private void dataGridView1_CellClick(object sender, EventArgs e) + private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { - // Ensure the user clicked a valid row (not the headers) if (e.RowIndex >= 0) { DataGridViewRow row = dataGridView1.Rows[e.RowIndex]; - - txtStudentId.Text = row.Cells["studentId"].Value?.ToString(); - txtName.Text = row.Cells["Name"].Value?.ToString(); - txtAge.Text = row.Cells["age"].Value?.ToString(); - txtCourse.Text = row.Cells["course"].Value?.ToString(); + + txtId.Text = row.Cells["Id"].Value.ToString(); + txtName.Text = row.Cells["Name"].Value.ToString(); + txtCourse.Text = row.Cells["Course"].Value.ToString(); + txtYear.Text = row.Cells["Year"].Value.ToString(); } } } From 42550b6ac53d94d4ef3b9f8e61242ab4ea83b9f7 Mon Sep 17 00:00:00 2001 From: jannysins <93581889+jannysins@users.noreply.github.com> Date: Sun, 29 Mar 2026 19:51:19 +0800 Subject: [PATCH 5/5] updated button functionalities --- TestWins/Form1.cs | 109 +++++++++++++++++++++++++++++++++------------- 1 file changed, 78 insertions(+), 31 deletions(-) diff --git a/TestWins/Form1.cs b/TestWins/Form1.cs index 826bf5e..17cc29d 100644 --- a/TestWins/Form1.cs +++ b/TestWins/Form1.cs @@ -1,5 +1,4 @@ using System.Drawing.Text; -using System.Xml.Linq; using TestWins.Controller; using TestWins.Model; @@ -7,10 +6,8 @@ namespace TestWins; public partial class Form1 : Form { - //business - private readonly StudentController controller = new StudentController(); - + public Form1() { InitializeComponent(); @@ -22,55 +19,105 @@ private void loadData() dataGridView1.DataSource = controller.getAll(); } + private void ClearFields() + { + txtStudentId.Clear(); + txtName.Clear(); + txtAge.Clear(); + txtCourse.Clear(); + } + private void btnAdd_Click(object sender, EventArgs e) { - var student = new Student + try { - Id = int.Parse(txtId.Text), - Name = txtName.Text, - Course = txtCourse.Text, - Year = int.Parse(txtYear.Text) - }; + var student = new Student + { + studentId = txtStudentId.Text, + Name = txtName.Text, + age = int.Parse(txtAge.Text), + course = txtCourse.Text + }; - controller.create(student); - loadData(); + controller.createStudent(student); + MessageBox.Show("Student added successfully!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); + + loadData(); + ClearFields(); + } + catch (Exception ex) + { + MessageBox.Show($"Error adding student: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + } } private void btnUpdate_Click(object sender, EventArgs e) { - var student = new Student + try { - Id = int.Parse(txtId.Text), - Name = txtName.Text, - Course = txtCourse.Text, - Year = int.Parse(txtYear.Text) - }; + if (string.IsNullOrWhiteSpace(txtStudentId.Text)) + { + MessageBox.Show("Please select a student to update.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); + return; + } - controller.update(student); - loadData(); + var student = new Student + { + studentId = txtStudentId.Text, + Name = txtName.Text, + age = int.Parse(txtAge.Text), + course = txtCourse.Text + }; + + controller.update(student); + MessageBox.Show("Student updated successfully!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); + + loadData(); + ClearFields(); + } + catch (Exception ex) + { + MessageBox.Show($"Error updating student: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + } } private void btnDelete_Click(object sender, EventArgs e) { - int id = int.Parse(txtId.Text); - - + try + { + if (string.IsNullOrWhiteSpace(txtStudentId.Text)) + { + MessageBox.Show("Please select a student to delete.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); + return; + } - controller.delete(id); - loadData(); + var confirmResult = MessageBox.Show("Are you sure to delete this student?", "Confirm Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question); + if (confirmResult == DialogResult.Yes) + { + controller.delete(txtStudentId.Text); + MessageBox.Show("Student deleted successfully!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); + + loadData(); + ClearFields(); + } + } + catch (Exception ex) + { + MessageBox.Show($"Error deleting student: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + } } private void dataGridView1_CellClick(object sender, EventArgs e) - private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { + // Ensure the user clicked a valid row (not the headers) if (e.RowIndex >= 0) { DataGridViewRow row = dataGridView1.Rows[e.RowIndex]; - - txtId.Text = row.Cells["Id"].Value.ToString(); - txtName.Text = row.Cells["Name"].Value.ToString(); - txtCourse.Text = row.Cells["Course"].Value.ToString(); - txtYear.Text = row.Cells["Year"].Value.ToString(); + + txtStudentId.Text = row.Cells["studentId"].Value?.ToString(); + txtName.Text = row.Cells["Name"].Value?.ToString(); + txtAge.Text = row.Cells["age"].Value?.ToString(); + txtCourse.Text = row.Cells["course"].Value?.ToString(); } } }