diff --git a/TestWins/Form1.cs b/TestWins/Form1.cs index fcefc88..2c526eb 100644 --- a/TestWins/Form1.cs +++ b/TestWins/Form1.cs @@ -1,43 +1,120 @@ -using System.Drawing.Text; using TestWins.Controller; namespace TestWins; public partial class Form1 : Form { - //business - private readonly StudentController controller = new StudentController(); + public Form1() { InitializeComponent(); - loadData(); + LoadData(); } - private void loadData() + private void LoadData() { - dataGridView1.DataSource = controller.getAll(); + try + { + dataGridView1.DataSource = controller.getAll(); + } + catch (Exception ex) + { + MessageBox.Show("Error loading data: " + ex.Message); + } + } + + private Students GetStudentFromForm(bool includeId = false) + { + Students s = new Students() + { + Name = txtName.Text, + Course = txtCourse.Text + }; + + if (int.TryParse(txtAge.Text, out int age)) + s.Age = age; + else + throw new Exception("Invalid Age"); + + if (includeId) + { + if (int.TryParse(txtId.Text, out int id)) + s.Id = id; + else + throw new Exception("Invalid ID"); + } + + return s; + } + + private void ClearFields() + { + txtId.Clear(); + txtName.Clear(); + txtAge.Clear(); + txtCourse.Clear(); } private void btnAdd_Click(object sender, EventArgs e) { + try + { + var student = GetStudentFromForm(); + controller.insert(student); + LoadData(); + ClearFields(); + } + catch (Exception ex) + { + MessageBox.Show("Add failed: " + ex.Message); + } } private void btnUpdate_Click(object sender, EventArgs e) { + try + { + var student = GetStudentFromForm(true); + controller.update(student); + LoadData(); + ClearFields(); + } + catch (Exception ex) + { + MessageBox.Show("Update failed: " + ex.Message); + } } private void btnDelete_Click(object sender, EventArgs e) { + try + { + if (!int.TryParse(txtId.Text, out int id)) + throw new Exception("Invalid ID"); + controller.delete(id); - + LoadData(); + ClearFields(); + } + catch (Exception ex) + { + MessageBox.Show("Delete failed: " + ex.Message); + } } - 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]; + txtId.Text = row.Cells["Id"].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..90e3fc5 100644 --- a/TestWins/model/ConnectionSql.cs +++ b/TestWins/model/ConnectionSql.cs @@ -6,17 +6,25 @@ namespace TestWins.Model; public class ConnectionSql { private readonly string _connectionString = "server=localhost;database=student;uid=root;pwd=root"; - private MySqlConnection _conn; + private MySqlConnection _conn; - public MySqlConnection connectSql() + public MySqlConnection ConnectSql() { + try + { + Console.WriteLine("Connecting to DB..."); - Console.WriteLine("Connecting to DB"); + _conn = new MySqlConnection(_connectionString); + _conn.Open(); - _conn = new MySqlConnection(_connectionString); + Console.WriteLine("Database connection successful"); - Console.WriteLine(_conn == null ? "Datbase Connection Failed" : "Database connection successful"); - - return _conn; + return _conn; + } + catch (Exception ex) + { + Console.WriteLine("Database connection failed: " + ex.Message); + return null; + } } -} \ No newline at end of file +}