diff --git a/TestWins/Form1.cs b/TestWins/Form1.cs index fcefc88..c766467 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(); + private DataGridViewRow? selectedRow; + public Form1() { InitializeComponent(); @@ -21,23 +21,109 @@ private void loadData() private void btnAdd_Click(object sender, EventArgs e) { + if (!ValidateFields()) + { + MessageBox.Show("Please fill in all required fields!"); + return; + } + var success = controller.add(GetStudent()); + ShowResult(success, "Added"); + ClearAndRefresh(); } private void btnUpdate_Click(object sender, EventArgs e) { + if (selectedRow == null) + { + MessageBox.Show("Select a row first!"); + return; + } + + if (!ValidateFields()) + { + MessageBox.Show("Please fill in all required fields!"); + return; + } + var success = controller.update(GetStudent()); + ShowResult(success, "Updated"); + ClearAndRefresh(); } private void btnDelete_Click(object sender, EventArgs e) { + if (selectedRow == null) + { + MessageBox.Show("Select a row first!"); + return; + } + + if (MessageBox.Show("Delete this record?", "Confirm", MessageBoxButtons.YesNo) == DialogResult.No) + return; + + int id = Convert.ToInt32(selectedRow.Cells["Id"].Value); + var success = controller.delete(id); + + ShowResult(success, "Deleted"); + ClearAndRefresh(); + } + + private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) + { + if (e.RowIndex < 0) return; + + selectedRow = dataGridView1.Rows[e.RowIndex]; + PopulateFields(); + } + + //HELPERS + + private bool ValidateFields() + { + return !string.IsNullOrWhiteSpace(txtName.Text) && + !string.IsNullOrWhiteSpace(txtCourse.Text); + } + + private Student GetStudent() + { + return new Student + { + Id = int.TryParse(txtId.Text, out int id) ? id : 0, + Name = txtName.Text, + Course = txtCourse.Text, + Age = int.TryParse(txtAge.Text, out int age) ? age : 0 + }; + } + private void PopulateFields() + { + if (selectedRow == null) return; + txtId.Text = selectedRow.Cells["Id"].Value?.ToString() ?? ""; + txtName.Text = selectedRow.Cells["Name"].Value?.ToString() ?? ""; + txtCourse.Text = selectedRow.Cells["Course"].Value?.ToString() ?? ""; + txtAge.Text = selectedRow.Cells["Age"].Value?.ToString() ?? ""; + } + private void ClearAndRefresh() + { + ClearFields(); + loadData(); } - private void dataGridView1_CellClick(object sender, EventArgs e) + private void ClearFields() { + txtId.Clear(); + txtName.Clear(); + txtCourse.Clear(); + txtAge.Clear(); + selectedRow = null; + } + private void ShowResult(bool success, string action) + { + string msg = success ? $"✅ {action} successfully!" : $"❌ Failed to {action}!"; + MessageBox.Show(msg, success ? "Success" : "Error"); } } diff --git a/TestWins/model/ConnectionSql.cs b/TestWins/model/ConnectionSql.cs index 1b1bdea..0fa42f4 100644 --- a/TestWins/model/ConnectionSql.cs +++ b/TestWins/model/ConnectionSql.cs @@ -1,22 +1,61 @@ namespace TestWins.Model; -//dotnet add package MySql.Data +// dotnet add package MySql.Data using MySql.Data.MySqlClient; +using System; +using System.Data; public class ConnectionSql { - private readonly string _connectionString = "server=localhost;database=student;uid=root;pwd=root"; - private MySqlConnection _conn; + private readonly string _connectionString = + "server=localhost;database=student;uid=root;pwd=root"; + + private MySqlConnection _conn; public MySqlConnection connectSql() { + try + { + Console.WriteLine("Connecting to DB..."); + + _conn = new MySqlConnection(_connectionString); - Console.WriteLine("Connecting to DB"); + _conn.Open(); - _conn = new MySqlConnection(_connectionString); + Console.WriteLine("Database connection successful"); - Console.WriteLine(_conn == null ? "Datbase Connection Failed" : "Database connection successful"); + return _conn; + } + catch (MySqlException ex) + { + Console.WriteLine("Database Error: " + ex.Message); + return null; + } + catch (Exception ex) + { + Console.WriteLine("Unexpected Error: " + ex.Message); + return null; + } + } - return _conn; + public void closeSql() + { + try + { + if (_conn != null && _conn.State == ConnectionState.Open) + { + _conn.Close(); + Console.WriteLine("Connection closed"); + } + } + catch (Exception ex) + { + Console.WriteLine("Error closing connection: " + ex.Message); + } + } + + public MySqlConnection getConnection() + { + return connectSql(); } -} \ No newline at end of file +}