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