diff --git a/TestWins/Form1.cs b/TestWins/Form1.cs index fcefc88..8fbab1c 100644 --- a/TestWins/Form1.cs +++ b/TestWins/Form1.cs @@ -6,8 +6,8 @@ namespace TestWins; public partial class Form1 : Form { //business - private readonly StudentController controller = new StudentController(); + public Form1() { InitializeComponent(); @@ -18,26 +18,60 @@ private void loadData() { dataGridView1.DataSource = controller.getAll(); } - 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/repository/StudentRepository.cs b/TestWins/repository/StudentRepository.cs index a823e74..bb06d47 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); @@ -82,4 +82,4 @@ public void delete(string id) cmd.ExecuteNonQuery(); } -} \ No newline at end of file +}