-
Notifications
You must be signed in to change notification settings - Fork 0
/
AdminUserControl.cs
179 lines (154 loc) · 6.04 KB
/
AdminUserControl.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
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CS322___Projektni_zadatak___Bojana___Stajić___4596
{
public partial class AdminUserControl : Form
{
MySqlConnection mycon;
string con = "datasource=localhost;port=3306;username=root;password='';database=cs322_pz";
DataTable dt = new DataTable();
public AdminUserControl()
{
InitializeComponent();
}
private void UserControl_Load(object sender, EventArgs e)
{
try
{
mycon = new MySqlConnection(con);
MySqlCommand cmd = new MySqlCommand("SELECT * FROM user", mycon);
MySqlDataAdapter adapter = new MySqlDataAdapter();
adapter.SelectCommand = cmd;
DataTable dt = new DataTable();
adapter.Fill(dt);
BindingSource bind = new BindingSource();
bind.DataSource = dt;
dgvUsers.DataSource = bind;
adapter.Update(dt);
dgvUsers.AutoResizeColumns();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
}
private void dgvUsers_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
{
DataGridViewRow row = this.dgvUsers.Rows[e.RowIndex];
Shared.user_id = int.Parse(row.Cells["id"].Value.ToString());
tbUsername.Text = row.Cells["username"].Value.ToString();
tbPassword.Text = row.Cells["password"].Value.ToString();
tbEmail.Text = row.Cells["email"].Value.ToString();
tbName.Text = row.Cells["name"].Value.ToString();
tbSurname.Text = row.Cells["surname"].Value.ToString();
}
}
private void pictureBox1_Click(object sender, EventArgs e)
{
this.Hide();
AdminPanel adminPanelForm = new AdminPanel();
adminPanelForm.Show();
adminPanelForm.FormClosed += (s, args) => this.Close();
}
private void AdminUserControl_FormClosing(object sender, FormClosingEventArgs e)
{
Application.Exit();
}
private void btnEdit_Click(object sender, EventArgs e)
{
//Update
string username = tbUsername.Text;
string password = tbPassword.Text;
string email = tbEmail.Text;
string name = tbName.Text;
string surname = tbSurname.Text;
try
{
mycon = new MySqlConnection(con);
mycon.Open();
string query = "UPDATE user SET username=@username, password=@password, email=@email, name=@name, surname=@surname WHERE id=@id";
MySqlCommand cmd = new MySqlCommand(query, mycon);
cmd.Parameters.AddWithValue("@username", username);
cmd.Parameters.AddWithValue("@password", password);
cmd.Parameters.AddWithValue("@email", email);
cmd.Parameters.AddWithValue("@name", name);
cmd.Parameters.AddWithValue("@surname", surname);
cmd.Parameters.AddWithValue("@id", Shared.user_id);
cmd.ExecuteScalar();
MessageBox.Show("Admin, you have successfully changed a user!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void btnRemove_Click(object sender, EventArgs e)
{
string username = tbUsername.Text;
try
{
mycon = new MySqlConnection(con);
mycon.Open();
string query = "DELETE FROM user WHERE username=@username";
MySqlCommand cmd = new MySqlCommand(query, mycon);
cmd.Parameters.AddWithValue("@username", username);
cmd.ExecuteScalar();
MessageBox.Show("Admin, you have successfully deleted a user!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void btnRefresh_Click(object sender, EventArgs e)
{
//Refresh
try
{
mycon = new MySqlConnection(con);
MySqlCommand cmd = new MySqlCommand("select * from user;", mycon);
MySqlDataAdapter adapter = new MySqlDataAdapter();
adapter.SelectCommand = cmd;
DataTable dt = new DataTable();
adapter.Fill(dt);
BindingSource bind = new BindingSource();
bind.DataSource = dt;
dgvUsers.DataSource = bind;
adapter.Update(dt);
dgvUsers.AutoResizeColumn(0);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public void searchUser(string userToFind)
{
string searchQuery = "SELECT * FROM user WHERE username LIKE '%" + userToFind + "%'";
MySqlDataAdapter adapter = new MySqlDataAdapter(searchQuery, con);
DataTable table = new DataTable();
adapter.Fill(table);
dgvUsers.DataSource = table;
}
private void tbSearch_TextChanged(object sender, EventArgs e)
{
searchUser(tbSearch.Text);
}
private void dgvUsers_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
{
}
}
}