-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomerMenu.cs
More file actions
221 lines (177 loc) · 6.6 KB
/
CustomerMenu.cs
File metadata and controls
221 lines (177 loc) · 6.6 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
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 Project;
namespace Project
{
public partial class CustomerMenu : Form
{
private int customerId;
private string customerName;
// Constructor to accept customer ID
public CustomerMenu(int customerId)
{
InitializeComponent();
this.customerId = customerId;
LoadCustomerInfo(customerId);
DisplayWelcomeMessage();
}
private void LoadCustomerInfo(int customerId)
{
string connectionString = "Data Source=SAAD-DESKTOP\\SQLEXPRESS;Integrated Security=True";
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
string query = "SELECT FirstName FROM Customers WHERE CustomerID = @CustomerId";
using (SqlCommand command = new SqlCommand(query, connection))
{
command.Parameters.AddWithValue("@CustomerId", customerId);
using (SqlDataReader reader = command.ExecuteReader())
{
if (reader.Read())
{
customerName = reader["FirstName"].ToString();
}
}
}
}
}
private void DisplayWelcomeMessage()
{
if (!string.IsNullOrEmpty(customerName))
{
welcome.Text = $"Welcome, {customerName}!";
}
}
private void CustomerMenu_Load(object sender, EventArgs e)
{
}
private bool TableExists(DataTable schema, string tableName)
{
foreach (DataRow row in schema.Rows)
{
string table = (string)row["TABLE_NAME"];
Console.WriteLine($"Found table: {table}");
if (string.Equals(table, tableName, StringComparison.OrdinalIgnoreCase))
{
Console.WriteLine($"Table {tableName} exists!");
return true;
}
}
Console.WriteLine($"Table {tableName} does not exist!");
return false;
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
allCategories allcategories = new allCategories(customerId);
allcategories.Show();
}
private void button1_Click(object sender, EventArgs e)
{
string connectionString = "Data Source=SAAD-DESKTOP\\SQLEXPRESS;Integrated Security=True";
SqlConnection connection = new SqlConnection(connectionString);
SqlCommand command = new SqlCommand("SELECT * FROM Products", connection);
try
{
connection.Open();
SqlDataReader reader = command.ExecuteReader();
List<string> products = new List<string>();
while (reader.Read())
{
int productID = reader.GetInt32(0);
string productName = reader.GetString(1);
int categoryID = reader.GetInt32(2);
decimal price = reader.GetDecimal(3);
int stockCount = reader.GetInt32(4);
ice
products.Add($"ID: {productID}, Name: {productName}, Category ID: {categoryID}, Price: {price}");
}
MessageBox.Show(string.Join("\n", products), "Products");
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message, "Error");
}
finally
{
if (connection.State == ConnectionState.Open)
{
connection.Close();
}
}
}
private void customerInfo_Click(object sender, EventArgs e)
{
customerInfo.Text = $"1 - John Doe";
}
private void label2_Click(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
this.Hide();
customerRegister CustomerRegister = new customerRegister(FormMode.EditProfile, customerId);
CustomerRegister.Show();
}
private void button6_Click(object sender, EventArgs e)
{
// Display a success message
MessageBox.Show("Account Logged out!");
// Close the current form (editCustomerInfo)
this.Close();
// Optionally, open the menu form
CutomerLogin CutomerLogin = new CutomerLogin();
CutomerLogin.Show();
}
private void button4_Click(object sender, EventArgs e)
{
DisplayPreviousOrders(customerId);
}
private void DisplayPreviousOrders(int customerId)
{
string connectionString = "Data Source=SAAD-DESKTOP\\SQLEXPRESS;Integrated Security=True";
SqlConnection connection = new SqlConnection(connectionString);
SqlCommand command = new SqlCommand("SELECT * FROM Orders WHERE CustomerID = @CustomerId", connection);
command.Parameters.AddWithValue("@CustomerId", customerId);
try
{
connection.Open();
SqlDataReader reader = command.ExecuteReader();
List<string> orders = new List<string>();
while (reader.Read())
{
int orderId = reader.GetInt32(0);
DateTime orderDate = reader.GetDateTime(2);
decimal totalAmount = reader.GetDecimal(3);
orders.Add($"Order ID: {orderId}, Date: {orderDate}, Total Amount: {totalAmount:C}");
}
MessageBox.Show(string.Join("\n", orders), "Previous Orders");
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message, "Error");
}
finally
{
if (connection.State == ConnectionState.Open)
{
connection.Close();
}
}
}
private void label1_Click(object sender, EventArgs e)
{
}
private void welcome_Click(object sender, EventArgs e)
{
}
}
}