diff --git a/asm b/asm new file mode 100644 index 00000000..be0b1e42 --- /dev/null +++ b/asm @@ -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(); + } + } +}