-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainForm.cs
More file actions
416 lines (355 loc) · 16.1 KB
/
MainForm.cs
File metadata and controls
416 lines (355 loc) · 16.1 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
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 Inventory
{
public partial class MainForm : Form
{
private string connectionString = "Server=127.0.0.1;Database=Inventory;User Id=sa;Password=sa;";
//Create a DataTable to hold the data
DataTable _productDataTable;
DataTable _cboTable;
SqlDataAdapter _productDataAdapter;
SqlDataAdapter _cboAdapter;
public MainForm()
{
InitializeComponent();
fillGridView();
//initCRUD_gridView();
fillComboBox();
}
private void fillGridView()
{
string sql_query = "SELECT * FROM product;";
_productDataTable = new DataTable();
// Create a SqlDataAdapter to fetch the database data
_productDataAdapter = new SqlDataAdapter(sql_query, connectionString);
dataGridView1.Columns.Clear();
// Fill the DataTable with data from the database
_productDataAdapter.Fill(_productDataTable);
// Bind the DataTable to the DataGridView
dataGridView1.DataSource = _productDataTable;
// Set the DataGridView to be read-only
dataGridView1.ReadOnly = true;
}
// Description: Initializes the GridView by filling it
// and initializes SqlDataAdapter to maintain CRUD operations
//private void initCRUD_gridView()
//{
// // Create a new DataTable Object to hold the data
// _productDataTable = new DataTable();
// // Create a SqlDataAdapter Object to fetch the database data and fill is DataTable
// string sql_query = "SELECT * FROM product;";
// _productDataAdapter = new SqlDataAdapter(sql_query, connectionString);
// // Fill the DataTable with data from the database
// _productDataAdapter.Fill(_productDataTable);
// // Bind the DataTable to the DataGridView
// dataGridView1.DataSource = _productDataTable;
//}
private void insertIntoGridView()
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
try
{
connection.Open();
string insertQuery = "INSERT INTO product(Name, Brand, Type, Size, Category) VALUES(@Name, @Brand, @Type, @Size, @Category);";
SqlCommand insertCommand = new SqlCommand(insertQuery, connection);
if (string.IsNullOrWhiteSpace(txtNameInsert.Text))
{
MessageBox.Show("Name field cannot be empty.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtNameInsert.Focus();
return;
}
insertCommand.Parameters.AddWithValue("@Name", txtNameInsert.Text);
insertCommand.Parameters.AddWithValue("@Brand", txtBrandInsert.Text);
insertCommand.Parameters.AddWithValue("@Type", cmb_TypeInsert.Text?.ToString() ?? string.Empty);
insertCommand.Parameters.AddWithValue("@Size", cmb_SizeInsert.Text?.ToString() ?? string.Empty);
insertCommand.Parameters.AddWithValue("@Category", cmb_CategoryInsert.Text?.ToString() ?? string.Empty);
insertCommand.ExecuteNonQuery();
MessageBox.Show("Data Inserted Successfully", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
clearInsertFields();
refreshGridView();
}
catch (Exception ex)
{
MessageBox.Show("Error!\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void deleteFromGridView(string productID)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
try
{
connection.Open();
string deleteQuery = "DELETE FROM product WHERE ID = @ID;";
SqlCommand deleteCommand = new SqlCommand(deleteQuery, connection);
deleteCommand.Parameters.AddWithValue("@ID", productID);
deleteCommand.ExecuteNonQuery();
MessageBox.Show("Data Deleted Successfully", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtDel.Clear();
refreshGridView();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
// Description: Updates the selected row in the GridView based on the ID.
//private void updateGridView()
//{
// using (SqlConnection connection = new SqlConnection(connectionString))
// {
// try
// {
// connection.Open();
// SqlCommand updateCommand = new SqlCommand("UPDATE product SET Name = @Name, Brand = @Brand, Type = @Type, Size = @Size, Category = @Category WHERE ID = @ID;", connection);
// if (string.IsNullOrWhiteSpace(txtIDUpdate.Text))
// {
// MessageBox.Show("ID field cannot be empty.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
// txtIDUpdate.Focus();
// return;
// }
// updateCommand.Parameters.AddWithValue("@ID", txtIDUpdate.Text);
// updateCommand.Parameters.AddWithValue("@Name", txtNameUpdate.Text);
// updateCommand.Parameters.AddWithValue("@Brand", txtBrandUpdate.Text);
// updateCommand.Parameters.AddWithValue("@Type", cmb_TypeUpdate.Text?.ToString() ?? string.Empty);
// updateCommand.Parameters.AddWithValue("@Size", cmb_SizeUpdate.Text?.ToString() ?? string.Empty);
// updateCommand.Parameters.AddWithValue("@Category", cmb_CategoryUpdate.Text?.ToString() ?? string.Empty);
// updateCommand.ExecuteNonQuery();
// MessageBox.Show("Data Updated Successfully", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
// clearUpdateFields();
// refreshGridView();
// }
// catch (Exception ex)
// {
// MessageBox.Show("Error!\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
// }
// }
//}
private void updateGridView()
{
// updateStructure will hold the SQL placeholders for the fields to be updated
List<string> updateStructure= new List<string>();
// Create a SqlConnection to the database
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
SqlCommand command = new SqlCommand();
// Validate that the ID field is not empty
if (string.IsNullOrWhiteSpace(txtIDUpdate.Text))
{
MessageBox.Show("ID field cannot be empty.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtIDUpdate.Focus();
return;
}
else
{
// Replaces the placeholder with the actual value
command.Parameters.AddWithValue("@ID", txtIDUpdate.Text);
}
// Validate that the ID field is numeric.
if (!int.TryParse(txtIDUpdate.Text, out _))
{
MessageBox.Show("ID must be a numeric value.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtIDUpdate.Focus();
return;
}
if (!string.IsNullOrWhiteSpace(txtNameUpdate.Text))
{
updateStructure.Add("Name = @Name");
// Replaces the placeholder with the actual value
command.Parameters.AddWithValue("@Name", txtNameUpdate.Text);
}
if (!string.IsNullOrWhiteSpace(txtBrandUpdate.Text))
{
updateStructure.Add("Brand = @Brand");
command.Parameters.AddWithValue("@Brand", txtBrandUpdate.Text);
}
if (cmb_TypeUpdate.SelectedIndex > -1 || !string.IsNullOrWhiteSpace(cmb_TypeUpdate.Text))
{
updateStructure.Add("Type = @Type");
command.Parameters.AddWithValue("@Type", cmb_TypeUpdate.Text);
}
if (cmb_SizeUpdate.SelectedIndex > -1 || !string.IsNullOrWhiteSpace(cmb_SizeUpdate.Text))
{
updateStructure.Add("Size = @Size");
command.Parameters.AddWithValue("@Size", cmb_SizeUpdate.Text);
}
if (cmb_CategoryUpdate.SelectedIndex > -1 || !string.IsNullOrWhiteSpace(cmb_CategoryUpdate.Text))
{
updateStructure.Add("Category = @Category");
command.Parameters.AddWithValue("@Category", cmb_CategoryUpdate.Text);
}
string updateQuery = string.Join(", ", updateStructure);
if (string.IsNullOrWhiteSpace(updateQuery))
{
MessageBox.Show("No fields to update.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
command.CommandText = $"UPDATE product SET {updateQuery} WHERE ID = @ID;";
command.Connection = connection;
command.ExecuteNonQuery();
clearUpdateFields();
refreshGridView();
}
}
// Description: Binds a ComboBox to the database
// sql_query: SQL query to fetch distinct values
// displayMem: Column name to display in the ComboBox
// valueMem: Column name to use as the value of the ComboBox items for logical control
private void bindComboBox(ComboBox cboBox,string sql_query, string displayMem, string valueMem)
{
_cboAdapter = new SqlDataAdapter(sql_query, connectionString);
_cboTable = new DataTable();
_cboAdapter.Fill(_cboTable);
cboBox.DataSource = _cboTable;
cboBox.DisplayMember = displayMem;
cboBox.ValueMember = valueMem;
cboBox.SelectedIndex = -1;
}
//private void refreshComboBox()
//{
// _cboTable.Clear();
// _cboAdapter.Fill(_cboTable);
//}
// Description: Creates a list of filters based on the
// text in the TextBoxes and the selected items in the ComboBoxes
private void applyFilter()
{
List<string> filters = new List<string>();
if (!string.IsNullOrWhiteSpace(txtIDUpdate.Text))
{
filters.Add($"ID = '{txtIDUpdate.Text}'");
}
if (!string.IsNullOrWhiteSpace(txtName.Text))
{
filters.Add($"Name LIKE '%{txtName.Text}%'");
}
if (!string.IsNullOrWhiteSpace(txtBrand.Text))
{
filters.Add("Brand LIKE '%" + txtBrand.Text + "%'");
}
if (cmb_Type.SelectedIndex != -1)
{
filters.Add($"Type = '{cmb_Type.Text}'");
}
if (cmb_Size.SelectedIndex != -1)
{
filters.Add($"Size = '{cmb_Size.Text}'");
}
if (cmb_Category.SelectedIndex != -1)
{
filters.Add($"Category = '{cmb_Category.Text}'");
}
// If no filters are applied, show all products.
string finalFilter = string.Join(" AND ", filters);
_productDataTable.DefaultView.RowFilter = finalFilter;
}
// Fills the ComboBoxes with distinct values from the database
private void fillComboBox()
{
// Bind the ComboBoxes to distinct values from the database
// ISSUE: Empty values are not handled properly (e.g., if a product has no Type, Size, or Category)
bindComboBox(cmb_Type, "SELECT DISTINCT Type FROM product;", "Type", "Type");
bindComboBox(cmb_Size, "SELECT DISTINCT Size FROM product;", "Size", "Size");
bindComboBox(cmb_Category, "SELECT DISTINCT Category FROM product;", "Category", "Category");
// For the Insert section
bindComboBox(cmb_TypeInsert, "SELECT DISTINCT Type FROM product;", "Type", "Type");
bindComboBox(cmb_SizeInsert, "SELECT DISTINCT Size FROM product;", "Size", "Size");
bindComboBox(cmb_CategoryInsert, "SELECT DISTINCT Category FROM product;", "Category", "Category");
//For the Update section
bindComboBox(cmb_TypeUpdate, "SELECT DISTINCT Type FROM product;", "Type", "Type");
bindComboBox(cmb_SizeUpdate, "SELECT DISTINCT Size FROM product;", "Size", "Size");
bindComboBox(cmb_CategoryUpdate, "SELECT DISTINCT Category FROM product;", "Category", "Category");
}
// Event handler for the "Reset" button
private void btn_Reset_Click(object sender, EventArgs e)
{
clearSearchFields();
}
private void txtName_TextChanged(object sender, EventArgs e)
{
applyFilter();
}
private void txtBrand_TextChanged(object sender, EventArgs e)
{
applyFilter();
}
private void cmb_Type_SelectedIndexChanged(object sender, EventArgs e)
{
applyFilter();
}
private void cmb_Size_SelectedIndexChanged(object sender, EventArgs e)
{
applyFilter();
}
private void cmb_Category_SelectedIndexChanged(object sender, EventArgs e)
{
applyFilter();
}
private void refreshGridView()
{
_productDataTable.Clear();
_productDataAdapter.Fill(_productDataTable);
}
private void btn_Insert_Click(object sender, EventArgs e)
{
insertIntoGridView();
//refreshComboBox();
fillComboBox();
}
private void clearInsertFields()
{
txtNameInsert.Clear();
txtBrandInsert.Clear();
cmb_TypeInsert.SelectedIndex = -1;
cmb_SizeInsert.SelectedIndex = -1;
cmb_CategoryInsert.SelectedIndex = -1;
}
private void clearUpdateFields()
{
txtIDUpdate.Clear();
txtNameUpdate.Clear();
txtBrandUpdate.Clear();
cmb_TypeUpdate.SelectedIndex = -1;
cmb_SizeUpdate.SelectedIndex = -1;
cmb_CategoryUpdate.SelectedIndex = -1;
}
private void clearSearchFields()
{
txtName.Clear();
txtBrand.Clear();
// Reset the ComboBoxes to their default state (No selection)
cmb_Type.SelectedIndex = -1;
cmb_Size.SelectedIndex = -1;
cmb_Category.SelectedIndex = -1;
}
private void btn_Del_Click(object sender, EventArgs e)
{
deleteFromGridView(txtDel.Text);
//refreshComboBox();
fillComboBox();
}
private void btn_Update_Click(object sender, EventArgs e)
{
updateGridView();
refreshGridView();
}
private void txtIDUpdate_TextChanged(object sender, EventArgs e)
{
clearInsertFields();
applyFilter();
}
}
}