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
58 changes: 54 additions & 4 deletions TestWins/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,83 @@ public partial class Form1 : Form
public Form1()
{
InitializeComponent();


dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
dataGridView1.AutoGenerateColumns = true;
dataGridView1.MultiSelect = false;

loadData();
}

private void loadData()
{
dataGridView1.DataSource = controller.getAll();
try
{
dataGridView1.DataSource = controller.getAll();
}
catch (Exception ex)
{
MessageBox.Show("Error loading data: " + ex.Message);
}
}

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

controller.createStudent(student);
loadData();
clearFields();
}

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

controller.update(student);
loadData();
clearFields();
}

private void btnDelete_Click(object sender, EventArgs e)
{



controller.delete(txtStudentId.Text);
loadData();
clearFields();
}

private void dataGridView1_CellClick(object sender, EventArgs e)
{
if (dataGridView1.CurrentRow != null)
{
DataGridViewRow row = dataGridView1.CurrentRow;
txtStudentId.Text = row.Cells[0].Value.ToString();
txtName.Text = row.Cells[1].Value.ToString();
txtAge.Text = row.Cells[2].Value.ToString();
txtCourse.Text = row.Cells[3].Value.ToString();
}
}

private void clearFields()
{
txtStudentId.Text = "";
txtName.Text = "";
txtAge.Text = "";
txtCourse.Text = "";
}

}

2 changes: 1 addition & 1 deletion TestWins/model/ConnectionSql.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public MySqlConnection connectSql()

_conn = new MySqlConnection(_connectionString);

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

return _conn;
}
Expand Down
4 changes: 2 additions & 2 deletions TestWins/repository/StudentRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public void create(Student student)
using var conn = _db.connectSql(); //Connection
conn.Open(); //Open Connection

string query = "INSERT INTO students VALUES(@name, @age, @course)";
string query = "INSERT INTO (name, age, course) students VALUES(@name, @age, @course)";

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

Expand Down Expand Up @@ -56,7 +56,7 @@ 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);

Expand Down