-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInventoryManagerOperations.cs
More file actions
243 lines (203 loc) · 9 KB
/
InventoryManagerOperations.cs
File metadata and controls
243 lines (203 loc) · 9 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
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 static Project.allCategories;
using static Project.InventoryManagerOperations;
namespace Project
{
public partial class InventoryManagerOperations : Form
{
string connectionString = "Data Source= SAAD-DESKTOP\\SQLEXPRESS;Integrated Security=True";
public InventoryManagerOperations()
{
InitializeComponent();
LoadCategories();
}
private void button1_Click(object sender, EventArgs e)
{
int selectedIndex = Categories.SelectedIndex;
}
private void InventoryManagerOperations_Load(object sender, EventArgs e)
{
Products.DisplayMember = "ToString";
}
private void LoadCategories()
{
// Clear existing items in the listBox1
Categories.Items.Clear();
// SQL query to select all categories
string query = "SELECT * FROM Categories";
// Create a SqlConnection to the database
using (SqlConnection connection = new SqlConnection(connectionString))
{
// Open the database connection
connection.Open();
// Create a SqlCommand to execute the query
using (SqlCommand command = new SqlCommand(query, connection))
{
// Execute the query and get a data reader
using (SqlDataReader reader = command.ExecuteReader())
{
// Check if the reader has rows
while (reader.Read())
{
// Assuming the second column is CategoryName
string categoryName = reader.GetString(1);
// Add the category name to the listBox1
Categories.Items.Add(categoryName);
}
}
}
}
}
private void buttonUpdateStock_Click(object sender, EventArgs e)
{
if (Products.SelectedIndex != -1)
{
// Get the selected product from the listBox2
Product selectedProduct = (Product)Products.SelectedItem;
// Retrieve the new stock count from the TextBox
if (int.TryParse(textBoxStockCount.Text, out int newStockCount))
{
// Update the stock count in the ListBox
selectedProduct.StockCount = newStockCount;
// Update the display in the ListBox
Products.Items[Products.SelectedIndex] = selectedProduct;
// Update the database with the new stock count (you need to implement this)
UpdateStockCountInDatabase(selectedProduct.ProductID, newStockCount);
}
else
{
// Display an error message if the entered stock count is not a valid integer
MessageBox.Show("Please enter a valid stock count (integer).");
}
}
}
private void listBox1_SelectedIndexChanged_1(object sender, EventArgs e)
{
if (Categories.SelectedIndex != -1)
{
string selectedCategory = Categories.SelectedItem.ToString();
// Clear existing items in the listBox2
Products.Items.Clear();
// SQL query to select products for the selected category with their prices
string query = "SELECT P.ProductID, P.Name, P.Price, P.StockCount " +
"FROM Products P " +
"JOIN Categories C ON P.CategoryID = C.CategoryID " +
"WHERE C.CategoryName = @CategoryName";
// Create a SqlConnection to the database
using (SqlConnection connection = new SqlConnection(connectionString))
{
// Open the database connection
connection.Open();
// Create a SqlCommand to execute the query
using (SqlCommand command = new SqlCommand(query, connection))
{
// Add a parameter to the query to filter by category name
command.Parameters.AddWithValue("@CategoryName", selectedCategory);
// Execute the query and get a data reader
using (SqlDataReader reader = command.ExecuteReader())
{
// Check if the reader has rows
while (reader.Read())
{
int productID = reader.GetInt32(0);
string productName = reader.GetString(1);
decimal productPrice = reader.GetDecimal(2);
int stockCount = reader.GetInt32(3); // Added to retrieve StockCount
Products.Items.Add(new Product { ProductID = productID, Name = productName, Price = productPrice, StockCount = stockCount });
}
}
}
}
}
}
private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{
if (Products.SelectedIndex != -1)
{
// Get the selected product from the listBox2
Product selectedProduct = (Product)Products.SelectedItem;
// Display the product details in the TextBox
textBoxProductName.Text = selectedProduct.Name;
textBoxStockCount.Text = selectedProduct.StockCount.ToString();
}
}
private void button1_Click_1(object sender, EventArgs e)
{
// Display a success message
MessageBox.Show("Account Logged out!");
this.Hide();
loginstaff loginstaff = new loginstaff();
loginstaff.Show();
}
private void stockCount_TextChanged(object sender, EventArgs e)
{
}
private void UpdateStockCountInDatabase(int productID, int newStockCount)
{
string query = "UPDATE Products SET StockCount = @NewStockCount WHERE ProductID = @ProductID";
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
using (SqlCommand command = new SqlCommand(query, connection))
{
command.Parameters.AddWithValue("@NewStockCount", newStockCount);
command.Parameters.AddWithValue("@ProductID", productID);
int rowsAffected = command.ExecuteNonQuery();
if (rowsAffected > 0)
{
MessageBox.Show("Stock count updated successfully.");
}
else
{
MessageBox.Show("Failed to update stock count.");
}
}
}
}
// Add StockCount property to your Product class
public class Product
{
public int ProductID { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public int StockCount { get; set; }
// Override ToString to display the product information
public override string ToString()
{
return $"{Name} - ${Price} - Stock: {StockCount}";
}
}
private void buttonUpdateStock_Click_1(object sender, EventArgs e)
{
if (Products.SelectedIndex != -1)
{
// Get the selected product from the listBox2
Product selectedProduct = (Product)Products.SelectedItem;
// Retrieve the new stock count from the TextBox
if (int.TryParse(textBoxStockCount.Text, out int newStockCount))
{
// Update the stock count in the ListBox
selectedProduct.StockCount = newStockCount;
// Update the display in the ListBox
Products.Items[Products.SelectedIndex] = selectedProduct;
// Update the database with the new stock count (you need to implement this)
UpdateStockCountInDatabase(selectedProduct.ProductID, newStockCount);
}
else
{
// Display an error message if the entered stock count is not a valid integer
MessageBox.Show("Please enter a valid stock count (integer).");
}
}
}
}
}