Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create asm #1050

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions asm
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();
}
}
}