-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStudent.cs
202 lines (191 loc) · 6.67 KB
/
Student.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
using System;
using System.Data.SqlClient;
using System.Configuration;
using System.Windows.Forms;
using System.Data;
namespace AdoDotNetProject
{
public partial class Student : Form
{
SqlConnection con;
SqlCommand cmd;
SqlDataReader dr;
public Student()
{
InitializeComponent();
con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString);
}
private void btnSave_Click(object sender, EventArgs e)
{
try
{
// step 2 - > write the sql query
string qry = "insert into Student values(@name,@stream,@percentage,@age,@gender)";
//step 3 - assign the query to command class
cmd = new SqlCommand(qry, con);
// ste-4 assign values to the parameter
cmd.Parameters.AddWithValue("@name", txtname.Text);
cmd.Parameters.AddWithValue("@stream", txtstream.Text);
cmd.Parameters.AddWithValue("@percentage", Convert.ToInt32(txtpercentage.Text));
cmd.Parameters.AddWithValue("@age", Convert.ToInt32(txtage.Text));
cmd.Parameters.AddWithValue("@gender", txtgender.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 inserted");
ClearForm();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
// step 7
con.Close();
}
}
private void ClearForm()
{
txtrollNo.Clear();
txtname.Clear();
txtstream.Clear();
txtpercentage.Clear();
txtage.Clear();
txtgender.Clear();
}
private void btnUpdate_Click(object sender, EventArgs e)
{
try
{
// step 2 - > write the sql query
string qry = "update Student set name=@name,stream=@stream, percentage=@percentage, age=@age, gender=@gender where rollNo=@rollNo";
//step 3 - assign the query to command class
cmd = new SqlCommand(qry, con);
// step-4 assing values to the parameter
cmd.Parameters.AddWithValue("@name", txtname.Text);
cmd.Parameters.AddWithValue("@stream", txtstream.Text);
cmd.Parameters.AddWithValue("@percentage", Convert.ToInt32(txtpercentage.Text));
cmd.Parameters.AddWithValue("@age", Convert.ToInt32(txtage.Text));
cmd.Parameters.AddWithValue("@gender", txtgender.Text);
cmd.Parameters.AddWithValue("@rollNo", Convert.ToInt32(txtrollNo.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 Student where rollNo=@rollNo";
//step 3 - assign the query to command class
cmd = new SqlCommand(qry, con);
// step-4 assing values to the parameter
cmd.Parameters.AddWithValue("@rollNo", Convert.ToInt32(txtrollNo.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 Student where rollNo =@rollNo";
cmd = new SqlCommand(qry, con);
cmd.Parameters.AddWithValue("@rollNo", Convert.ToInt32(txtrollNo.Text));
con.Open();
dr = cmd.ExecuteReader(); // fire the select query
if (dr.HasRows)
{
while (dr.Read())
{
txtname.Text = dr["name"].ToString();
txtstream.Text = dr["stream"].ToString();
txtpercentage.Text = dr["percentage"].ToString();
txtage.Text = dr["age"].ToString();
txtgender.Text = dr["gender"].ToString();
}
}
else
{
MessageBox.Show("Record not found");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
// step 7
con.Close();
}
}
private void btnShowDetails_Click(object sender, EventArgs e)
{
try
{
string qry = "select * from Student";
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();
}
}
}
}