-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathForm4.cs
More file actions
237 lines (192 loc) · 7.35 KB
/
Copy pathForm4.cs
File metadata and controls
237 lines (192 loc) · 7.35 KB
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace UniversityTranscriptCreator
{
public partial class SubjectForm : Form
{
private SqlConnection cnn;
private string connectionString;
public SubjectForm()
{
InitializeComponent();
connectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Jerry Allan Akshay\Documents\GitHub\University Transcript Creator\UniversityTranscriptCreator\Database2.mdf;Integrated Security=True";
cnn = new SqlConnection(connectionString);
//cnn.Open();
SubjectGroupBox.Enabled = false;
FillStudentData("select * from dbo.Subject;");
}
void FillStudentData(string query)
{
using (SqlConnection sqlCon = new SqlConnection(connectionString))
{
sqlCon.Open();
SqlDataAdapter sqlDa = new SqlDataAdapter(query, sqlCon);
DataTable dtb = new DataTable();
sqlDa.Fill(dtb);
DataGridSubject.DataSource = dtb;
sqlCon.Close();
}
}
private void AddButton_Click(object sender, EventArgs e)
{
SqlConnection sqlConnection1 = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
SqlCommand command;
string sql;
//MessageBox.Show(date);
if (!AddCode.Text.Trim().Equals("") && !AddSubjectName.Text.Trim().Equals(""))
{
cmd.CommandText = "insert dbo.Subject (Code, Title) values (\'" + AddCode.Text.Trim() + "\', \'" + AddSubjectName.Text.Trim() + "\');";
cmd.Connection = sqlConnection1;
sqlConnection1.Open();
try
{
cmd.ExecuteNonQuery();
MessageBox.Show("Subject Successfully Added.");
FillStudentData("select * from dbo.Subject;");
AddCode.Text = "";
AddSubjectName.Text = "";
}
catch (Exception ex)
{
MessageBox.Show("Unable to add Subject. Kindly check the details again and try again.");
}
sqlConnection1.Close();
}
else
{
MessageBox.Show("Please fill in all the fields.");
}
}
private void button1_Click(object sender, EventArgs e)
{
cnn.Open();
CodeModify.Enabled = false;
button1.Enabled = false;
string code_string = CodeModify.Text.Trim();
SqlCommand command;
SqlDataReader dataReader;
string sql;
sql = "Select * from dbo.Subject where Code=\'" + code_string + "\';";
command = new SqlCommand(sql, cnn);
try
{
dataReader = command.ExecuteReader();
if (dataReader.HasRows)
{
SubjectGroupBox.Enabled = true;
while (dataReader.Read())
{
SubjectNameModify.Text = dataReader.GetValue(1).ToString();
}
}
else
{
MessageBox.Show("Subject not found.");
SubjectGroupBox.Enabled = false;
CodeModify.Enabled = true;
button1.Enabled = true;
}
dataReader.Close();
}
catch (Exception ex)
{
//MessageBox.Show(ex.ToString());
SubjectGroupBox.Enabled = false;
CodeModify.Enabled = true;
button1.Enabled = true;
}
cnn.Close();
}
private void UpdateSubject_Click(object sender, EventArgs e)
{
cnn.Open();
SqlCommand command;
SqlDataReader dataReader;
string sql;
sql = "Update dbo.Subject set Title=\'" + SubjectNameModify.Text.Trim() + "\' where Code=\'" + CodeModify.Text.Trim() + "\';";
command = new SqlCommand(sql, cnn);
try
{
dataReader = command.ExecuteReader();
MessageBox.Show("Details Successfully updated.");
FillStudentData("select * from dbo.Subject;");
SubjectGroupBox.Enabled = false;
dataReader.Close();
CodeModify.Enabled = true;
button1.Enabled = true;
SubjectNameModify.Text = "";
}
catch (Exception ex)
{
MessageBox.Show("Unable to Update Details. Kindly check the details again and try again.");
}
cnn.Close();
}
private void DeleteSubject_Click(object sender, EventArgs e)
{
SqlConnection sqlConnection1 = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "delete from dbo.Subject where Code=\'" + CodeModify.Text.Trim() + "\';";
cmd.Connection = sqlConnection1;
sqlConnection1.Open();
try
{
cmd.ExecuteNonQuery();
MessageBox.Show("Subject Successfully Deleted.");
FillStudentData("select * from dbo.Subject;");
CodeModify.Enabled = true;
button1.Enabled = true;
SubjectNameModify.Text = "";
SubjectGroupBox.Enabled = false;
}
catch (Exception ex)
{
MessageBox.Show("Unable to delete Subject.");
}
sqlConnection1.Close();
}
private void SearchButton_Click(object sender, EventArgs e)
{
string namequery = SubjectNameView.Text.Trim();
string regnoquery = CodeView.Text.Trim();
if (!namequery.Equals(""))
{
namequery = " Title like \'%" + namequery + "%\' and";
}
if (!regnoquery.Equals(""))
{
regnoquery = " Code like \'%" + regnoquery + "%\' and";
}
string query = "select * from dbo.Subject where" + namequery + regnoquery;
query = query.Substring(0, query.Length - 3) + ";";
//MessageBox.Show(query);
if (namequery.Equals("") && regnoquery.Equals(""))
{
FillStudentData("select * from dbo.Subject;");
}
else
{
try
{
FillStudentData(query);
}
catch (Exception ex)
{
MessageBox.Show("An error occured with the query. Kindly try again.");
FillStudentData("select * from dbo.Subject;");
}
}
}
}
}