How to Create Login Form with MySQL in C#

By FoxLearn 8/26/2024 9:52:42 AM   6.35K
Creating a login form in a C# application that connects to a MySQL database involves several steps.

How to Create Login Form with MySQL in C#

Click New Project, then select Visual C# on the left, then Windows and then select Windows Forms Application. Name your project "LoginMySql" and then click OK

Drag and drop the Panel, Label, TextBox and Button controls from the Visual Studio toolbox onto your form designer, then you can layout your login form as shown below.

login form in c#

Ensure you have a MySQL database setup with a user table that you’ll use for login.

You also need to Install the MySQL Connector/NET, which allows .NET applications to interact with MySQL.

Make sure you have a MySQL database with a table for user authentication.

CREATE TABLE Users (
    username VARCHAR(50) PRIMARY KEY,
    password VARCHAR(255) NOT NULL
);

Next, You can insert some test data

INSERT INTO Users(username, password) VALUES('foxlearn', '123');

After you create a user table, you need to add it to your dataset as shown below.

dataset in c#

Add a click event handler to the login button as shown below.

using System;
using System.Windows.Forms;

namespace LoginMySql
{
    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.Login(txtUsername.Text, txtPassword.Text);
                //Check user exists
                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);
            }
        }
    }
}

Through this example, you have created a basic login form in a C# Windows Forms application that connects to a MySQL database.

VIDEO TUTORIAL