-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdminForm.cs
207 lines (187 loc) · 6.81 KB
/
AdminForm.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
using System;
using System.Data;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Gamey
{
public partial class AdminForm : Form
{
[DllImport("user32.DLL", EntryPoint = "ReleaseCapture")]
private extern static void ReleaseCapture();
[DllImport("user32.DLL", EntryPoint = "SendMessage")]
private extern static void SendMessage(System.IntPtr hWnd, int wMsg, int wParam, int lParam);
[DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
private static extern IntPtr CreateRoundRectRgn
(
int nLeftRect,
int nTopRect,
int nRightRect,
int nBottomRect,
int nWidthEllipse,
int nHeightEllipse
);
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\StudyMe\StudyMeDB.mdf;Integrated Security=True;Connect Timeout=30");
public AdminForm()
{
InitializeComponent();
Region = Region.FromHrgn(CreateRoundRectRgn(0, 0, Width, Height, 25, 25));
timeTimer.Start();
}
private void AdminForm_Load(object sender, EventArgs e)
{
Pobulate();
}
private void logOutButton_Click(object sender, EventArgs e)
{
this.Hide();
LoginForm loginForm = new LoginForm();
loginForm.Show();
}
private void timeTimer_Tick(object sender, EventArgs e)
{
timeLabel.Text = DateTime.Now.ToString("HH:mm");
}
private void exitButton_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void minButton_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
}
private void clearButton_Click(object sender, EventArgs e)
{
nameBox.Text = "";
passBox.Text = "";
}
private void searchBox_TextChanged(object sender, EventArgs e)
{
con.Open();
string query = "select * from adminTable";
SqlDataAdapter sda = new SqlDataAdapter(query, con);
SqlCommandBuilder scd = new SqlCommandBuilder(sda);
DataSet ds = new DataSet();
BindingSource bs = new BindingSource();
sda.Fill(ds);
bs.DataSource = ds.Tables[0];
adminGrid.DataSource = bs;
con.Close();
//Actual search
bs.Filter = "AdminName LIKE '" + searchBox.Text + "%'";
}
private void addButton_Click(object sender, EventArgs e)
{
try
{
con.Open();
if (String.IsNullOrEmpty(nameBox.Text) || String.IsNullOrEmpty(passBox.Text))
{
MessageBox.Show("Missing fields were found. Please make sure all fields are filled");
}
else if (nameBox.Text == "admin" && passBox.Text == "admin")
{
MessageBox.Show("Please choose another username and password");
}
else
{
string query = "insert into adminTable values('" + nameBox.Text + "','" + passBox.Text + "')";
SqlCommand cmd = new SqlCommand(query, con);
cmd.ExecuteNonQuery();
MessageBox.Show("Admin added successfully");
nameBox.Clear();
passBox.Clear();
}
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
Pobulate();
}
private void Pobulate()
{
con.Open();
string query = "select * from adminTable";
SqlDataAdapter sda = new SqlDataAdapter(query, con);
SqlCommandBuilder scd = new SqlCommandBuilder(sda);
DataSet ds = new DataSet();
BindingSource bs = new BindingSource();
sda.Fill(ds);
bs.DataSource = ds.Tables[0];
adminGrid.DataSource = bs;
con.Close();
}
private void editButton_Click(object sender, EventArgs e)
{
try
{
if (String.IsNullOrEmpty(nameBox.Text) || String.IsNullOrEmpty(passBox.Text))
{
MessageBox.Show("Missing fields were found. Please make sure all fields are filled");
}
else
{
con.Open();
string query = "update adminTable set AdminName=@name,AdminPass=@pass where AdminName=@name";
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@name", nameBox.Text);
cmd.Parameters.AddWithValue("@pass", passBox.Text);
cmd.ExecuteNonQuery();
MessageBox.Show("Admin updated successfully");
con.Close();
Pobulate();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void deleteButton_Click(object sender, EventArgs e)
{
try
{
if (nameBox.Text == "")
{
MessageBox.Show("Please select an admin to delete");
}
else
{
con.Open();
string query = "DELETE FROM adminTable WHERE AdminName LIKE '" + nameBox.Text + "'";
SqlCommand cmd = new SqlCommand(query, con);
cmd.ExecuteNonQuery();
MessageBox.Show("Admin deleted successfully");
con.Close();
Pobulate();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void adminGrid_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex != -1)
{
DataGridViewRow adminRow = adminGrid.Rows[e.RowIndex];
nameBox.Text = adminRow.Cells[0].Value.ToString();
passBox.Text = adminRow.Cells[1].Value.ToString();
}
}
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
ReleaseCapture();
SendMessage(this.Handle, 0x112, 0xf012, 0);
}
private void AdminForm_MouseDown(object sender, MouseEventArgs e)
{
ReleaseCapture();
SendMessage(this.Handle, 0x112, 0xf012, 0);
}
}
}