-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditCustomersData.cs
More file actions
434 lines (367 loc) · 16.2 KB
/
editCustomersData.cs
File metadata and controls
434 lines (367 loc) · 16.2 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
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 Project
{
public partial class editCustomersData : Form
{
private bool registrationInProgress = false;
private const string connectionString = "Data Source= SAAD-DESKTOP\\SQLEXPRESS;Integrated Security=True";
public editCustomersData()
{
InitializeComponent();
LoadCustomerData();
}
private void editCustomersData_Load(object sender, EventArgs e)
{
register.Visible = false;
inputcustomerID.Visible = false;
CustomerID.Visible = false;
deleteCustomer.Visible = false;
}
private void LoadCustomerData()
{
try
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
// Query to select all customers
string query = "SELECT * FROM Customers";
using (SqlCommand command = new SqlCommand(query, connection))
{
SqlDataAdapter adapter = new SqlDataAdapter(command);
DataTable dataTable = new DataTable();
adapter.Fill(dataTable);
// Bind the DataTable to the DataGridView
dataGridView1.DataSource = dataTable;
}
}
}
catch (Exception ex)
{
MessageBox.Show($"Error: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
{
DataGridViewRow selectedRow = dataGridView1.Rows[e.RowIndex];
// Assuming your input boxes are TextBox controls
textBox1.Text = selectedRow.Cells["CustomerID"].Value.ToString();
textBox2.Text = selectedRow.Cells["FirstName"].Value.ToString();
textBox5.Text = selectedRow.Cells["LastName"].Value.ToString();
textBox4.Text = selectedRow.Cells["Email"].Value.ToString();
textBox3.Text = selectedRow.Cells["Phone"].Value.ToString();
textBox6.Text = selectedRow.Cells["Passcode"].Value.ToString();
}
}
private void update_Click(object sender, EventArgs e)
{
try
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
// Update query with specified CustomerID
string updateQuery = "UPDATE Customers SET FirstName = @FirstName, LastName = @LastName, " +
"Email = @Email, Phone = @Phone, Passcode = @Passcode WHERE CustomerID = @CustomerID";
using (SqlCommand command = new SqlCommand(updateQuery, connection))
{
// Parameters to avoid SQL injection
command.Parameters.AddWithValue("@CustomerID", textBox1.Text);
command.Parameters.AddWithValue("@FirstName", textBox2.Text);
command.Parameters.AddWithValue("@LastName", textBox5.Text);
command.Parameters.AddWithValue("@Email", textBox4.Text);
command.Parameters.AddWithValue("@Phone", textBox3.Text);
// Check if Passcode is not empty before adding it to parameters
if (!string.IsNullOrWhiteSpace(textBox6.Text))
{
command.Parameters.AddWithValue("@Passcode", textBox6.Text);
}
else
{
// Pass NULL for Passcode if it's empty
command.Parameters.AddWithValue("@Passcode", DBNull.Value);
}
int rowsAffected = command.ExecuteNonQuery();
if (rowsAffected > 0)
{
MessageBox.Show("Customer details updated successfully!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
// Optionally, you can reload the customer data in the DataGridView
LoadCustomerData();
}
else
{
MessageBox.Show("No matching customer found for update.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
catch (SqlException ex)
{
HandleError("An unexpected error occurred", ex);
}
catch (Exception ex)
{
HandleError("An unexpected error occurred", ex);
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void textBox5_TextChanged(object sender, EventArgs e)
{
}
private void textBox4_TextChanged(object sender, EventArgs e)
{
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void textBox6_TextChanged(object sender, EventArgs e)
{
}
private void addCustomer_Click(object sender, EventArgs e)
{
register.Visible = true;
try
{
// Clear existing values
textBox1.Clear(); // CustomerID
textBox2.Clear(); // FirstName
textBox5.Clear(); // LastName
textBox4.Clear(); // Email
textBox3.Clear(); // Phone
textBox6.Clear(); // Passcode
registrationInProgress = true;
update.Enabled = false;
// Disable CustomerID textbox and clear its value
textBox1.Enabled = false;
// Enable and focus on other textboxes for input
textBox2.Enabled = true;
textBox5.Enabled = true;
textBox4.Enabled = true;
textBox3.Enabled = true;
textBox6.Enabled = true;
// Generate new CustomerID by finding the maximum existing CustomerID and incrementing it by 1
int newCustomerID = GetMaxCustomerID() + 1;
textBox1.Text = newCustomerID.ToString();
// Set focus on the FirstName textbox
textBox2.Focus();
register.Visible = true;
}
catch (Exception ex)
{
MessageBox.Show($"Error: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
// Helper method to get the maximum existing CustomerID
private int GetMaxCustomerID()
{
int maxCustomerID = 0;
try
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
// Query to get the maximum existing CustomerID
string query = "SELECT MAX(CustomerID) FROM Customers";
using (SqlCommand command = new SqlCommand(query, connection))
{
object result = command.ExecuteScalar();
if (result != null && result != DBNull.Value)
{
maxCustomerID = Convert.ToInt32(result);
}
}
}
}
catch (SqlException ex)
{
HandleError("Error executing SQL command", ex);
}
catch (Exception ex)
{
HandleError("An unexpected error occurred", ex);
}
return maxCustomerID;
}
private void ChangeFontColor(Color color)
{
// Remove font color constraints
textBox1.ForeColor = color;
textBox2.ForeColor = color;
textBox5.ForeColor = color;
textBox4.ForeColor = color;
textBox3.ForeColor = color;
textBox6.ForeColor = color;
}
private void register_Click(object sender, EventArgs e)
{
register.Visible = false;
update.Enabled = true;
try
{
// Validate that all required fields are filled
if (string.IsNullOrWhiteSpace(textBox2.Text) || string.IsNullOrWhiteSpace(textBox5.Text) ||
string.IsNullOrWhiteSpace(textBox4.Text) || string.IsNullOrWhiteSpace(textBox3.Text))
{
MessageBox.Show("Please fill in all required fields.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return; // Exit the method if validation fails
}
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
// Insert query with specified CustomerID and allowing NULL for Passcode
string insertQuery = "INSERT INTO Customers (CustomerID, FirstName, LastName, Email, Phone, Passcode) " +
"VALUES (@CustomerID, @FirstName, @LastName, @Email, @Phone, @Passcode)";
using (SqlCommand command = new SqlCommand(insertQuery, connection))
{
// Parameters to avoid SQL injection
command.Parameters.AddWithValue("@CustomerID", textBox1.Text);
command.Parameters.AddWithValue("@FirstName", textBox2.Text);
command.Parameters.AddWithValue("@LastName", textBox5.Text);
command.Parameters.AddWithValue("@Email", textBox4.Text);
command.Parameters.AddWithValue("@Phone", textBox3.Text);
// Check if Passcode is not empty before adding it to parameters
if (!string.IsNullOrWhiteSpace(textBox6.Text))
{
command.Parameters.AddWithValue("@Passcode", textBox6.Text);
}
else
{
// Pass NULL for Passcode if it's empty
command.Parameters.AddWithValue("@Passcode", DBNull.Value);
}
int rowsAffected = command.ExecuteNonQuery();
if (rowsAffected > 0)
{
MessageBox.Show("Customer registered successfully!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
// After registration, hide the "register" button
register.Visible = false;
// Optionally, you can reload the customer data in the DataGridView
LoadCustomerData();
}
else
{
MessageBox.Show("Failed to register customer.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
catch (SqlException ex)
{
// Handle SQL constraint violations
if (ex.Number == 547) // Foreign key violation
{
MessageBox.Show("A foreign key constraint violation occurred. Make sure the referenced data exists.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else if (ex.Number == 2627 || ex.Number == 2601) // Unique constraint violation
{
MessageBox.Show("A unique constraint violation occurred. Ensure that the data being inserted is unique.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else if (ex.Number == 547) // Check constraint violation
{
MessageBox.Show("A check constraint violation occurred. Make sure the data meets the specified constraints.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
HandleError("Error executing SQL command", ex);
}
}
catch (Exception ex)
{
HandleError("An unexpected error occurred", ex);
}
}
private void delete_Click(object sender, EventArgs e)
{
try
{
inputcustomerID.Visible = true;
CustomerID.Visible = true;
deleteCustomer.Visible = true;
// Disable update button during delete operation
update.Enabled = false;
// Hide the "Register" button during delete operation
register.Visible = false;
// Set focus on the "Customer ID" text box
CustomerID.Focus();
}
catch (Exception ex)
{
HandleError("An unexpected error occurred", ex);
}
}
private void CustomerID_TextChanged(object sender, EventArgs e)
{
}
private void inputcustomerID_Click(object sender, EventArgs e)
{
}
private void deleteCustomer_Click(object sender, EventArgs e)
{
try
{
deleteCustomer.Visible = false;
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
// Delete query based on specified CustomerID
string deleteQuery = "DELETE FROM Customers WHERE CustomerID = @CustomerID";
using (SqlCommand command = new SqlCommand(deleteQuery, connection))
{
// Parameters to avoid SQL injection
command.Parameters.AddWithValue("@CustomerID", CustomerID.Text);
int rowsAffected = command.ExecuteNonQuery();
if (rowsAffected > 0)
{
MessageBox.Show("Customer deleted successfully!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
// Optionally, you can reload the customer data in the DataGridView
LoadCustomerData();
}
else
{
MessageBox.Show("No matching customer found.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}
}
catch (SqlException ex)
{
HandleError("Error executing SQL command", ex);
}
catch (Exception ex)
{
HandleError("An unexpected error occurred", ex);
}
finally
{
// Reset the UI state after delete operation
inputcustomerID.Visible = false;
CustomerID.Visible = false;
update.Enabled = true;
register.Visible = true;
}
}
private void HandleError(string message, Exception ex)
{
// Log the error
Console.WriteLine($"{message}: {ex.Message}");
// Display a user-friendly error message
MessageBox.Show($"{message}. Please contact support for assistance.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}