Skip to content

Commit 7184edc

Browse files
committed
Auto increment of employee id feature added.
1 parent adaf379 commit 7184edc

File tree

7 files changed

+410
-369
lines changed

7 files changed

+410
-369
lines changed

EmployeeManagementSystem.Data/Employee.cs

+12-12
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,25 @@ public Employee()
1010
{
1111
}
1212

13-
public Employee(string id, string fullName, string address, string contact, string email, string designation, string department, string dateOfJoin,
13+
public Employee(int id, string fullName, string address, string contact, string email, string designation, string department, string dateOfJoin,
1414
string wageRate, string workedHour)
1515
{
16-
this.EmployeeID = id;
17-
this.FullName = fullName;
18-
this.Address = address;
19-
this.Contact = contact;
20-
this.Email = email;
21-
this.Designation = designation;
22-
this.Department = department;
23-
this.DateOfJoin = string.IsNullOrEmpty(dateOfJoin) ? DateTime.Now.ToShortDateString() : dateOfJoin;
24-
this.WageRate = wageRate;
25-
this.WorkedHour = workedHour;
16+
EmployeeID = id;
17+
FullName = fullName;
18+
Address = address;
19+
Contact = contact;
20+
Email = email;
21+
Designation = designation;
22+
Department = department;
23+
DateOfJoin = string.IsNullOrEmpty(dateOfJoin) ? DateTime.Now.ToShortDateString() : dateOfJoin;
24+
WageRate = wageRate;
25+
WorkedHour = workedHour;
2626
}
2727

2828
[Key]
2929
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
3030
public Guid Id { get; set; }
31-
public string EmployeeID { get; set; }
31+
public int EmployeeID { get; set; }
3232
public string FullName { get; set; }
3333
public string Address { get; set; }
3434
public string Contact { get; set; }

EmployeeManagementSystem.Data/EmployeeManagementContext.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public EmployeeManagementContext()
1313

1414
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
1515
{
16-
optionsBuilder.UseSqlServer(@"Server=kirans;Database=EmployeeManagement;Trusted_Connection=True;TrustServerCertificate=True;");
16+
optionsBuilder.UseSqlServer(@"Server=localhost;Database=EmployeeManagement;Trusted_Connection=True;TrustServerCertificate=True;");
1717
}
1818
}
1919
}

EmployeeManagementSystem/AddEmployee.Designer.cs

+334-311
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

EmployeeManagementSystem/AddEmployee.cs

+9-5
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ public partial class AddEmployee : Form
2222
//Delegate has been added
2323
public delegate void IdentityHandler(object sender, IdentityEventArgs e);
2424

25-
25+
2626
//Event of the delegate type has been added. i.e. Object of delegate created
2727
public event IdentityHandler IdentityUpdated;
2828

29-
public AddEmployee()
29+
public AddEmployee(int nextEmployeID)
3030
{
3131
InitializeComponent();
3232
comboBoxDepartment.Items.Add("Administrative");
@@ -35,6 +35,10 @@ public AddEmployee()
3535
comboBoxDepartment.Items.Add("Marketing");
3636
comboBoxDepartment.Items.Add("IT");
3737
comboBoxDepartment.SelectedIndex = 0;
38+
if (nextEmployeID != 0)
39+
{
40+
txtIdNo.Text = nextEmployeID.ToString();
41+
}
3842
}
3943

4044
//This method will set the values on controls received from the selected row.
@@ -82,7 +86,7 @@ private void LblClose_Click(object sender, EventArgs e)
8286

8387
private async void BtnSave_Click(object sender, EventArgs e)
8488
{
85-
var id = txtIdNo.Text;
89+
var id = int.TryParse(txtIdNo.Text, out int EmployeeNo);
8690
var name = txtFullName.Text;
8791
var address = txtAddress.Text;
8892
var contactNo = txtContact.Text;
@@ -95,13 +99,13 @@ private async void BtnSave_Click(object sender, EventArgs e)
9599

96100
using (var context = new EmployeeManagementContext())
97101
{
98-
var emp = new Employee(id, name, address, contactNo, email, desigination, department, dateOfJoin, wageRate, hourWorked);
102+
var emp = new Employee(EmployeeNo, name, address, contactNo, email, desigination, department, dateOfJoin, wageRate, hourWorked);
99103
context.Employees.Add(emp);
100104
await context.SaveChangesAsync();
101105
}
102106

103107
//instance event args and value has been passed
104-
var args = new IdentityEventArgs(id, name, address, contactNo, email, desigination, department, dateOfJoin, wageRate, hourWorked);
108+
var args = new IdentityEventArgs(EmployeeNo, name, address, contactNo, email, desigination, department, dateOfJoin, wageRate, hourWorked);
105109

106110
//Event has be raised with update arguments of delegate
107111
IdentityUpdated?.Invoke(this, args);

EmployeeManagementSystem/AddEmployee.resx

+25-25
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<root>
3-
<!--
3+
<!--
44
Microsoft ResX Schema
5-
5+
66
Version 2.0
7-
8-
The primary goals of this format is to allow a simple XML format
9-
that is mostly human readable. The generation and parsing of the
10-
various data types are done through the TypeConverter classes
7+
8+
The primary goals of this format is to allow a simple XML format
9+
that is mostly human readable. The generation and parsing of the
10+
various data types are done through the TypeConverter classes
1111
associated with the data types.
12-
12+
1313
Example:
14-
14+
1515
... ado.net/XML headers & schema ...
1616
<resheader name="resmimetype">text/microsoft-resx</resheader>
1717
<resheader name="version">2.0</resheader>
@@ -26,36 +26,36 @@
2626
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
2727
<comment>This is a comment</comment>
2828
</data>
29-
30-
There are any number of "resheader" rows that contain simple
29+
30+
There are any number of "resheader" rows that contain simple
3131
name/value pairs.
32-
33-
Each data row contains a name, and value. The row also contains a
34-
type or mimetype. Type corresponds to a .NET class that support
35-
text/value conversion through the TypeConverter architecture.
36-
Classes that don't support this are serialized and stored with the
32+
33+
Each data row contains a name, and value. The row also contains a
34+
type or mimetype. Type corresponds to a .NET class that support
35+
text/value conversion through the TypeConverter architecture.
36+
Classes that don't support this are serialized and stored with the
3737
mimetype set.
38-
39-
The mimetype is used for serialized objects, and tells the
40-
ResXResourceReader how to depersist the object. This is currently not
38+
39+
The mimetype is used for serialized objects, and tells the
40+
ResXResourceReader how to depersist the object. This is currently not
4141
extensible. For a given mimetype the value must be set accordingly:
42-
43-
Note - application/x-microsoft.net.object.binary.base64 is the format
44-
that the ResXResourceWriter will generate, however the reader can
42+
43+
Note - application/x-microsoft.net.object.binary.base64 is the format
44+
that the ResXResourceWriter will generate, however the reader can
4545
read any of the formats listed below.
46-
46+
4747
mimetype: application/x-microsoft.net.object.binary.base64
48-
value : The object must be serialized with
48+
value : The object must be serialized with
4949
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
5050
: and then encoded with base64 encoding.
5151
5252
mimetype: application/x-microsoft.net.object.soap.base64
53-
value : The object must be serialized with
53+
value : The object must be serialized with
5454
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
5555
: and then encoded with base64 encoding.
5656
5757
mimetype: application/x-microsoft.net.object.bytearray.base64
58-
value : The object must be serialized into a byte array
58+
value : The object must be serialized into a byte array
5959
: using a System.ComponentModel.TypeConverter
6060
: and then encoded with base64 encoding.
6161
-->

EmployeeManagementSystem/EmployeeDetails.cs

+17-3
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ private void OnMouseUp(object sender, MouseEventArgs e)
6161

6262
private void AddEmployee_Click(object sender, EventArgs e)
6363
{
64-
var addEmp = new AddEmployee();
64+
var addEmp = new AddEmployee(GetHighestEmployeeID() + 1);
6565
addEmp.IdentityUpdated += SaveRecord;
6666
addEmp.ShowDialog();
6767
}
@@ -118,7 +118,7 @@ private void BtnEdit_Click(object sender, EventArgs e)
118118
var hourWorked = Convert.ToString(dataGridView.Rows[row].Cells[9].Value);
119119

120120

121-
var addEmp = new AddEmployee();
121+
var addEmp = new AddEmployee(0);
122122
addEmp.LoadData(id, name, address, contact, email, desigination, department, dateOfJoin, wageRate, hourWorked);
123123
addEmp.IdentityUpdated += UpdateRecord;
124124
addEmp.ShowDialog();
@@ -262,7 +262,8 @@ public async void ImportEmployeeFromCsv()
262262
{
263263
using var context = new EmployeeManagementContext();
264264
dataGridView.Rows.Add(value[0], value[1], value[2], value[3], value[4], value[5], value[6], value[7], value[8], value[9]);
265-
var emp = new Employee(value[0], value[1], value[2], value[3], value[4], value[5], value[6], value[7], value[8], value[9]);
265+
bool isParsable = int.TryParse(value[0], out int employeeNo);
266+
var emp = new Employee(isParsable ? employeeNo : 0, value[1], value[2], value[3], value[4], value[5], value[6], value[7], value[8], value[9]);
266267
context.Employees.Add(emp);
267268
try
268269
{
@@ -310,5 +311,18 @@ public void GetEmployees()
310311
}
311312
}
312313
}
314+
public int GetHighestEmployeeID()
315+
{
316+
int highestEmployeeID = 0;
317+
foreach (DataGridViewRow item in dataGridView.Rows)
318+
{
319+
bool isParsable = int.TryParse(Convert.ToString(item.Cells[0].Value), out int employeeID);
320+
if (isParsable)
321+
{
322+
highestEmployeeID = employeeID > highestEmployeeID ? employeeID : highestEmployeeID;
323+
}
324+
}
325+
return highestEmployeeID;
326+
}
313327
}
314328
}

EmployeeManagementSystem/IdentityEventArgs.cs

+12-12
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,22 @@ namespace EmployeeManagementSystem
99
*******/
1010
public class IdentityEventArgs : EventArgs
1111
{
12-
public IdentityEventArgs(string id, string fullName, string address, string contact, string email,
12+
public IdentityEventArgs(int id, string fullName, string address, string contact, string email,
1313
string designation, string department,string dateOfJoin, string wageRate, string workedHour)
1414
{
15-
this.Id = id;
16-
this.FullName = fullName;
17-
this.Address = address;
18-
this.Contact = contact;
19-
this.Email = email;
20-
this.Designation = designation;
21-
this.Department = department;
22-
this.DateOfJoin = dateOfJoin;
23-
this.WageRate = wageRate;
24-
this.WorkedHour = workedHour;
15+
Id = id;
16+
FullName = fullName;
17+
Address = address;
18+
Contact = contact;
19+
Email = email;
20+
Designation = designation;
21+
Department = department;
22+
DateOfJoin = dateOfJoin;
23+
WageRate = wageRate;
24+
WorkedHour = workedHour;
2525
}
2626

27-
public string Id { get; }
27+
public int Id { get; }
2828
public string FullName { get; }
2929
public string Address { get; }
3030
public string Contact { get; }

0 commit comments

Comments
 (0)