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
93 changes: 85 additions & 8 deletions TestWins/Form1.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
24 changes: 16 additions & 8 deletions TestWins/model/ConnectionSql.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
}