-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUser&Operator.cs
More file actions
158 lines (141 loc) · 5.85 KB
/
Copy pathUser&Operator.cs
File metadata and controls
158 lines (141 loc) · 5.85 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
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 System.Windows.Forms.VisualStyles;
namespace DB_Project
{
public partial class User_Operator : Form
{
// Updated connection string as specified
private string connectionString = @"Data Source=ABDULSABOOR190\SQLEXPRESS;Initial Catalog=sabbbb;Integrated Security=True;Encrypt=False";
public User_Operator()
{
InitializeComponent();
// Load status and role comboboxes
LoadComboBoxes();
}
private void LoadComboBoxes()
{
// Ensure the comboboxes have default selections
if (comboBox1.Items.Count > 0)
comboBox1.SelectedIndex = 0;
if (comboBox2.Items.Count > 0)
comboBox2.SelectedIndex = 0;
}
private void label4_Click(object sender, EventArgs e)
{
// Empty event handler
}
private void button1_Click(object sender, EventArgs e)
{
Role_Permission role_Permission = new Role_Permission();
role_Permission.Show();
}
private void button2_Click(object sender, EventArgs e)
{
Tour_Category_Management tour_Category_Management = new Tour_Category_Management();
tour_Category_Management.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 button3_Click(object sender, EventArgs e)
{
AnalyticsFilter filter = new AnalyticsFilter();
filter.Show();
}
private void button4_Click(object sender, EventArgs e)
{
StatusManagement statusManagement = new StatusManagement();
statusManagement.Show();
}
private bool TestDatabaseConnection()
{
try
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
MessageBox.Show("Connection successful!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
return true;
}
}
catch (Exception ex)
{
MessageBox.Show("Connection failed: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
}
private void button5_Click(object sender, EventArgs e)
{
// Test connection first
if (!TestDatabaseConnection()) return;
// Validate input
if (string.IsNullOrWhiteSpace(textBox1.Text) ||
string.IsNullOrWhiteSpace(textBox2.Text) ||
string.IsNullOrWhiteSpace(maskedTextBox1.Text) ||
comboBox1.SelectedItem == null ||
comboBox2.SelectedItem == null)
{
MessageBox.Show("Please fill all required fields", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
// Retrieve input values
string fullName = textBox1.Text;
string email = textBox2.Text;
string role = comboBox1.SelectedItem.ToString();
string password = maskedTextBox1.Text;
string status = comboBox2.SelectedItem.ToString();
// Convert status to bit value for is_active
bool isActive = (status == "Active");
try
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
string query = @"
INSERT INTO AppUser (email, password_hash, registration_status, role, is_active)
VALUES (@email, @password_hash, @registration_status, @role, @is_active)";
using (SqlCommand command = new SqlCommand(query, connection))
{
// Add parameters to prevent SQL injection
command.Parameters.AddWithValue("@email", email);
command.Parameters.AddWithValue("@password_hash", password);
command.Parameters.AddWithValue("@registration_status", "approved");
command.Parameters.AddWithValue("@role", role.ToLower());
command.Parameters.AddWithValue("@is_active", isActive);
int rowsAffected = command.ExecuteNonQuery();
if (rowsAffected > 0)
{
MessageBox.Show("User added successfully to AppUser table!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
// Clear form fields
textBox1.Text = "";
textBox2.Text = "";
maskedTextBox1.Text = "";
comboBox1.SelectedIndex = 0;
comboBox2.SelectedIndex = 0;
}
else
{
MessageBox.Show("Failed to add user.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message + "\r\n\r\nStack Trace: " + ex.StackTrace,
"Database Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}