-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
250 lines (240 loc) · 8.65 KB
/
Form1.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
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;
using MySql.Data.MySqlClient;
namespace Hotel_Management_System
{
public partial class FormPrincipal : Form
{
Connection connector = new Connection();
MySqlDataAdapter adapter;
MySqlCommand command;
string sql;
string id;
public FormPrincipal()
{
InitializeComponent();
clearTxt();
disableTxt();
disableButtons();
btnNew.Enabled = true;
btnSearch.Enabled = true;
}
private void formatGrid()
{
grid.Columns[0].HeaderText = "ID";
grid.Columns[1].HeaderText = "Name";
grid.Columns[2].HeaderText = "Adress";
grid.Columns[3].HeaderText = "CPF";
grid.Columns[4].HeaderText = "Telephone";
grid.Columns[0].Visible = false;
}
private void listGrid()
{
connector.openConnection();
sql = "SELECT * from client ORDER BY NAME ASC";
command = new MySqlCommand(sql, connector.sqlConnector);
adapter = new MySqlDataAdapter();
adapter.SelectCommand = command;
DataTable dt = new DataTable();
adapter.Fill(dt);
grid.DataSource = dt;
connector.closedConnection();
formatGrid();
}
private void btnNew_Click(object sender, EventArgs e)
{
enableTxt();
disableButtons();
btnSave.Enabled = true;
btnCancel.Enabled = true;
txtName.Focus();
}
private void btnSave_Click(object sender, EventArgs e)
{
if (txtName.ToString().Trim() == "")
{
MessageBox.Show("Insira o nome!");
txtName.Text = "";
txtName.Focus();
return;
}
if (txtCPF.Text == " . . -" || txtCPF.Text.Length < 14)
{
MessageBox.Show("Insira um CPF valido!");
txtCPF.Focus();
return;
}
if (txtTel.Text == "( ) -" || txtTel.Text.Length < 11)
{
MessageBox.Show("Insira um numero valido!");
txtTel.Focus();
return;
}
connector.openConnection();
sql = "INSERT INTO client (name, adress, cpf, tel) VALUES (@name, @adress, @cpf, @tel)";
command = new MySqlCommand(sql, connector.sqlConnector);
command.Parameters.AddWithValue("@name", txtName.Text);
command.Parameters.AddWithValue("@adress", txtAdress.Text);
command.Parameters.AddWithValue("@cpf", txtCPF.Text);
command.Parameters.AddWithValue("@tel", txtTel.Text);
command.ExecuteNonQuery();
connector.closedConnection();
disableButtons();
btnNew.Enabled = true;
btnDelete.Enabled = true;
btnEdit.Enabled = true;
clearTxt();
disableTxt();
listGrid();
MessageBox.Show("Saved successfully!", "Save", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void btnDelete_Click(object sender, EventArgs e)
{
disableButtons();
btnNew.Enabled = true;
connector.openConnection();
sql = "DELETE from client WHERE id = @id";
command = new MySqlCommand(sql, connector.sqlConnector);
command.Parameters.AddWithValue("@id", id);
command.ExecuteNonQuery();
connector.closedConnection();
MessageBox.Show("Deleted successfully!", "Delete", MessageBoxButtons.OK, MessageBoxIcon.Information);
disableButtons();
clearTxt();
disableTxt();
btnNew.Enabled = true;
listGrid();
}
private void btnCancel_Click(object sender, EventArgs e)
{
disableButtons();
disableTxt();
clearTxt();
btnNew.Enabled = true;
}
private void btnEdit_Click(object sender, EventArgs e)
{
if (txtName.ToString().Trim() == "")
{
MessageBox.Show("Enter a valid name!");
txtName.Text = "";
txtName.Focus();
return;
}
if (txtCPF.Text == " . . -" || txtCPF.Text.Length < 14)
{
MessageBox.Show("Enter a valid CPF!");
txtCPF.Focus();
return;
}
if (txtTel.Text == "( ) -" || txtTel.Text.Length < 11)
{
MessageBox.Show("Enter a valid number!");
txtTel.Focus();
return;
}
connector.openConnection();
sql = "UPDATE client SET name = @name, adress = @adress, cpf = @cpf, tel = @tel WHERE id = @id";
command = new MySqlCommand(sql, connector.sqlConnector);
command.Parameters.AddWithValue("@id", id);
command.Parameters.AddWithValue("@name", txtName.Text);
command.Parameters.AddWithValue("@adress", txtAdress.Text);
command.Parameters.AddWithValue("@cpf", txtCPF.Text);
command.Parameters.AddWithValue("@tel", txtTel.Text);
formatGrid();
command.ExecuteNonQuery();
connector.closedConnection();
MessageBox.Show("Edited successfully!", "Edit", MessageBoxButtons.OK, MessageBoxIcon.Information);
disableButtons();
clearTxt();
disableTxt();
btnNew.Enabled = true;
listGrid();
}
private void clearTxt()
{
txtName.Text = "";
txtAdress.Text = "";
txtCPF.Text = "";
txtTel.Text = "";
txtSearch.Text = "";
}
private void enableTxt()
{
txtName.Enabled = true;
txtAdress.Enabled = true;
txtCPF.Enabled = true;
txtTel.Enabled = true;
}
private void disableTxt()
{
txtName.Enabled = false;
txtAdress.Enabled = false;
txtCPF.Enabled = false;
txtTel.Enabled = false;
}
private void enableButtons()
{
btnNew.Enabled = true;
btnSave.Enabled = true;
btnDelete.Enabled = true;
btnCancel.Enabled = true;
btnEdit.Enabled = true;
}
private void disableButtons()
{
btnNew.Enabled = false;
btnSave.Enabled = false;
btnDelete.Enabled = false;
btnCancel.Enabled = false;
btnEdit.Enabled = false;
}
private void deleteOrEdit()
{
btnDelete.Enabled = true;
btnEdit.Enabled = true;
}
private void FormPrincipal_Load(object sender, EventArgs e)
{
listGrid();
}
private void grid_CellClick(object sender, DataGridViewCellEventArgs e)
{
enableButtons();
btnNew.Enabled = false;
btnSave.Enabled = false;
enableTxt();
id = grid.CurrentRow.Cells[0].Value.ToString();
txtName.Text = grid.CurrentRow.Cells[1].Value.ToString();
txtAdress.Text = grid.CurrentRow.Cells[2].Value.ToString();
txtCPF.Text = grid.CurrentRow.Cells[3].Value.ToString();
txtTel.Text = grid.CurrentRow.Cells[4].Value.ToString();
}
private void btnSearch_Click(object sender, EventArgs e)
{
if (txtSearch.ToString().Trim() == "")
{
MessageBox.Show("Search unsuccessfully!");
txtSearch.Text = "";
txtSearch.Focus();
return;
}
connector.openConnection();
sql = "SELECT name = @name from client WHERE name = @name";
command = new MySqlCommand(sql, connector.sqlConnector);
command.Parameters.AddWithValue("@name", Name);
formatGrid();
command.ExecuteNonQuery();
connector.closedConnection();
listGrid();
}
}
}