From adc4621b8e3f3b0a45c7793b92ffa0763efeff65 Mon Sep 17 00:00:00 2001 From: Tamago! Date: Sun, 29 Mar 2026 16:22:54 +0800 Subject: [PATCH 1/2] Fix and Add button Functionalities --- TestWins/Form1.cs | 67 ++++++++++++++++++++++-- TestWins/model/ConnectionSql.cs | 2 +- TestWins/repository/StudentRepository.cs | 4 +- 3 files changed, 67 insertions(+), 6 deletions(-) diff --git a/TestWins/Form1.cs b/TestWins/Form1.cs index fcefc88..4f25c0a 100644 --- a/TestWins/Form1.cs +++ b/TestWins/Form1.cs @@ -1,3 +1,6 @@ +using System.Drawing.Text; +using TestWins.Controller; + using System.Drawing.Text; using TestWins.Controller; @@ -11,33 +14,91 @@ public partial class Form1 : Form public Form1() { InitializeComponent(); + + + dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect; + dataGridView1.AutoGenerateColumns = true; + dataGridView1.MultiSelect = false; + loadData(); } private void loadData() { - dataGridView1.DataSource = controller.getAll(); + try + { + dataGridView1.DataSource = controller.getAll(); + } + catch (Exception ex) + { + MessageBox.Show("Error loading data: " + ex.Message); + } } private void btnAdd_Click(object sender, EventArgs e) { + var student = new Student + { + studentId = Guid.NewGuid().ToString(), + Name = txtName.Text, + age = int.TryParse(txtAge.Text, out int a) ? a : 0, + course = txtCourse.Text + }; + controller.createStudent(student); + LoadData(); + ClearInputs(); } private void btnUpdate_Click(object sender, EventArgs e) { + var currentRow = dataGridView1.CurrentRow; + if (currentRow == null) return; + + var id = currentRow.Cells["studentId"]?.Value?.ToString(); + if (string.IsNullOrEmpty(id)) return; + var student = new Student + { + studentId = id, + Name = txtName.Text, + age = int.TryParse(txtAge.Text, out int a) ? a : 0, + course = txtCourse.Text + }; + + controller.update(student); + LoadData(); + ClearInputs(); } private void btnDelete_Click(object sender, EventArgs e) { + var currentRow = dataGridView1.CurrentRow; + if (currentRow == null) return; + var id = currentRow.Cells["studentId"]?.Value?.ToString(); + if (string.IsNullOrEmpty(id)) return; - + controller.delete(id); + LoadData(); + ClearInputs(); } - 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]; + txtName.Text = row.Cells["Name"]?.Value?.ToString() ?? ""; + txtAge.Text = row.Cells["age"]?.Value?.ToString() ?? ""; + txtCourse.Text = row.Cells["course"]?.Value?.ToString() ?? ""; + } + private void ClearInputs() + { + txtName.Text = ""; + txtAge.Text = ""; + txtCourse.Text = ""; } + } diff --git a/TestWins/model/ConnectionSql.cs b/TestWins/model/ConnectionSql.cs index 1b1bdea..c03986d 100644 --- a/TestWins/model/ConnectionSql.cs +++ b/TestWins/model/ConnectionSql.cs @@ -15,7 +15,7 @@ public MySqlConnection connectSql() _conn = new MySqlConnection(_connectionString); - Console.WriteLine(_conn == null ? "Datbase Connection Failed" : "Database connection successful"); + Console.WriteLine(_conn == null ? "Database Connection Failed" : "Database connection successful"); return _conn; } diff --git a/TestWins/repository/StudentRepository.cs b/TestWins/repository/StudentRepository.cs index a823e74..98c38a7 100644 --- a/TestWins/repository/StudentRepository.cs +++ b/TestWins/repository/StudentRepository.cs @@ -16,7 +16,7 @@ public void create(Student student) using var conn = _db.connectSql(); //Connection conn.Open(); //Open Connection - string query = "INSERT INTO students VALUES(@name, @age, @course)"; + string query = "INSERT INTO (name, age, course) students VALUES(@name, @age, @course)"; using var cmd = new MySqlCommand(query, conn); @@ -56,7 +56,7 @@ 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); From 7fb487eded4a1a009a3aa3483b35ac421e1d79bc Mon Sep 17 00:00:00 2001 From: Tamago! Date: Sun, 29 Mar 2026 16:39:59 +0800 Subject: [PATCH 2/2] Fix student management buttons method in Form1 --- TestWins/Form1.cs | 61 +++++++++++++++++++---------------------------- 1 file changed, 25 insertions(+), 36 deletions(-) diff --git a/TestWins/Form1.cs b/TestWins/Form1.cs index 4f25c0a..5008ece 100644 --- a/TestWins/Form1.cs +++ b/TestWins/Form1.cs @@ -1,6 +1,3 @@ -using System.Drawing.Text; -using TestWins.Controller; - using System.Drawing.Text; using TestWins.Controller; @@ -37,68 +34,60 @@ private void loadData() private void btnAdd_Click(object sender, EventArgs e) { - var student = new Student + var student = new TestWins.Model.Student { - studentId = Guid.NewGuid().ToString(), + studentId = txtStudentId.Text, Name = txtName.Text, - age = int.TryParse(txtAge.Text, out int a) ? a : 0, + age = int.Parse(txtAge.Text), course = txtCourse.Text }; controller.createStudent(student); - LoadData(); - ClearInputs(); + loadData(); + clearFields(); } private void btnUpdate_Click(object sender, EventArgs e) { - var currentRow = dataGridView1.CurrentRow; - if (currentRow == null) return; - - var id = currentRow.Cells["studentId"]?.Value?.ToString(); - if (string.IsNullOrEmpty(id)) return; - - var student = new Student + var student = new TestWins.Model.Student { - studentId = id, + studentId = txtStudentId.Text, Name = txtName.Text, - age = int.TryParse(txtAge.Text, out int a) ? a : 0, + age = int.Parse(txtAge.Text), course = txtCourse.Text }; controller.update(student); - LoadData(); - ClearInputs(); + loadData(); + clearFields(); } private void btnDelete_Click(object sender, EventArgs e) { - var currentRow = dataGridView1.CurrentRow; - if (currentRow == null) return; - - var id = currentRow.Cells["studentId"]?.Value?.ToString(); - if (string.IsNullOrEmpty(id)) return; - - controller.delete(id); - LoadData(); - ClearInputs(); + controller.delete(txtStudentId.Text); + loadData(); + clearFields(); } - private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) + private void dataGridView1_CellClick(object sender, EventArgs e) { - if (e.RowIndex < 0) return; - - var row = dataGridView1.Rows[e.RowIndex]; - txtName.Text = row.Cells["Name"]?.Value?.ToString() ?? ""; - txtAge.Text = row.Cells["age"]?.Value?.ToString() ?? ""; - txtCourse.Text = row.Cells["course"]?.Value?.ToString() ?? ""; + if (dataGridView1.CurrentRow != null) + { + DataGridViewRow row = dataGridView1.CurrentRow; + txtStudentId.Text = row.Cells[0].Value.ToString(); + txtName.Text = row.Cells[1].Value.ToString(); + txtAge.Text = row.Cells[2].Value.ToString(); + txtCourse.Text = row.Cells[3].Value.ToString(); + } } - private void ClearInputs() + private void clearFields() { + txtStudentId.Text = ""; txtName.Text = ""; txtAge.Text = ""; txtCourse.Text = ""; } } +