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
90 changes: 85 additions & 5 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();

public Form1()
{
InitializeComponent();
Expand All @@ -19,25 +19,105 @@ private void loadData()
dataGridView1.DataSource = controller.getAll();
}

private void ClearFields()
{
txtStudentId.Clear();
txtName.Clear();
txtAge.Clear();
txtCourse.Clear();
}

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

controller.createStudent(student);
MessageBox.Show("Student added successfully!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);

loadData();
ClearFields();
}
catch (Exception ex)
{
MessageBox.Show($"Error adding student: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

private void btnUpdate_Click(object sender, EventArgs e)
{
try
{
if (string.IsNullOrWhiteSpace(txtStudentId.Text))
{
MessageBox.Show("Please select a student to update.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}

var student = new Student
{
studentId = txtStudentId.Text,
Name = txtName.Text,
age = int.Parse(txtAge.Text),
course = txtCourse.Text
};

controller.update(student);
MessageBox.Show("Student updated successfully!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);

loadData();
ClearFields();
}
catch (Exception ex)
{
MessageBox.Show($"Error updating student: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

private void btnDelete_Click(object sender, EventArgs e)
{
try
{
if (string.IsNullOrWhiteSpace(txtStudentId.Text))
{
MessageBox.Show("Please select a student to delete.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}



var confirmResult = MessageBox.Show("Are you sure to delete this student?", "Confirm Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (confirmResult == DialogResult.Yes)
{
controller.delete(txtStudentId.Text);
MessageBox.Show("Student deleted successfully!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);

loadData();
ClearFields();
}
}
catch (Exception ex)
{
MessageBox.Show($"Error deleting student: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

private void dataGridView1_CellClick(object sender, EventArgs e)
{

// Ensure the user clicked a valid row (not the headers)
if (e.RowIndex >= 0)
{
DataGridViewRow 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();
}
}
}
14 changes: 3 additions & 11 deletions TestWins/model/ConnectionSql.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
namespace TestWins.Model;

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

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

public MySqlConnection connectSql()
{

Console.WriteLine("Connecting to DB");

_conn = new MySqlConnection(_connectionString);

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

return _conn;
// Return a fresh connection object. The 'using' blocks in your repository will handle opening and disposing it.
return new MySqlConnection(_connectionString);
}
}
}
29 changes: 11 additions & 18 deletions TestWins/repository/StudentRepository.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
//Imports
using MySql.Data.MySqlClient;
using TestWins.Model;

namespace TestWins.Repoository;


public class StudentRepository
{

private readonly ConnectionSql _db = new ConnectionSql();

//Create CRUD functionalities
public void create(Student student)
{
using var conn = _db.connectSql(); //Connection
conn.Open(); //Open Connection

string query = "INSERT INTO students VALUES(@name, @age, @course)";
using var conn = _db.connectSql();
conn.Open();

// Explicitly defining columns prevents errors if the DB schema order changes
string query = "INSERT INTO students (studentId, name, age, course) VALUES (@id, @name, @age, @course)";
using var cmd = new MySqlCommand(query, conn);

cmd.Parameters.AddWithValue("@id", student.studentId);
cmd.Parameters.AddWithValue("@name", student.Name);
cmd.Parameters.AddWithValue("@age", student.age);
cmd.Parameters.AddWithValue("@course", student.course);
Expand All @@ -30,7 +27,6 @@ public void create(Student student)
public List<Student> getAll()
{
var list = new List<Student>();

using var conn = _db.connectSql();
conn.Open();

Expand All @@ -48,16 +44,15 @@ public List<Student> getAll()
course = reader.GetString(3),
});
}

return list;
}

public void update(Student student)
{
using var conn = _db.connectSql();
conn.Open();

string query = "UPDATE students SET name = @name, age=@age, course=@course where studentId = @ID";

string query = "UPDATE students SET name = @name, age=@age, course=@course WHERE studentId = @id";
using var cmd = new MySqlCommand(query, conn);

cmd.Parameters.AddWithValue("@id", student.studentId);
Expand All @@ -66,20 +61,18 @@ public void update(Student student)
cmd.Parameters.AddWithValue("@course", student.course);

cmd.ExecuteNonQuery();

}

public void delete(string id)
{
using var conn = _db.connectSql();

conn.Open();

string query = "DELETE FROM students WHERE studentId = @id";

using var cmd = new MySqlCommand(query, conn);


// FIX: The parameter was missing here!
cmd.Parameters.AddWithValue("@id", id);
cmd.ExecuteNonQuery();
}

}
}