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
64 changes: 62 additions & 2 deletions TestWins/Form1.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Drawing.Text;
using TestWins.Controller;
using TestWins.Model;

namespace TestWins;

Expand All @@ -8,36 +9,95 @@ public partial class Form1 : Form
//business

private readonly StudentController controller = new StudentController();

public Form1()
{
InitializeComponent();
loadData();
}

private void loadData()
{
dataGridView1.DataSource = null;
dataGridView1.DataSource = controller.getAll();
}

private void btnAdd_Click(object sender, EventArgs e)
{
try
{
Student s = new Student
{
Name = txtName.Text,
age = int.Parse(txtAge.Text),
course = txtCourse.Text
};

controller.createStudent(s);
loadData();
clearFields();
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message);
}
}

private void btnUpdate_Click(object sender, EventArgs e)
{
try
{
Student s = new Student
{
studentId = txtStudentId.Text,
Name = txtName.Text,
age = int.Parse(txtAge.Text),
course = txtCourse.Text
};

controller.update(s);
loadData();
clearFields();
}
catch (Exception ex)
{
MessageBox.Show("Update Error: " + ex.Message);
}
}

private void btnDelete_Click(object sender, EventArgs e)
{
try
{
controller.delete(txtStudentId.Text);
loadData();
clearFields();
}
catch (Exception ex)
{
MessageBox.Show("Delete Error: " + ex.Message);
}
}

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
{
var row = dataGridView1.Rows[e.RowIndex];


txtStudentId.Text = row.Cells["studentId"].Value.ToString();
txtName.Text = row.Cells["Name"].Value.ToString();
txtAge.Text = row.Cells["age"].Value.ToString();
txtCourse.Text = row.Cells["course"].Value.ToString();
}
}

private void dataGridView1_CellClick(object sender, EventArgs e)
private void clearFields()
{

txtStudentId.Text = "";
txtName.Text = "";
txtAge.Text = "";
txtCourse.Text = "";
}
}
33 changes: 20 additions & 13 deletions TestWins/model/ConnectionSql.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
namespace TestWins.Model;

//dotnet add package MySql.Data
using MySql.Data.MySqlClient;

public class ConnectionSql
namespace TestWins.Model
{
private readonly string _connectionString = "server=localhost;database=student;uid=root;pwd=root";
private MySqlConnection _conn;

public MySqlConnection connectSql()
public class ConnectionSql
{
private readonly string _connectionString = "server=localhost;database=student;uid=root;pwd=root";

Console.WriteLine("Connecting to DB");
public MySqlConnection connectSql()
{
try
{
Console.WriteLine("Connecting to DB");

_conn = new MySqlConnection(_connectionString);
MySqlConnection conn = new MySqlConnection(_connectionString);
conn.Open();

Console.WriteLine(_conn == null ? "Datbase Connection Failed" : "Database connection successful");
Console.WriteLine("Database connection successful");

return _conn;
return conn;
}
catch (Exception ex)
{
Console.WriteLine("Database connection failed: " + ex.Message);
throw;
}
}
}
}
}