Windows Forms: Create Login Form with Access Database in C#
By FoxLearn 6/1/2017 10:14:22 PM 16.24K
How to Create a Login Window with MS Access Database in C#
Step 1: Click New Project, then select Visual C# on the left, then Windows and then select Windows Forms Application. Name your project "AppLogin" and then click OK
Step 2: Design your login form as below
Step 3: Create a user table, then add the user table to your dataset as below
Step 4: Add a click event handler to the button
using System; using System.Windows.Forms; namespace AppLogin { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void txtUsername_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == (char)13) txtPassword.Focus(); } private void txtPassword_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == (char)13) btnLogin.PerformClick(); } private void btnLogin_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(txtUsername.Text)) { MessageBox.Show("Please enter your username.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Warning); txtUsername.Focus(); return; } try { //Get data from user table AppDataTableAdapters.UsersTableAdapter user = new AppDataTableAdapters.UsersTableAdapter(); AppData.UsersDataTable dt = user.GetDataByUsernamePassword(txtUsername.Text, txtPassword.Text); //Check row > 0 if (dt.Rows.Count > 0) { MessageBox.Show("You have been successfully logged in.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information); //Process your login here } else { MessageBox.Show("Your username or password is incorrect.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information); } } catch (Exception ex) { MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } }
VIDEO TUTORIALS
Categories
Popular Posts
How to Download Chromedriver for Selenium
08/29/2024
C# Tutorial
07/20/2024
How to Download Microsoft SQL Server
06/22/2024