-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.cs
More file actions
113 lines (105 loc) · 3.77 KB
/
Form1.cs
File metadata and controls
113 lines (105 loc) · 3.77 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
using Microsoft.Data.SqlClient;
using System.Configuration;
using System.Data;
namespace Managment
{
public partial class Form1 : Form
{
SqlConnection conn = new SqlConnection(StaticVariables.connectionString);
Thread newFormThread;
public Form1()
{
InitializeComponent();
}
private async void loginButton_Click(object sender, EventArgs e)
{
string userName = usernameText.CustomText;
string password = passwordText.CustomText;
try
{
string sqlQuery = "SELECT * FROM USERS WHERE username=@username AND userPassword=@password";
using (SqlCommand cmd = new SqlCommand(sqlQuery, conn))
{
// Use parameterized queries to prevent SQL injection
cmd.Parameters.AddWithValue("@username", userName);
cmd.Parameters.AddWithValue("@password", password);
await conn.OpenAsync(); // Open the connection asynchronously
using (SqlDataReader reader = await cmd.ExecuteReaderAsync()) // Execute query asynchronously
{
if (await reader.ReadAsync()) // Check if any rows are returned
{
errorMessage.Text = "";
this.Hide(); // Hide the login form
MainForm mainForm = new MainForm("John", this);
mainForm.Show();
}
else
{
errorMessage.Text = "Incorrect values";
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
if (conn.State == ConnectionState.Open)
{
await conn.CloseAsync(); // Close the connection asynchronously
}
}
}
private void usernameText_Enter(object sender, EventArgs e)
{
if (usernameText.CustomText == "Username")
{
usernameText.CustomText = "";
usernameText.CustomTextColor = Color.Black;
}
errorMessage.Text = "";
}
private void passwordText_Enter(object sender, EventArgs e)
{
if (passwordText.CustomText == "Password")
{
passwordText.CustomText = "";
passwordText.CustomPasswordChar = '*';
passwordText.CustomTextColor = Color.Black;
}
errorMessage.Text = "";
}
private void usernameText_Leave(object sender, EventArgs e)
{
if (usernameText.CustomText == "")
{
usernameText.CustomText = "Username";
usernameText.CustomTextColor = Color.LightGray;
}
}
private void passwordText_Leave(object sender, EventArgs e)
{
if (passwordText.CustomText == "")
{
passwordText.CustomText = "Password";
passwordText.CustomPasswordChar = '\0';
passwordText.CustomTextColor = Color.LightGray;
}
}
private void label2_Click(object sender, EventArgs e)
{
Application.Exit();
}
public void ClearValues()
{
usernameText.CustomText = "";
passwordText.CustomText = "";
usernameText_Leave(null, EventArgs.Empty);
passwordText_Leave(null, EventArgs.Empty);
errorMessage.Text = "";
usernameText.Focus();
}
}
}