/
AttendenceForm.cs
385 lines (333 loc) · 13 KB
/
AttendenceForm.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
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.Data.SqlClient;
using System.IO;
namespace MultiFaceRec
{
public partial class AttendenceForm : Form
{
public AttendenceForm()
{
InitializeComponent();
ShowSR();
getstudentId();
getcourseId();
getModulename();
}
SqlConnection Con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\mhamz\Documents\FinalYearProjectdb.mdf;Integrated Security=True;Connect Timeout=30");
private void AttendenceForm_Load(object sender, EventArgs e)
{
}
private void getstudentId()
{
Con.Open();
SqlCommand cmd = new SqlCommand("Select StudentId from StudentRegistrationTable", Con);
SqlDataReader r;
r = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(r);
// dt.Columns.Add("CourseId", typeof(int));
Sitxt.ValueMember = "StudentId";
Sitxt.DataSource = dt;
Con.Close();
}
public void getstudentfname()
{
Con.Open();
string Qeruy = "Select * from StudentRegistrationTable where StudentId=" + Sitxt.SelectedValue.ToString() + "";
SqlCommand cmd = new SqlCommand(Qeruy, Con);
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
Fntxt.Text = dr["Firstname"].ToString();
}
Con.Close();
}
public void getstudentsname()
{
Con.Open();
string Qeruy = "Select * from StudentRegistrationTable where StudentId=" + Sitxt.SelectedValue.ToString() + "";
SqlCommand cmd = new SqlCommand(Qeruy, Con);
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
Sntxt.Text = dr["Surname"].ToString();
}
Con.Close();
}
private void getcourseId()
{
Con.Open();
SqlCommand cmd = new SqlCommand("Select CourseId from CourseTable", Con);
SqlDataReader r;
r = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(r);
// dt.Columns.Add("CourseId", typeof(int));
Citxt.ValueMember = "CourseId";
Citxt.DataSource = dt;
Con.Close();
}
public void getcoursename()
{
Con.Open();
string Qeruy = "Select * from CourseTable where CourseId=" + Citxt.SelectedValue.ToString() + "";
SqlCommand cmd = new SqlCommand(Qeruy, Con);
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
Cotxt.Text = dr["Coursename"].ToString();
}
Con.Close();
}
private void getModulename()
{
Con.Open();
string Qeruy = "Select * from ModuleTable where CourseId=" + Citxt.SelectedValue.ToString() + "";
SqlCommand cmd = new SqlCommand(Qeruy, Con);
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
Mdtxt.Text = dr["Modulename"].ToString();
}
Con.Close();
/* Con.Open();
SqlCommand cmd = new SqlCommand("Select Modulename from ModuleTable", Con);
SqlDataReader r;
r = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(r);
// dt.Columns.Add("CourseId", typeof(int));
Mdtxt.ValueMember = "Modulename";
Mdtxt.DataSource = dt;
Con.Close();
*/
}
private void ShowSR()
{
Con.Open();
String Query = "Select * from AttendenceTable";
SqlDataAdapter sda = new SqlDataAdapter(Query, Con);
SqlCommandBuilder builder = new SqlCommandBuilder(sda);
var ds = new DataSet();
sda.Fill(ds);
StDGV.DataSource = ds.Tables[0];
Con.Close();
}
private void Reset()
{
Attxt.Text = "";
Sitxt.Text = "";
Fntxt.Text = "";
Sntxt.Text = "";
Datxt.Text = "";
Cotxt.Text = "";
Citxt.Text = "";
Mdtxt.Text = "";
Sttxt.Text = "";
}
private void StDGV_CellClick(object sender, DataGridViewCellEventArgs e)
{
StDGV.CurrentRow.Selected = true;
Sitxt.Text = StDGV.Rows[e.RowIndex].Cells["AttendenceId"].Value.ToString();
Sitxt.Text = StDGV.Rows[e.RowIndex].Cells["StudentId"].Value.ToString();
Fntxt.Text = StDGV.Rows[e.RowIndex].Cells["Firstname"].Value.ToString();
Sntxt.Text = StDGV.Rows[e.RowIndex].Cells["Surname"].Value.ToString();
Datxt.Text = StDGV.Rows[e.RowIndex].Cells["Date"].Value.ToString();
Cotxt.Text = StDGV.Rows[e.RowIndex].Cells["Coursename"].Value.ToString();
Citxt.Text = StDGV.Rows[e.RowIndex].Cells["CourseId"].Value.ToString();
Mdtxt.Text = StDGV.Rows[e.RowIndex].Cells["Modulename"].Value.ToString();
Sttxt.Text = StDGV.Rows[e.RowIndex].Cells["Status"].Value.ToString();
}
private void Sitxt_SelectionChangeCommitted(object sender, EventArgs e)
{
getstudentfname();
getstudentsname();
}
private void Citxt_SelectionChangeCommitted(object sender, EventArgs e)
{
getcoursename();
getModulename();
}
private void Cbtn_Click(object sender, EventArgs e)
{
Reset();
}
private void lobnt_Click(object sender, EventArgs e)
{
ShowSR();
}
private void Stremovebtn_Click(object sender, EventArgs e)
{
if (Fntxt.Text.Length > 0)
{
Con.Open();
SqlCommand cmd = Con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "delete from AttendenceTable where Firstname='" + Fntxt.Text + "'";
cmd.ExecuteNonQuery();
Con.Close();
ShowSR();
MessageBox.Show("Record deleted Successfully");
Reset();
}
else
{
MessageBox.Show("No record is selscted");
}
}
private void Stupdatebtn_Click(object sender, EventArgs e)
{
if (Cotxt.Text == "" || Citxt.Text == "" || Sttxt.Text == "" || Mdtxt.Text == "" || Fntxt.Text == "" || Sntxt.Text == "")
{
MessageBox.Show("Missing Inforamtion");
}
else
{
try
{
SqlDataAdapter sda = new SqlDataAdapter();
Con.Open();
sda.UpdateCommand = new SqlCommand("update AttendenceTable set CourseId='" + Citxt.Text + "',Coursename='" + Cotxt.Text + "',Modulename='" + Mdtxt.Text + "',Status='" + Sttxt.Text + "' where AttendenceId='" + Attxt.Text + "' ", Con);
sda.UpdateCommand.ExecuteNonQuery();
Con.Close();
MessageBox.Show("record updated");
ShowSR();
Reset();
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message);
}
}
}
private void StAddbtn_Click(object sender, EventArgs e)
{
if (Fntxt.Text == "" || Sntxt.Text == "" || Datxt.Text == "" || Citxt.Text == "" || Sttxt.Text == "" || Mdtxt.Text == "")
{
MessageBox.Show("Missing Inforamtion");
}
else
{
try
{
Con.Open();
SqlCommand cmd = new SqlCommand("Insert into AttendenceTable(StudentId,Firstname,Surname,Date,CourseId,Coursename,Modulename,Status)Values(@SI,@DF,@DS,@DA,@DI,@DC,@DN,@DSA)", Con);
cmd.Parameters.AddWithValue("@SI", Sitxt.Text);
cmd.Parameters.AddWithValue("@DF", Fntxt.Text);
cmd.Parameters.AddWithValue("@DS", Sntxt.Text);
cmd.Parameters.AddWithValue("@DA", Datxt.Text);
cmd.Parameters.AddWithValue("@DI", Citxt.Text);
cmd.Parameters.AddWithValue("@DC", Cotxt.Text);
cmd.Parameters.AddWithValue("@DN", Mdtxt.Text);
cmd.Parameters.AddWithValue("@DSA", Sttxt.Text);
cmd.ExecuteNonQuery();
MessageBox.Show("Attendence recorded Sucessfully");
Con.Close();
ShowSR();
Reset();
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message);
}
}
}
private void Stsearchbtn_Click(object sender, EventArgs e)
{
Con.Open();
SqlCommand cmd = Con.CreateCommand();
cmd.CommandType = CommandType.Text;
try
{
if (Fntxt.Text.Length > 0)
{
cmd.CommandText = "select * from AttendenceTable where Firstname='" + Fntxt.Text + "'";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
StDGV.DataSource = dt;
}
else if (Sntxt.Text.Length > 0)
{
cmd.CommandText = "select * from AttendenceTable where Surname='" + Sntxt.Text + "'";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
StDGV.DataSource = dt;
}
else if (Datxt.Text.Length > 0)
{
cmd.CommandText = "select * from AttendenceTable where Date='" + Datxt.Text + "'";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
StDGV.DataSource = dt;
}
else if (Citxt.Text.Length > 0)
{
cmd.CommandText = "select * from AttendenceTable where CourseId='" + Citxt.Text + "'";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
StDGV.DataSource = dt;
}
else if (Cotxt.Text.Length > 0)
{
cmd.CommandText = "select * from AttendenceTable where Coursename='" + Cotxt.Text + "'";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
StDGV.DataSource = dt;
}
else if (Mdtxt.Text.Length > 0)
{
cmd.CommandText = "select * from AttendenceTable where Modulename='" + Mdtxt.Text + "'";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
StDGV.DataSource = dt;
}
else if (Sttxt.Text.Length > 0)
{
cmd.CommandText = "select * from AttendenceTable where Status='" + Sttxt.Text + "'";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
StDGV.DataSource = dt;
}
else
{
MessageBox.Show("No Information is provided to search the records");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
Con.Close();
}
}//
}