-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAddEmployee.cs
388 lines (327 loc) · 15.3 KB
/
AddEmployee.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
386
387
388
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;
using System.Runtime.Remoting.Contexts;
using System.Data.Common;
namespace Workforce_
{
public partial class AddEmployee : UserControl
{
SqlConnection connect = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\devar\Documents\SQLQuery1.mdf;Integrated Security=True;Connect Timeout=30");
private void label3_Click(object sender, EventArgs e)
{
}
private void label6_Click(object sender, EventArgs e)
{
}
private void label5_Click(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
public AddEmployee()
{
InitializeComponent();
displayEmployeeData();
}
public void RefreshData()
{
if (InvokeRequired)
{
Invoke((MethodInvoker)RefreshData);
return;
}
displayEmployeeData();
}
public void displayEmployeeData()
{
EmployeeData ed = new EmployeeData();
List<EmployeeData> listData = ed.employeeListData();
dataGridView1.DataSource = listData;
}
private void addEmployee_addBtn_Click(object sender, EventArgs e)
{
if (addEmployee_id.Text == ""
|| addEmployee_fullName.Text == ""
|| addEmployee_gender.Text == ""
|| addEmployee_phoneNum.Text == ""
|| addEmployee_position.Text == ""
|| addEmployee_status.Text == ""
|| addEmployee_picture.Image == null)
{
MessageBox.Show("Please fill all blank fields"
, "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
if (connect.State == ConnectionState.Closed)
{
try
{
connect.Open();
string checkEmID = "SELECT COUNT(*) FROM employees WHERE employee_id = @emID AND delete_date IS NULL";
using (SqlCommand checkEm = new SqlCommand(checkEmID, connect))
{
checkEm.Parameters.AddWithValue("@emID", addEmployee_id.Text.Trim());
int count = (int)checkEm.ExecuteScalar();
if (count >= 1)
{
MessageBox.Show(addEmployee_id.Text.Trim() + " is already taken"
, "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
DateTime today = DateTime.Today;
string insertData = "INSERT INTO employees " +
"(employee_id, full_name, gender, contact_number" +
", position, image, salary, insert_date, status_) " +
"VALUES(@employeeID, @fullName, @gender, @contactNum" +
", @position, @image, @salary, @insertDate, @status_)";
string path = Path.Combine(@"C:\Users\devar\source\repos\Workforce_\Directory\"
+ addEmployee_id.Text.Trim() + ".jpg");
string directoryPath = Path.GetDirectoryName(path);
if (!Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
}
File.Copy(addEmployee_picture.ImageLocation, path, true);
using (SqlCommand cmd = new SqlCommand(insertData, connect))
{
cmd.Parameters.AddWithValue("@employeeID", addEmployee_id.Text.Trim());
cmd.Parameters.AddWithValue("@fullName", addEmployee_fullName.Text.Trim());
cmd.Parameters.AddWithValue("@gender", addEmployee_gender.Text.Trim());
cmd.Parameters.AddWithValue("@contactNum", addEmployee_phoneNum.Text.Trim());
cmd.Parameters.AddWithValue("@position", addEmployee_position.Text.Trim());
cmd.Parameters.AddWithValue("@image", path);
cmd.Parameters.AddWithValue("@salary", 0);
cmd.Parameters.AddWithValue("@insertDate", today);
cmd.Parameters.AddWithValue("@status_", addEmployee_status.Text.Trim());
cmd.ExecuteNonQuery();
displayEmployeeData();
MessageBox.Show("Added successfully!"
, "Information Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
clearFields();
}
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex
, "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
connect.Close();
}
}
}
}
private void addEmployee_importBtn_Click(object sender, EventArgs e)
{
try
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "Image Files (*.jpg; *.png)|*.jpg;*.png";
string imagePath = "";
if (dialog.ShowDialog() == DialogResult.OK)
{
imagePath = dialog.FileName;
addEmployee_picture.ImageLocation = imagePath;
}
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex, "Error Message"
, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex != -1)
{
DataGridViewRow row = dataGridView1.Rows[e.RowIndex];
addEmployee_id.Text = row.Cells[1].Value.ToString();
addEmployee_fullName.Text = row.Cells[2].Value.ToString();
addEmployee_gender.Text = row.Cells[3].Value.ToString();
addEmployee_phoneNum.Text = row.Cells[4].Value.ToString();
addEmployee_position.Text = row.Cells[5].Value.ToString();
string imagePath = row.Cells[6].Value.ToString();
if (imagePath != null)
{
addEmployee_picture.Image = Image.FromFile(imagePath);
}
else
{
addEmployee_picture.Image = null;
}
addEmployee_status.Text = row.Cells[8].Value.ToString();
}
}
private void addEmployee_clearBtn_Click(object sender, EventArgs e)
{
clearFields();
}
private void addEmployee_deleteBtn_Click(object sender, EventArgs e)
{
if (addEmployee_id.Text == ""
|| addEmployee_fullName.Text == ""
|| addEmployee_gender.Text == ""
|| addEmployee_phoneNum.Text == ""
|| addEmployee_position.Text == ""
|| addEmployee_status.Text == ""
|| addEmployee_picture.Image == null)
{
MessageBox.Show("Please fill all blank fields"
, "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
DialogResult check = MessageBox.Show("Are you sure you want to DELETE " +
"Employee ID: " + addEmployee_id.Text.Trim() + "?", "Confirmation Message"
, MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (check == DialogResult.Yes)
{
try
{
connect.Open();
DateTime today = DateTime.Today;
string updateData = "UPDATE employees SET delete_date = @deleteDate " +
"WHERE employee_id = @employeeID";
using (SqlCommand cmd = new SqlCommand(updateData, connect))
{
cmd.Parameters.AddWithValue("@deleteDate", today);
cmd.Parameters.AddWithValue("@employeeID", addEmployee_id.Text.Trim());
cmd.ExecuteNonQuery();
displayEmployeeData();
MessageBox.Show("Update successfully!"
, "Information Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
clearFields();
}
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex
, "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
connect.Close();
}
}
else
{
MessageBox.Show("Cancelled."
, "Information Message", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}
public void clearFields()
{
addEmployee_id.Text = "";
addEmployee_fullName.Text = "";
addEmployee_gender.SelectedIndex = -1;
addEmployee_phoneNum.Text = "";
addEmployee_position.SelectedIndex = -1;
addEmployee_status.SelectedIndex = -1;
addEmployee_picture.Image = null;
}
private void addEmployee_updateBtn_Click(object sender, EventArgs e)
{
if (addEmployee_id.Text == ""
|| addEmployee_fullName.Text == ""
|| addEmployee_gender.Text == ""
|| addEmployee_phoneNum.Text == ""
|| addEmployee_position.Text == ""
|| addEmployee_status.Text == ""
|| addEmployee_picture.Image == null)
{
MessageBox.Show("Please fill all blank fields", "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
DialogResult check = MessageBox.Show("Are you sure you want to UPDATE " +
"Employee ID: " + addEmployee_id.Text.Trim() + "?", "Confirmation Message",
MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (check == DialogResult.Yes)
{
try
{
connect.Open();
DateTime today = DateTime.Today;
string updateData = "UPDATE employees SET full_name = @fullName, " +
"gender = @gender, contact_number = @contactNum, " +
"position = @position, update_date = @updateDate " +
"WHERE employee_id = @employeeID";
using (SqlCommand cmd = new SqlCommand(updateData, connect))
{
cmd.Parameters.AddWithValue("@fullName", addEmployee_fullName.Text.Trim());
cmd.Parameters.AddWithValue("@gender", addEmployee_gender.Text.Trim());
cmd.Parameters.AddWithValue("@contactNum", addEmployee_phoneNum.Text.Trim());
cmd.Parameters.AddWithValue("@position", addEmployee_position.Text.Trim());
cmd.Parameters.AddWithValue("@status", addEmployee_status.Text.Trim());
cmd.Parameters.AddWithValue("@updateDate", today);
cmd.Parameters.AddWithValue("@employeeID", addEmployee_id.Text.Trim());
cmd.ExecuteNonQuery();
displayEmployeeData();
MessageBox.Show("Update successfully!", "Information Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
clearFields();
}
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message, "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
connect.Close();
}
}
else
{
MessageBox.Show("Cancelled.", "Information Message", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}
private void dataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex != -1)
{
DataGridViewRow row = dataGridView1.Rows[e.RowIndex];
addEmployee_id.Text = row.Cells[1].Value.ToString();
addEmployee_fullName.Text = row.Cells[2].Value.ToString();
addEmployee_gender.Text = row.Cells[3].Value.ToString();
addEmployee_phoneNum.Text = row.Cells[4].Value.ToString();
addEmployee_position.Text = row.Cells[5].Value.ToString();
string imagePath = row.Cells[6].Value.ToString();
if (imagePath != null)
{
addEmployee_picture.Image = Image.FromFile(imagePath);
}
else
{
addEmployee_picture.Image = null;
}
addEmployee_status.Text = row.Cells[8].Value.ToString();
}
}
private void addEmployee_id_TextChanged(object sender, EventArgs e)
{
}
}
}