Windows Forms: Create Login Form with SQL Server in C#

This post show you How to Create a Login Window in C#, then implement logic to authenticate users with SQL Server using Entity Framework in C# Windows Forms Application

Creating a login form in C# involves designing the user interface and implementing the logic to authenticate users.

How to create login form in c#?

Here's a simple example of how to create a login form in c# windows application with database

Open your Visual Studio, then click New Project, then select Visual C# on the left, then Windows and then select Windows Forms Application. Name your project "LoginForm" and then click OK button.

Next, Open your form designer, then drag and drop labels, textboxes for username and password input, a button for logging in, a panel for image from your Visual Studio toolbox onto your forn, then design your windows form login as below.

Login form design windows

c# login form design

Create a user table, then add the user table to Entity Framework Model First as below

login form c#

Adding a KeyPress event handler to the TextBox control allows you to set password focus when entering keys at the username textbox and clicking the login button when entering at the password textbox.

private void txtUsername_KeyPress(object sender, KeyPressEventArgs e)
    if (e.KeyChar == (char)13)//Enter key

private void txtPassword_KeyPress(object sender, KeyPressEventArgs e)
    if (e.KeyChar == (char)13)

In this example, txtUsername and txtPassword are TextBox controls where the user can input their username and password, respectively. btnLogin is a Button control for initiating the login process.

Finally, add a click event handler allows you to handle the logic for authenticating the user. You can use linq to sql to help you query the user table with name and password condition.

// c# login form code
private void btnLogin_Click(object sender, EventArgs e)
    if (string.IsNullOrEmpty(txtUsername.Text))
        MessageBox.Show("Please enter your username.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        using (TestEntities test = new TestEntities())
            //using linq to query data
            var query = from o in test.Users
                        where o.Username == txtUsername.Text && o.Password == txtPassword.Text
                        select o;
            //check if user exists
            if (query.SingleOrDefault() != null)
                MessageBox.Show("You have been successfully logged in.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                //Add your code process login here
                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);

This is a simple login form in c# for beginners, in a real-world application, you would typically use more secure authentication methods, such as hashing and salting passwords, and possibly integrate with a database or an authentication service.