-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataUser.cs
234 lines (216 loc) · 8.15 KB
/
DataUser.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
224
225
226
227
228
229
230
231
232
233
234
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;
using System.Data.SqlClient;
namespace SIPMK
{
public partial class DataUser : Form
{
private SqlCommand cmd;
public DataUser()
{
InitializeComponent();
}
void Display()
{
try
{
using (SqlConnection SqlConnect = new SqlConnection(Koneksi.Connect))
{
SqlConnect.Open();
SqlDataAdapter sqlDisplay = new SqlDataAdapter("EXEC spDataUser", SqlConnect);
sqlDisplay.SelectCommand.ExecuteNonQuery();
DataTable data = new DataTable();
sqlDisplay.Fill(data);
dgvUser.DataSource = data;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
void Search()
{
try
{
using (SqlConnection SqlConnect = new SqlConnection(Koneksi.Connect))
{
SqlConnect.Open();
SqlDataAdapter GetUser = new SqlDataAdapter("EXEC spCariDataUser @CARI", SqlConnect);
GetUser.SelectCommand.Parameters.AddWithValue("@CARI", txtCari.Text.Trim());
GetUser.SelectCommand.ExecuteNonQuery();
DataTable data = new DataTable();
GetUser.Fill(data);
dgvUser.DataSource = data;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
void IdOtomatis()
{
long itung;
string urut;
SqlDataReader dr;
using (SqlConnection IdSqlConnect = new SqlConnection(Koneksi.Connect))
{
IdSqlConnect.Open();
cmd = new SqlCommand("EXECUTE spIdUser", IdSqlConnect);
dr = cmd.ExecuteReader();
dr.Read();
if (dr.HasRows)
{
itung = Convert.ToInt64(dr[0].ToString().Substring(dr["kd_user"].ToString().Length - 3, 3)) + 1;
string idurut = "000" + itung;
urut = "USR" + idurut.Substring(idurut.Length - 3, 3);
}
else
{
urut = "USR001";
}
dr.Close();
txtID.Text = urut;
}
}
void ClearData()
{
txtID.Clear();
txtNama.Clear();
txtEmail.Clear();
txtPassword.Clear();
cbLevel.Text = "Pilih Level User";
}
private void DataUser_Load(object sender, EventArgs e)
{
Display();
ClearData();
IdOtomatis();
}
private void txtCari_TextChanged(object sender, EventArgs e)
{
Search();
}
private void btnAdd_Click(object sender, EventArgs e)
{
try
{
if (txtID.Text.Trim() == "" || txtNama.Text.Trim() == "" || cbLevel.Text.Trim() == "" ||
txtEmail.Text.Trim() == "" || txtPassword.Text.Trim() == "")
{
MessageBox.Show("Data tidak boleh dikosongkan");
}
else
{
using (SqlConnection SqlConnectSimpan = new SqlConnection(Koneksi.Connect))
{
SqlConnectSimpan.Open();
SqlDataAdapter Insert = new SqlDataAdapter("EXEC spInputUser @ID, @NAMA, @EMAIL, @PASSWD, @LEVEL", SqlConnectSimpan);
Insert.SelectCommand.Parameters.AddWithValue("@ID", txtID.Text.Trim());
Insert.SelectCommand.Parameters.AddWithValue("@NAMA", txtNama.Text.Trim());
Insert.SelectCommand.Parameters.AddWithValue("@EMAIL", txtEmail.Text.Trim());
Insert.SelectCommand.Parameters.AddWithValue("@PASSWD", txtPassword.Text.Trim());
Insert.SelectCommand.Parameters.AddWithValue("@LEVEL", cbLevel.Text.Trim());
Insert.SelectCommand.ExecuteNonQuery();
MessageBox.Show("Data Tersimpan");
ClearData();
Display();
IdOtomatis();
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void dgvUser_CellClick(object sender, DataGridViewCellEventArgs e)
{
try
{
foreach (DataGridViewRow row in dgvUser.SelectedRows)
{
txtID.Text = row.Cells[0].Value.ToString();
txtNama.Text = row.Cells[1].Value.ToString();
txtEmail.Text = row.Cells[2].Value.ToString();
txtPassword.Text = row.Cells[3].Value.ToString();
cbLevel.Text = row.Cells[4].Value.ToString();
}
}
catch (Exception x)
{
MessageBox.Show(x.ToString());
}
}
private void btnEdit_Click(object sender, EventArgs e)
{
try
{
using (SqlConnection IdSqlConnectEdit = new SqlConnection(Koneksi.Connect))
{
IdSqlConnectEdit.Open();
DialogResult dr = MessageBox.Show("Anda yakin ingin mengubah data " + txtID.Text + " ?",
"Informasi", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dr == DialogResult.Yes)
{
SqlCommand update = new SqlCommand("EXEC spUpdateUser @ID, @NAMA, @EMAIL, @PASSWD, @LEVEL", IdSqlConnectEdit);
update.Parameters.AddWithValue("@ID", txtID.Text.Trim());
update.Parameters.AddWithValue("@NAMA", txtNama.Text.Trim());
update.Parameters.AddWithValue("@EMAIL", txtEmail.Text.Trim());
update.Parameters.AddWithValue("@PASSWD", txtPassword.Text.Trim());
update.Parameters.AddWithValue("@LEVEL", cbLevel.Text.Trim());
update.ExecuteNonQuery();
MessageBox.Show("Data " + txtNama.Text + " Terupdate");
ClearData();
Display();
IdOtomatis();
}
}
}
catch (Exception x)
{
MessageBox.Show(x.ToString());
}
}
private void btnDelete_Click(object sender, EventArgs e)
{
try
{
using (SqlConnection IdSqlConnectHps = new SqlConnection(Koneksi.Connect))
{
IdSqlConnectHps.Open();
DialogResult dr = MessageBox.Show("Anda yakin ingin menghapus data " + txtNama.Text + " ?",
"Informasi", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dr == DialogResult.Yes)
{
SqlCommand hapus = new SqlCommand("EXEC spHapusUser @ID ", IdSqlConnectHps);
hapus.Parameters.AddWithValue("@ID", txtID.Text);
hapus.ExecuteNonQuery();
MessageBox.Show("Data " + txtNama.Text + " Terhapus");
ClearData();
Display();
IdOtomatis();
}
}
}
catch (Exception x)
{
MessageBox.Show(x.ToString());
}
}
private void btnRefresh_Click(object sender, EventArgs e)
{
Display();
ClearData();
IdOtomatis();
}
}
}