-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfStudent.cs
356 lines (273 loc) · 11.8 KB
/
fStudent.cs
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;
namespace Thuc_Tap_CSDL
{
public partial class fStudent : Form
{
public fStudent()
{
InitializeComponent();
}
SqlConnection con;
private void fStudent_Load(object sender, EventArgs e)
{
txtStudentID.Focus();
string conString = ConfigurationManager.ConnectionStrings["QLTrungTamHocThem"].ConnectionString.ToString();
con = new SqlConnection(conString);
con.Open();
Display();
autoLoadStudentID();
loadDataCombobox();
}
private void fStudent_FormClosing(object sender, FormClosingEventArgs e)
{
con.Close();
}
public void Display()
{
string sqlCode = "SELECT TOP(30) * FROM HOCSINH";
SqlCommand cmd = new SqlCommand(sqlCode, con);
SqlDataReader dataReader = cmd.ExecuteReader();
DataTable dataTable = new DataTable();
dataTable.Load(dataReader);
dgvStudent.DataSource = dataTable;
}
public void autoLoadStudentID()
{
string sqlCode = "SELECT TOP(1) MaHocSinh FROM HOCSINH ORDER BY MaHocSinh DESC";
SqlCommand cmd = new SqlCommand(sqlCode, con);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
int id = Convert.ToInt32(dr.GetValue(0).ToString().Substring(2)) + 1;
txtStudentID.Text = "HS" + id.ToString();
}
dr.Close();
}
private void txtStudentBirth_Enter(object sender, EventArgs e)
{
if (txtStudentBirth.Text == "MM/DD/YYYY")
{
txtStudentBirth.Text = "";
txtStudentBirth.ForeColor = Color.FromArgb(0, 72, 39);
}
}
private void txtStudentBirth_Leave(object sender, EventArgs e)
{
if (txtStudentBirth.Text == "")
{
txtStudentBirth.Text = "MM/DD/YYYY";
txtStudentBirth.ForeColor = Color.Silver;
}
}
private void btnStudent_add_Click(object sender, EventArgs e)
{
string gender;
if (radGender_female.Checked)
{
gender = radGender_female.Text;
}
else
{
gender = radGender_male.Text;
}
string readerCode = "select * from HOCSINH where MaHocSinh = '" + txtStudentID.Text + "' ";
SqlCommand cmd = new SqlCommand(readerCode, con);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read() == true)
{
dr.Close();
string readerCode1 = "select * from DANHSACHLOP where MaHocSinh = '" + txtStudentID.Text + "' and MaLopHoc = '" + textmalop.Text + "'";
SqlCommand cmd1 = new SqlCommand(readerCode1, con);
SqlDataReader dr1 = cmd1.ExecuteReader();
if (dr1.Read() == true)
{
dr1.Close();
MessageBox.Show("Học sinh đã có sẵn trong lớp", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
dr1.Close();
string sqlCode = "exec PROC_INSERT_DANHSACHLOP '" + textmalop.Text + "','" + txtStudentID.Text + "' ";
SqlCommand cmd2 = new SqlCommand(sqlCode, con);
cmd2.ExecuteNonQuery();
string C_name = comboBox1.Text;
string S_name = txtStudentName.Text;
string noti = "Thêm thành công\n" + S_name + " vào lớp " + C_name;
MessageBox.Show(noti, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
else
{
dr.Close();
string sqlCode = "exec PROC_INSERT_HOCSINH '" + txtStudentID.Text + "','" + txtStudentName.Text + "','" + txtStudentBirth.Text + "','" + gender + "','" + txtStudentAddress.Text + "','" + txtStudentPhone.Text + "'";
SqlCommand cmd2 = new SqlCommand(sqlCode, con);
cmd2.ExecuteNonQuery();
string sqlCode1 = "exec PROC_INSERT_DANHSACHLOP '" + textmalop.Text + "','" + txtStudentID.Text + "' ";
SqlCommand cmd3 = new SqlCommand(sqlCode1, con);
cmd3.ExecuteNonQuery();
string C_name = comboBox1.Text;
string S_name = txtStudentName.Text;
string noti = "Thêm thành công\n" + S_name + " vào lớp " + C_name;
MessageBox.Show(noti, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
autoLoadStudentID();
Display();
}
private void btnStudent_edit_Click(object sender, EventArgs e)
{
string gender;
if (radGender_female.Checked)
{
gender = radGender_female.Text;
}
else
{
gender = radGender_male.Text;
}
string sqlEdit = "exec PROC_UPDATE_HOCSINH '" + txtStudentID.Text + "','" + txtStudentName.Text + "','" + txtStudentBirth.Text + "','" + gender + "','" + txtStudentAddress.Text + "','" + txtStudentPhone.Text + "'";
SqlCommand cmd = new SqlCommand(sqlEdit, con);
cmd.ExecuteNonQuery();
Display();
}
private void btnStudent_delete_Click(object sender, EventArgs e)
{
string sqlupdateDelete1 = "update DANHSACHLOP set MaHocSinh = null where MaHocSinh = '" + txtStudentID.Text + "' ";
SqlCommand cmd1 = new SqlCommand(sqlupdateDelete1, con);
cmd1.ExecuteNonQuery();
string sqlDelete = "exec PROC_DELETE_HOCSINH '" + txtStudentID.Text + "'";
SqlCommand cmd = new SqlCommand(sqlDelete, con);
cmd.ExecuteNonQuery();
Display();
}
private void btnStudent_clear_Click(object sender, EventArgs e)
{
txtStudentID.Text = "";
txtStudentName.Text = "";
txtStudentBirth.Text = "MM/DD/YYYY";
txtStudentBirth.ForeColor = Color.Silver;
radGender_male.Checked = true;
txtStudentAddress.Text = "";
txtStudentPhone.Text = "";
txtStudentSearch.Text = "";
//textmalop.Text = "";
autoLoadStudentID();
}
private void btnStudent_all_Click(object sender, EventArgs e)
{
Display();
}
private void btnStudent_search_Click(object sender, EventArgs e)
{
if (cmbStudent.SelectedItem == "Mã học sinh")
{
string sqlCode1 = "select * from DBO.FUNC_SEARCH_HS_MAHS('" + txtStudentSearch.Text + "')";
SqlCommand cmd = new SqlCommand(sqlCode1, con);
cmd.ExecuteNonQuery();
SqlDataReader dataReader = cmd.ExecuteReader();
DataTable dataTable = new DataTable();
dataTable.Load(dataReader);
dgvStudent.DataSource = dataTable;
}
if (cmbStudent.SelectedItem == "Tên học sinh")
{
string sqlCode1 = "select * from DBO.FUNC_SEARCH_HS_TENHS('" + txtStudentSearch.Text + "')";
SqlCommand cmd = new SqlCommand(sqlCode1, con);
cmd.ExecuteNonQuery();
SqlDataReader dataReader = cmd.ExecuteReader();
DataTable dataTable = new DataTable();
dataTable.Load(dataReader);
dgvStudent.DataSource = dataTable;
}
if (cmbStudent.SelectedItem == "Ngày sinh")
{
string sqlCode1 = "select * from DBO.FUNC_SEARCH_HS_NGAYSINH('" + txtStudentSearch.Text + "')";
SqlCommand cmd = new SqlCommand(sqlCode1, con);
cmd.ExecuteNonQuery();
SqlDataReader dataReader = cmd.ExecuteReader();
DataTable dataTable = new DataTable();
dataTable.Load(dataReader);
dgvStudent.DataSource = dataTable;
}
if (cmbStudent.SelectedItem == "Giới tính")
{
string sqlCode1 = "select * from DBO.FUNC_SEARCH_HS_GIOITINH('" + txtStudentSearch.Text + "')";
SqlCommand cmd = new SqlCommand(sqlCode1, con);
cmd.ExecuteNonQuery();
SqlDataReader dataReader = cmd.ExecuteReader();
DataTable dataTable = new DataTable();
dataTable.Load(dataReader);
dgvStudent.DataSource = dataTable;
}
if (cmbStudent.SelectedItem == "Địa chỉ")
{
string sqlCode1 = "select * from DBO.FUNC_SEARCH_HS_DIACHI('" + txtStudentSearch.Text + "')";
SqlCommand cmd = new SqlCommand(sqlCode1, con);
cmd.ExecuteNonQuery();
SqlDataReader dataReader = cmd.ExecuteReader();
DataTable dataTable = new DataTable();
dataTable.Load(dataReader);
dgvStudent.DataSource = dataTable;
}
if (cmbStudent.SelectedItem == "Số điện thoại")
{
string sqlCode1 = "select * from DBO.FUNC_SEARCH_HS_SDT('" + txtStudentSearch.Text + "')";
SqlCommand cmd = new SqlCommand(sqlCode1, con);
cmd.ExecuteNonQuery();
SqlDataReader dataReader = cmd.ExecuteReader();
DataTable dataTable = new DataTable();
dataTable.Load(dataReader);
dgvStudent.DataSource = dataTable;
}
}
public string convertDate(string date)
{
string[] info = date.Split('/');
return info[1] + "/" + info[0] + "/" + info[2];
}
private void dgvStudent_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
int i;
i = dgvStudent.CurrentRow.Index;
txtStudentID.Text = dgvStudent.Rows[i].Cells[0].Value.ToString();
txtStudentName.Text = dgvStudent.Rows[i].Cells[1].Value.ToString();
//txtStudentBirth.Text = convertDate(dgvStudent.Rows[i].Cells[2].Value.ToString());
txtStudentBirth.Text = dgvStudent.Rows[i].Cells[2].Value.ToString();
//radGender_male.Checked = dgvStudent.Rows[i].Cells[3].Value.ToString();
txtStudentAddress.Text = dgvStudent.Rows[i].Cells[4].Value.ToString();
txtStudentPhone.Text = dgvStudent.Rows[i].Cells[5].Value.ToString();
}
public void loadDataCombobox()
{
var sqlCode1 = "Select TenLopHoc from LOPHOC";
SqlCommand cmd = new SqlCommand(sqlCode1, con);
///cmd.ExecuteNonQuery();
var dr = cmd.ExecuteReader();
var dt = new DataTable();
dt.Load(dr);
dr.Dispose();
comboBox1.ValueMember = "TenLopHoc";
comboBox1.DataSource = dt;
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string sqlCode = "SELECT MaLopHoc FROM LOPHOC WHERE TenLopHoc = '" + comboBox1.SelectedValue.ToString() + "'";
SqlCommand cmd = new SqlCommand(sqlCode, con);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
textmalop.Text = dr.GetValue(0).ToString();
}
dr.Close();
}
}
}