Monday, September 25, 2023
HomeProgrammingASP.NETHow To Create Login Page Using C# with SQL Database

How To Create Login Page Using C# with SQL Database [Download]

This post was last Updated on by Himanshu Tyagi to reflect the accuracy and up-to-date information on the page. login page

Most modern-day websites have login functionality to authenticate users. With the help of this functionality, website owners can provide special features to only specific users. For example, only registered users on Facebook can like or comment on other people’s posts. So, in case, you want to implement the login functionality in your website, then you have landed on the right page.

In this tutorial, you will learn how to create a simple login page using C# with SQL database. Before moving ahead with the tutorial, make sure you have set up MS Visual Studio. I am using the Visual Studio Ultimate 2011 version for this tutorial and in case you are using any other version, you might have to make some adjustments in the web.config file. In case, there is any issue, leave a comment and I will assist you.

Previously, we also covered how to create registration page. You can check it out if you want to also implement user registration process.

Steps to create login page:

Step 1. Creating an empty ASP.NET website.

Step 2. Designing the Login web page.

Step 3. Creating SQL database.

Step 4. Configuring web.config file.

Step 5. Coding ASP.Net login form

Let’s get started and see how it is done.

How To Create Login page in Using C# with SQL Database

1. Creating an empty ASP.NET website

Open Visual Studio and create an empty website project. Use the shortcut Control+Shift+N to open the dialog box as shown in the screenshot below. Name your project and click on the OK button to continue.

creating an empty login page

After you click on the OK button, Visual Studio will create the solution files and folders for your including Web.config file which we will edit later to connect our database.

2. Designing the Login web page

After your website project is up and running inside Visual Studio, you have to add a web form to your project which will be your login page. Hit Control+Shift+A to quickly open the dialog box from where you can select to add a web form.

Once you add a web form, you can design a simple login page as you can see in the screenshot below. In case, you want to make it more stylish, then feel free to use CSS or any front end framework. To keep it simple for tutorial purposes, I haven’t done any designing part. I will recommend you check out Bootstrap which is a front end component library to build responsive, mobile-first projects on the web.

designing login page

To design your login page, simply insert a table and then drag and drop UI components from the Toolbox (Control + W) to the Design window.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CodeItBro-Login.aspx.cs" Inherits="CodeItBro.CodeItBro_Login" %>

<!DOCTYPE html>

<html xmlns="">
<head runat="server">
    <style type="text/css">
        .auto-style1 {
            width: 100%;
        .auto-style2 {
            width: 216px;
    <form id="form1" runat="server">
        <table class="auto-style1">
                <td class="auto-style2">
                    <asp:Image ID="Image1" runat="server" ImageUrl="~/Images/codeitbro-blog-logo.png" Height="53px" Width="210px" />
                <td class="auto-style2">&nbsp;</td>
                <td class="auto-style2">
                    <asp:Label ID="Label1" runat="server" Text="Email"></asp:Label>
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                <td class="auto-style2">
                    <asp:Label ID="Label2" runat="server" Text="Password"></asp:Label>
                    <asp:TextBox ID="TextBox2" runat="server" TextMode="Password"></asp:TextBox>
                <td class="auto-style2">&nbsp;</td>
                <td class="auto-style2">&nbsp;</td>
                    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Login" />
                <td class="auto-style2">&nbsp;</td>
                    <asp:Label ID="Label3" runat="server" Text="Label" Visible="False"></asp:Label>


3. Creating a SQL database

After designing the login page, let’s now create an SQL database and add it to our project. To add SQL database in your website, use the Control + Shift + A shortcut and then select SQL database from the dialog box.

Visual Studio will show a prompt to create a folder app_data. Simply, click on the OK button to add the SQL database. Now, you can see your SQL database from the Server Explorer toolbar, as shown in the screenshot below.

Now, let’s add a table UserDetails from where we will match the credentials of the users. To add the table, right-click on the Tables menu in the Server Explorer and select the Add New Table option.

adding a new table

After that, Visual Studio will open a tab where you can write an SQL query to create the table. In case, you don’t want to write the SQL query, you can also specify table fields and data table right on the Table Design tab, as you can see in the screenshot below.

Click on the Update button and Visual Studio will then display a dialog box displaying script to make changes in the database. Accept the changes and the table UserDetails will be created in your website database.

create userdetails table to match user credentials for login page

4. Configuring web.config file

In this step, we will configure the web.config file to let our website project to connect with the SQL database.

Before configuring the web.config file, you need to copy the connection string of your SQL database.  For this, go to the properties of your database from the Server Explorer, as you can see in the screenshot below.

properties of login page database

Your connection string will look something similar to this.

Data Source=(LocalDB)\v11.0;AttachDbFilename="c:\users\dell\documents\visual studio 2012\Projects\CodeItBro\CodeItBro\App_Data\Database1.mdf";Integrated Security=True


connection string of website database

Now, add the code below in your web.config file. Add this code between <configuration></configuration> tag.

    <add name="dbconnection" connectionString="Paste Your Connection String"/>

After adding this code, you can move on to the next step to write the logic for your login page.

5. Coding ASP.Net login form

Double click on the Login button (on the Design tab) to open the C Sharp file of your Login page. Now, add these namespaces in your C# code.

using System.Configuration;
using System.Data;
using System.Data.SqlClient;

After adding these namespaces, add the code below inside the scope of protected void Button1_Click(object sender, EventArgs e).

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString);
            SqlCommand cmd = new SqlCommand("select * from UserDetails where Email =@Email and Password=@Password", con);
            cmd.Parameters.AddWithValue("@Email", TextBox1.Text);
            cmd.Parameters.AddWithValue("@Password", TextBox2.Text);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            if (dt.Rows.Count > 0)




                Label3.Visible = true;
                Label3.Text = "Wrong Details";

Do note that Button1 is the ID of your login button and if you have changed the button ID the name of the function will change accordingly.

In this code, we are simply creating an SQL query and passing the user’s entered values i.e., Email and Password to see whether that record exists in the UserDetails table or not. If that record exists, users will be redirected to the specified web page. If not, they will see an error message as shown in the GIF above this tutorial.

Final Words

By following these steps in this tutorial, you will have a better understanding of how a login page works in using the SQL Server database. In case, you have any doubts related to the steps, then do shoot an email at [email protected] or leave a comment. I will try to assist you in any way I can :)

Himanshu Tyagi
Himanshu Tyagi
Hello Friends! I am Himanshu, a hobbyist programmer, tech enthusiast, and digital content creator. Founder Cool SaaS Hunter. With CodeItBro, my mission is to promote coding and help people from non-tech backgrounds to learn this modern-age skill!

Most Popular

Recent Posts

- Advertisment -