-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm14.cs
More file actions
344 lines (305 loc) · 14.9 KB
/
Copy pathForm14.cs
File metadata and controls
344 lines (305 loc) · 14.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
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
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Linq;
using System.Windows.Forms;
namespace H1
{
public partial class Form14 : Form
{
// The member variables dataGridView1, txtSearch, etc. are declared in Form14.Designer.cs
// and accessible from this file through the partial class mechanism
private DataTable bookingsTable;
private string connectionString = @"Data Source=ABDULSABOOR190\SQLEXPRESS;Initial Catalog=sabbbb;Integrated Security=True;Encrypt=False";
// You'll need to update this with your actual connection string
public Form14()
{
InitializeComponent();
InitializeDataTable();
}
private void InitializeDataTable()
{
bookingsTable = new DataTable();
bookingsTable.Columns.Add("Booking ID", typeof(int));
bookingsTable.Columns.Add("Trip Title", typeof(string));
bookingsTable.Columns.Add("User", typeof(string));
bookingsTable.Columns.Add("Participants", typeof(int));
bookingsTable.Columns.Add("Booking Date", typeof(DateTime));
bookingsTable.Columns.Add("Status", typeof(string));
bookingsTable.Columns.Add("Amount", typeof(decimal));
}
private void Form14_Load(object sender, EventArgs e)
{
LoadBookingsFromDatabase();
ConfigureDataGridView();
}
private void LoadBookingsFromDatabase()
{
try
{
bookingsTable.Clear();
using (SqlConnection connection = new SqlConnection(connectionString))
{
string query = @"
SELECT b.booking_id, t.title,
tp.first_name + ' ' + tp.last_name AS full_name,
b.participants, b.booking_date, b.status, b.total_amount
FROM Booking b
INNER JOIN Trip t ON b.trip_id = t.trip_id
INNER JOIN TravelerProfile tp ON b.traveler_id = tp.user_id
ORDER BY b.booking_date DESC";
SqlCommand command = new SqlCommand(query, connection);
connection.Open();
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
bookingsTable.Rows.Add(
reader.GetInt32(0), // Booking ID
reader.GetString(1), // Trip Title
reader.GetString(2), // User Full Name
reader.GetInt32(3), // Participants
reader.GetDateTime(4), // Booking Date
reader.GetString(5), // Status
reader.GetDecimal(6) // Amount
);
}
}
}
dataGridView1.DataSource = bookingsTable;
}
catch (Exception ex)
{
MessageBox.Show($"Error loading bookings: {ex.Message}", "Database Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ConfigureDataGridView()
{
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
dataGridView1.ReadOnly = true;
}
private void btnSearch_Click(object sender, EventArgs e)
{
string searchTerm = txtSearch.Text.Trim().ToLower();
if (string.IsNullOrWhiteSpace(searchTerm))
{
dataGridView1.DataSource = bookingsTable;
return;
}
try
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
string query = @"
SELECT b.booking_id, t.title,
tp.first_name + ' ' + tp.last_name AS full_name,
b.participants, b.booking_date, b.status, b.total_amount
FROM Booking b
INNER JOIN Trip t ON b.trip_id = t.trip_id
INNER JOIN TravelerProfile tp ON b.traveler_id = tp.user_id
WHERE t.title LIKE @SearchTerm
OR (tp.first_name + ' ' + tp.last_name) LIKE @SearchTerm
OR CAST(b.booking_id AS VARCHAR) LIKE @SearchTerm
ORDER BY b.booking_date DESC";
SqlCommand command = new SqlCommand(query, connection);
command.Parameters.AddWithValue("@SearchTerm", "%" + searchTerm + "%");
connection.Open();
DataTable searchResults = new DataTable();
searchResults.Columns.Add("Booking ID", typeof(int));
searchResults.Columns.Add("Trip Title", typeof(string));
searchResults.Columns.Add("User", typeof(string));
searchResults.Columns.Add("Participants", typeof(int));
searchResults.Columns.Add("Booking Date", typeof(DateTime));
searchResults.Columns.Add("Status", typeof(string));
searchResults.Columns.Add("Amount", typeof(decimal));
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
searchResults.Rows.Add(
reader.GetInt32(0), // Booking ID
reader.GetString(1), // Trip Title
reader.GetString(20), // User Full Name
reader.GetInt32(3), // Participants
reader.GetDateTime(4), // Booking Date
reader.GetString(5), // Status
reader.GetDecimal(6) // Amount
);
}
}
if (searchResults.Rows.Count > 0)
{
dataGridView1.DataSource = searchResults;
}
else
{
MessageBox.Show("No matching bookings found.", "Search",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
catch (Exception ex)
{
MessageBox.Show($"Error searching bookings: {ex.Message}", "Database Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void btnUpdateStatus_Click(object sender, EventArgs e)
{
if (dataGridView1.SelectedRows.Count == 0)
{
MessageBox.Show("Please select a booking to update.", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
var selectedRow = dataGridView1.SelectedRows[0];
int bookingId = (int)selectedRow.Cells["Booking ID"].Value;
string currentStatus = selectedRow.Cells["Status"].Value.ToString();
using (var statusForm = new StatusUpdateForm(currentStatus))
{
if (statusForm.ShowDialog() == DialogResult.OK)
{
try
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
string query = "UPDATE Booking SET status = @Status WHERE booking_id = @BookingId";
SqlCommand command = new SqlCommand(query, connection);
command.Parameters.AddWithValue("@Status", statusForm.NewStatus);
command.Parameters.AddWithValue("@BookingId", bookingId);
connection.Open();
int rowsAffected = command.ExecuteNonQuery();
if (rowsAffected > 0)
{
selectedRow.Cells["Status"].Value = statusForm.NewStatus;
MessageBox.Show($"Booking #{bookingId} status updated to {statusForm.NewStatus}",
"Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("Failed to update booking status.", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
catch (Exception ex)
{
MessageBox.Show($"Error updating booking status: {ex.Message}",
"Database Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
private void btnViewDetails_Click(object sender, EventArgs e)
{
if (dataGridView1.SelectedRows.Count == 0)
{
MessageBox.Show("Please select a booking to view details.", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
var selectedRow = dataGridView1.SelectedRows[0];
int bookingId = (int)selectedRow.Cells["Booking ID"].Value;
try
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
string query = @"
SELECT b.booking_id, t.title, t.destination,
tp.first_name, tp.last_name, tp.phone,
b.participants, b.booking_date, b.status, b.total_amount,
b.special_requests, t.start_date, t.end_date
FROM Booking b
INNER JOIN Trip t ON b.trip_id = t.trip_id
INNER JOIN TravelerProfile tp ON b.traveler_id = tp.user_id
WHERE b.booking_id = @BookingId";
SqlCommand command = new SqlCommand(query, connection);
command.Parameters.AddWithValue("@BookingId", bookingId);
connection.Open();
using (SqlDataReader reader = command.ExecuteReader())
{
if (reader.Read())
{
string details = $"Booking ID: {reader["booking_id"]}\n" +
$"Trip: {reader["title"]}\n" +
$"Destination: {reader["destination"]}\n" +
$"User: {reader["first_name"]} {reader["last_name"]}\n" +
$"Contact: {reader["phone"]}\n" +
$"Participants: {reader["participants"]}\n" +
$"Booking Date: {((DateTime)reader["booking_date"]).ToString("yyyy-MM-dd")}\n" +
$"Trip Dates: {((DateTime)reader["start_date"]).ToString("yyyy-MM-dd")} to " +
$"{((DateTime)reader["end_date"]).ToString("yyyy-MM-dd")}\n" +
$"Status: {reader["status"]}\n" +
$"Amount: {string.Format("{0:C}", reader["total_amount"])}\n";
if (!reader.IsDBNull(reader.GetOrdinal("special_requests")))
{
details += $"\nSpecial Requests:\n{reader["special_requests"]}";
}
MessageBox.Show(details, "Booking Details",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("Booking details not found.", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
catch (Exception ex)
{
MessageBox.Show($"Error retrieving booking details: {ex.Message}",
"Database Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void btnRefresh_Click(object sender, EventArgs e)
{
txtSearch.Clear();
LoadBookingsFromDatabase();
}
}
// Status update dialog class
public class StatusUpdateForm : Form
{
public string NewStatus { get; private set; }
private ComboBox cmbStatus;
private Button btnOK;
private Button btnCancel;
public StatusUpdateForm(string currentStatus)
{
InitializeComponents();
cmbStatus.SelectedItem = currentStatus;
}
private void InitializeComponents()
{
this.Text = "Update Booking Status";
this.Size = new System.Drawing.Size(300, 150);
this.FormBorderStyle = FormBorderStyle.FixedDialog;
this.StartPosition = FormStartPosition.CenterParent;
this.MaximizeBox = false;
this.MinimizeBox = false;
cmbStatus = new ComboBox();
cmbStatus.Items.AddRange(new object[] { "pending", "confirmed", "canceled" });
cmbStatus.DropDownStyle = ComboBoxStyle.DropDownList;
cmbStatus.Location = new System.Drawing.Point(20, 20);
cmbStatus.Size = new System.Drawing.Size(250, 25);
this.Controls.Add(cmbStatus);
btnOK = new Button();
btnOK.Text = "OK";
btnOK.DialogResult = DialogResult.OK;
btnOK.Location = new System.Drawing.Point(100, 60);
btnOK.Click += (s, e) => { NewStatus = cmbStatus.SelectedItem.ToString(); this.Close(); };
this.Controls.Add(btnOK);
btnCancel = new Button();
btnCancel.Text = "Cancel";
btnCancel.DialogResult = DialogResult.Cancel;
btnCancel.Location = new System.Drawing.Point(180, 60);
this.Controls.Add(btnCancel);
this.AcceptButton = btnOK;
this.CancelButton = btnCancel;
}
}
}