diff --git a/TestWins/Form1.cs b/TestWins/Form1.cs index fcefc88..5008ece 100644 --- a/TestWins/Form1.cs +++ b/TestWins/Form1.cs @@ -11,33 +11,83 @@ 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 TestWins.Model.Student + { + studentId = txtStudentId.Text, + Name = txtName.Text, + age = int.Parse(txtAge.Text), + course = txtCourse.Text + }; + controller.createStudent(student); + loadData(); + clearFields(); } private void btnUpdate_Click(object sender, EventArgs e) { + var student = new TestWins.Model.Student + { + studentId = txtStudentId.Text, + Name = txtName.Text, + age = int.Parse(txtAge.Text), + course = txtCourse.Text + }; + controller.update(student); + loadData(); + clearFields(); } private void btnDelete_Click(object sender, EventArgs e) { - - - + controller.delete(txtStudentId.Text); + loadData(); + clearFields(); } private void dataGridView1_CellClick(object sender, EventArgs e) { + 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 clearFields() + { + txtStudentId.Text = ""; + 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);