NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

using System;
using System.Data.SqlClient;
using System.Windows;
using System.Windows.Input;

namespace KTKTM_DucTrung.Views
{
public partial class LoginWindow : Window
{
string connStr = @"Data Source=DESKTOP-04JT62LSQLEXPRESS01;Initial Catalog=QuanLySinhVien;Integrated Security=True";

public LoginWindow()
{
InitializeComponent();
}

// Sự kiện nhấn phím trong PasswordBox
private void PasswordBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter) // Nếu phím nhấn là Enter
{
BtnLogin_Click(sender, e); // Gọi hàm xử lý đăng nhập
}
}

// Xử lý sự kiện nhấn nút Đăng nhập
private void BtnLogin_Click(object sender, RoutedEventArgs e)
{
string username = txtUsername.Text.Trim();
string password = txtPassword.Password.Trim();

// Kiểm tra nếu tên đăng nhập hoặc mật khẩu rỗng
if (username == "" || password == "")
{
txtMessage.Text = "Vui lòng nhập đầy đủ thông tin.";
return;
}

try
{
using (SqlConnection conn = new SqlConnection(connStr))
{
conn.Open();
string sql = @"
SELECT u.IdStudent, ur.IdRole
FROM [User] u
JOIN [UserRole] ur ON u.IdStudent = ur.IdStudent
WHERE u.Username = @Username AND u.Password = @Password AND u.Status = 1";

using (SqlCommand cmd = new SqlCommand(sql, conn))
{
cmd.Parameters.AddWithValue("@Username", username);
cmd.Parameters.AddWithValue("@Password", password); // Xử lý mật khẩu thô (cần cải tiến bảo mật)

using (SqlDataReader reader = cmd.ExecuteReader())
{
if (reader.Read()) // Đăng nhập thành công
{
string role = reader["IdRole"].ToString();
string idStudent = reader["IdStudent"].ToString();

// Đăng nhập thành công, hiển thị thông báo
MessageBox.Show("Đăng nhập thành công!", "Thông báo");

// Mở cửa sổ chính tùy thuộc vào vai trò
if (role == "R1") // Admin
{
MainAdminWindow adminWindow = new MainAdminWindow(idStudent);
adminWindow.Show();
}
else if (role == "R2") // User
{
MainUserWindow userWindow = new MainUserWindow(idStudent);
userWindow.Show();
}

// Đóng cửa sổ đăng nhập
this.Close();
}
else
{
// Nếu đăng nhập thất bại
txtMessage.Text = "Tên đăng nhập hoặc mật khẩu không đúng!";
}
}
}
}
}
catch (Exception ex)
{
// Xử lý lỗi kết nối hoặc lỗi SQL
MessageBox.Show("Đã xảy ra lỗi: " + ex.Message, "Lỗi", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
}
     
 
what is notes.io
 

Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 14 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.