-
Notifications
You must be signed in to change notification settings - Fork 0
/
Employee.cs
223 lines (202 loc) · 6.99 KB
/
Employee.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
using System;
using System.Data.SqlClient;
using System.Configuration;
using System.Windows.Forms;
using System.Data;
namespace AdoDotNetProject
{
public partial class Employee : Form
{
SqlConnection con;
SqlCommand cmd;
SqlDataReader dr;
public Employee()
{
InitializeComponent();
con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString);
}
private void txtempId_TextChanged(object sender, EventArgs e)
{
}
private void btnSave_Click(object sender, EventArgs e)
{
try
{
// step 2--> write the sql query
string qry = "insert into Employee values(@empName,@deptList,@empSalary,@empAge)";
// step 3--> assign the query to command class
cmd = new SqlCommand(qry, con);
//step4 --> assign values to parameter
cmd.Parameters.AddWithValue("empName",txtempName.Text);
cmd.Parameters.AddWithValue("deptList",txtdeptList.Text);
cmd.Parameters.AddWithValue("empSalary",Convert.ToInt32(txtempSalary.Text));
cmd.Parameters.AddWithValue("empAge",Convert.ToInt32(txtempAge.Text));
//step 5--> open connection
con.Open();
//step 6--> fire the query
int result=cmd.ExecuteNonQuery();// fire the insert/update/delete
if (result == 1)
{
MessageBox.Show("Record Inserted");
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
// step 7--> close the connection
con.Close();
}
}
private void ClearForm()
{
txtempId.Clear();
txtempName.Clear();
txtdeptList.Clear();
txtempSalary.Clear();
txtempAge.Clear();
}
private void txtempName_TextChanged(object sender, EventArgs e)
{
}
private void txtdeptList_TextChanged(object sender, EventArgs e)
{
}
private void txtempSalary_TextChanged(object sender, EventArgs e)
{
}
private void txtempAge_TextChanged(object sender, EventArgs e)
{
}
private void btnUpdate_Click(object sender, EventArgs e)
{
try
{
// step 2 - > write the sql query
string qry = "update Employee set empName=@empName, deptList=@deptList, empSalary=@empSalary, empAge=@empAge where empId=@empId";
//step 3 - assign the query to command class
cmd = new SqlCommand(qry, con);
// step-4 assing values to the parameter
cmd.Parameters.AddWithValue("@empName", txtempName.Text);
cmd.Parameters.AddWithValue("@deptList", txtdeptList.Text);
cmd.Parameters.AddWithValue("@empSalary", Convert.ToInt32(txtempSalary.Text));
cmd.Parameters.AddWithValue("@empAge", Convert.ToInt32(txtempAge.Text));
cmd.Parameters.AddWithValue("@empId", Convert.ToInt32(txtempId.Text));
// step 5 - open conn
con.Open();
// step 6 - fire the query
int result = cmd.ExecuteNonQuery(); // fire the insert /update / delete
if (result == 1)
{
MessageBox.Show("Record updated");
ClearForm();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
// step 7
con.Close();
}
}
private void btnDelete_Click(object sender, EventArgs e)
{
try
{
// step 2 - > write the sql query
string qry = "delete from Employee where empId=@empId";
//step 3 - assign the query to command class
cmd = new SqlCommand(qry, con);
// step-4 assing values to the parameter
cmd.Parameters.AddWithValue("@empId", Convert.ToInt32(txtempId.Text));
// step 5 - open conn
con.Open();
// step 6 - fire the query
int result = cmd.ExecuteNonQuery(); // fire the insert /update / delete
if (result == 1)
{
MessageBox.Show("Record deleted");
ClearForm();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
// step 7
con.Close();
}
}
private void btnSearch_Click(object sender, EventArgs e)
{
try
{
string qry = "select * from Employee where empId =@empId";
cmd = new SqlCommand(qry, con);
cmd.Parameters.AddWithValue("@empId", Convert.ToInt32(txtempId.Text));
con.Open();
dr = cmd.ExecuteReader(); // fire the select query
if (dr.HasRows)
{
while (dr.Read())
{
txtempId.Text = dr["empId"].ToString();
txtempName.Text = dr["empName"].ToString();
txtdeptList.Text = dr["deptList"].ToString();
txtempSalary.Text = dr["empSalary"].ToString();
txtempAge.Text = dr["empAge"].ToString();
}
}
else
{
MessageBox.Show("Record not found");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
// step 7
con.Close();
}
}
private void btnShowAll_Click(object sender, EventArgs e)
{
try
{
string qry = "select * from Employee";
cmd = new SqlCommand(qry, con);
con.Open();
dr = cmd.ExecuteReader(); // fire the select query
if (dr.HasRows)
{
DataTable table = new DataTable();
table.Load(dr);
dataGridView1.DataSource = table;
}
else
{
MessageBox.Show("Record not found");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
// step 7
con.Close();
}
}
}
}