-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDisplayAllStudents.cs
More file actions
122 lines (110 loc) · 4.5 KB
/
DisplayAllStudents.cs
File metadata and controls
122 lines (110 loc) · 4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Configuration;
using System.Windows.Forms;
namespace TinyCollegeGUI
{
public partial class DisplayAllStudentsForm : Form
{
private string connectionString;
public DisplayAllStudentsForm()
{
InitializeComponent();
connectionString = ConfigurationManager.ConnectionStrings["TinyCollegeDB"].ConnectionString;
LoadStudents(); // Load students when the form loads
}
// Establish a connection to the SQL Server database
private SqlConnection GetConnection()
{
return new SqlConnection(connectionString);
}
// Retrieve all students from the Students table
public List<Student> GetAllStudents()
{
var students = new List<Student>();
using (var connection = GetConnection())
{
connection.Open();
string query = "SELECT * FROM Students";
using (var command = new SqlCommand(query, connection))
{
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
students.Add(new Student
{
StudentId = reader.GetInt32(0),
FirstName = reader.GetString(1),
LastName = reader.GetString(2),
GPA = reader.GetDouble(3)
});
}
}
}
}
return students;
}
// Load students into the DataGridView
private void LoadStudents()
{
var students = GetAllStudents();
dataGridViewStudents.DataSource = students;
}
// Method to remove a student by StudentId
private void RemoveStudent(int studentId)
{
using (var connection = GetConnection())
{
connection.Open();
using (var transaction = connection.BeginTransaction())
{
try
{
// Delete related enrollments
string deleteEnrollmentsQuery = "DELETE FROM Enrollments WHERE StudentID = @StudentID";
using (var command = new SqlCommand(deleteEnrollmentsQuery, connection, transaction))
{
command.Parameters.AddWithValue("@StudentID", studentId);
command.ExecuteNonQuery();
}
// Delete the student
string deleteStudentQuery = "DELETE FROM Students WHERE StudentId = @id";
using (var command = new SqlCommand(deleteStudentQuery, connection, transaction))
{
command.Parameters.AddWithValue("@id", studentId);
command.ExecuteNonQuery();
}
transaction.Commit();
// Displays message box to user to inform user when the selected student has been successfully deleted from the database
MessageBox.Show("Student removed successfully.");
LoadStudents(); // Refresh the list after removal
}
// catch statement allows you to define a block of code to be executed, if an error occurs
catch (Exception ex)
{
transaction.Rollback();
MessageBox.Show($"Error removing student: {ex.Message}");
}
}
}
}
private void button1_Click(object sender, EventArgs e)
{
if (dataGridViewStudents.SelectedRows.Count > 0)
{
int studentId = (int)dataGridViewStudents.SelectedRows[0].Cells["StudentId"].Value;
RemoveStudent(studentId);
}
else
{
// Prompts students to select the specific student they would like to remove from the database
MessageBox.Show("Please select a student to remove.");
}
}
private void DisplayAllStudentsForm_Load(object sender, EventArgs e)
{
}
}
}