-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStatusManagement.cs
More file actions
233 lines (200 loc) · 8.26 KB
/
Copy pathStatusManagement.cs
File metadata and controls
233 lines (200 loc) · 8.26 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
using H1;
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 DB_Project
{
public partial class StatusManagement : Form
{
// Your specified connection string
private string connectionString = @"Data Source=ABDULSABOOR190\SQLEXPRESS;Initial Catalog=sabbbb;Integrated Security=True;Encrypt=False";
public StatusManagement()
{
InitializeComponent();
LoadFormData();
}
private void LoadFormData()
{
// Initialize the form with default selections
comboBox1.SelectedIndex = 0; // Default to "User" type
comboBox2.SelectedIndex = 0; // Default to "Active" status
}
private void label2_Click(object sender, EventArgs e)
{
// Empty event handler
}
private void label4_Click(object sender, EventArgs e)
{
// Empty event handler
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
// Empty event handler
}
private void button1_Click(object sender, EventArgs e)
{
Form1 f1 = new Form1();
f1.Show();
}
private void button2_Click(object sender, EventArgs e)
{
Role_Permission role_Permission = new Role_Permission();
role_Permission.Show();
}
private void button4_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2(2);
form2.Show();
}
private void button6_Click(object sender, EventArgs e)
{
Review_Moderation_Form review_Moderation_Form = new Review_Moderation_Form();
review_Moderation_Form.Show();
}
private void button9_Click(object sender, EventArgs e)
{
}
private void button5_Click(object sender, EventArgs e)
{
// Retrieve the user ID from numericUpDown1
int userId = (int)numericUpDown1.Value;
// Retrieve the selected status from comboBox2
string selectedStatus = comboBox2.SelectedItem?.ToString();
// Retrieve the user type from comboBox1
string userType = comboBox1.SelectedItem?.ToString();
// Validate inputs
if (userId <= 0)
{
MessageBox.Show("Please enter a valid User ID.", "Invalid Input", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
if (string.IsNullOrEmpty(selectedStatus))
{
MessageBox.Show("Please select a valid status.", "Invalid Input", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
if (string.IsNullOrEmpty(userType))
{
MessageBox.Show("Please select a valid user type.", "Invalid Input", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
try
{
// Update the user's status in the database
bool isUpdated = UpdateUserStatus(userId, selectedStatus, userType);
if (isUpdated)
{
MessageBox.Show("User status updated successfully.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("Failed to update user status. User ID not found or you don't have permission.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
catch (Exception ex)
{
MessageBox.Show($"An error occurred: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private bool UpdateUserStatus(int userId, string status, string userType)
{
// Convert "Active"/"Inactive" to bit value for database
bool isActive = (status == "Active");
// SQL query to update the user's status based on user type
string query;
// Choose the correct table based on user type
switch (userType.ToLower())
{
case "user":
case "traveler":
// Use the AppUser table for travelers/users
query = "UPDATE AppUser SET is_active = @isActive WHERE user_id = @userId";
break;
case "operator":
// Verify the user exists in TourOperatorProfile first, then update AppUser
query = @"IF EXISTS (SELECT 1 FROM TourOperatorProfile WHERE user_id = @userId)
BEGIN
UPDATE AppUser SET is_active = @isActive WHERE user_id = @userId
END";
break;
case "admin":
// Verify the user exists in AdminProfile first, then update AppUser
query = @"IF EXISTS (SELECT 1 FROM AdminProfile WHERE user_id = @userId)
BEGIN
UPDATE AppUser SET is_active = @isActive WHERE user_id = @userId
END";
break;
default:
// Default to checking any user
query = "UPDATE AppUser SET is_active = @isActive WHERE user_id = @userId";
break;
}
try
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
// Open the database connection
connection.Open();
// Check if the user exists before attempting update
string checkUserQuery = "SELECT COUNT(*) FROM AppUser WHERE user_id = @userId";
using (SqlCommand checkCommand = new SqlCommand(checkUserQuery, connection))
{
checkCommand.Parameters.AddWithValue("@userId", userId);
int userCount = (int)checkCommand.ExecuteScalar();
if (userCount == 0)
{
// User doesn't exist
return false;
}
}
// Create a SQL command with the query and connection
using (SqlCommand command = new SqlCommand(query, connection))
{
// Add parameters to prevent SQL injection
command.Parameters.AddWithValue("@isActive", isActive);
command.Parameters.AddWithValue("@userId", userId);
// Execute the query and check if any rows were affected
int rowsAffected = command.ExecuteNonQuery();
// Return true if at least one row was updated, otherwise false
return rowsAffected > 0;
}
}
}
catch (Exception ex)
{
// Log the error and rethrow
MessageBox.Show($"Database error: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
throw;
}
}
// Add a method to check if a user exists
private bool UserExists(int userId)
{
try
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
string query = "SELECT COUNT(*) FROM AppUser WHERE user_id = @userId";
using (SqlCommand command = new SqlCommand(query, connection))
{
command.Parameters.AddWithValue("@userId", userId);
int count = (int)command.ExecuteScalar();
return count > 0;
}
}
}
catch (Exception)
{
return false;
}
}
}
}