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(); + } } } 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 +} 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 +}