Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 90 additions & 4 deletions TestWins/Form1.cs
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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");
}
}
55 changes: 47 additions & 8 deletions TestWins/model/ConnectionSql.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
}