-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
using System; | ||
using System.Data; | ||
using System.Data.SqlClient; | ||
using System.Windows.Forms; | ||
|
||
namespace SalesManagementSystem | ||
{ | ||
public partial class Login : Form | ||
{ | ||
private string connectionString = @"Server=DINHKHIEM\SQLEXPRESS01;Database=hoangdinhkhiem;Trusted_Connection=True;"; | ||
|
||
public Login() | ||
{ | ||
InitializeComponent(); | ||
} | ||
|
||
// Executes when the login button is clicked | ||
private void btnLogin_Click(object sender, EventArgs e) | ||
{ | ||
string username = txtUsername.Text.Trim(); | ||
string password = txtPassword.Text; | ||
|
||
if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password)) | ||
{ | ||
MessageBox.Show("Please enter your username and password!", "Notice", MessageBoxButtons.OK, MessageBoxIcon.Warning); | ||
return; | ||
} | ||
|
||
try | ||
{ | ||
using (SqlConnection conn = new SqlConnection(connectionString)) | ||
{ | ||
conn.Open(); | ||
string query = "SELECT COUNT(*) FROM Account WHERE Username = @Username AND Password = @Password"; | ||
using (SqlCommand cmd = new SqlCommand(query, conn)) | ||
{ | ||
cmd.Parameters.Add("@Username", SqlDbType.NVarChar).Value = username; | ||
cmd.Parameters.Add("@Password", SqlDbType.NVarChar).Value = password; | ||
|
||
int result = (int)cmd.ExecuteScalar(); | ||
|
||
if (result > 0) | ||
{ | ||
MessageBox.Show("Login successful!", "Notice", MessageBoxButtons.OK, MessageBoxIcon.Information); | ||
MainForm mainForm = new MainForm(); | ||
this.Hide(); | ||
mainForm.Show(); | ||
} | ||
else | ||
{ | ||
MessageBox.Show("Username or password is incorrect!", "Notice", MessageBoxButtons.OK, MessageBoxIcon.Error); | ||
} | ||
} | ||
} | ||
} | ||
catch (SqlException sqlEx) | ||
{ | ||
MessageBox.Show("Database error: " + sqlEx.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); | ||
} | ||
catch (Exception ex) | ||
{ | ||
MessageBox.Show("Unexpected error: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); | ||
} | ||
} | ||
|
||
// Open the registration form | ||
private void btnRegister_Click(object sender, EventArgs e) | ||
{ | ||
this.Hide(); | ||
Register registerForm = new Register(); | ||
registerForm.Show(); | ||
} | ||
|
||
// Close the application | ||
private void btnExit_Click(object sender, EventArgs e) | ||
{ | ||
Application.Exit(); | ||
} | ||
} | ||
} |