Create Login Page In ASP.NET Web Application Using ASP.Net, C# And SQL Server

Category > ASP.NET || Published on : Wednesday, January 17, 2018 || Views: 9887 || Simple User Login Form example in ASP.Net Create Login Page In ASP.NET


Introduction

Here Pawan Kumar will explain how to design and develop simple login and registration form in asp.net using c# with SQL Server as database

Description

In previous post I have explained How to Implement Reorder List (Drag and Drop) using jQuery in ASP.Net, How To Export HTML Table To Excel Using jQuery Plugin, How to Display Decimal Numbers As Money using Transact-SQL, How to Access Data From Ordered Dictionary using C#, How to use Regular Expression (Regex) to accept only Alphanumeric (Alphabets and Numbers) in TextBox in ASP.Net using RegularExpression Validator, Set or Display Watermark Text for ASP.Net TextBox, Password and MultiLine TextArea using jQuery in ASP.Net with demo and Example codes, and many more articles.

Now I will explain How to Create Login Page In ASP.NET Web Application Using ASP.Net, C# And SQL Server

So follow the steps to learn Create Login Page In ASP.NET Web Application Using ASP.Net, C# And SQL Server

In this article we are learn how to design and develop simple login and registration form in asp.net using c# with SQL Server as database

Use the below SQL Script to create database and table. Open SQL Managment Studio on your computer and run the below script.

Create database aspDb    

go 
 
Use aspDb
                    
Create table Userlogin          
(  
   UserId varchar(50) primary key not null,   
   Password varchar(100) not null  
)  
insert into  Userlogin values ('Admin','admin123')  

Above SQL Script will create a new database with name "aspDb" and also create a table which will store our UserId and password for login purpose.

A single dummy UserId and Password has been created to test our application.


Now create a ASP.Net website using Visual Studio. Also we are creating a ASPX page(CS-Source-Code.aspx) with the follwoing code

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CS-Source-Code.aspx.cs" Inherits="CS_Source_Code" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .auto-style1 {
            width: 100%;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <div>

            <table class="auto-style1">
                <tr>
                    <td colspan="6" style="text-align: center; vertical-align: top">
                        <asp:Label ID="Label1" runat="server" Font-Bold="True" Font-Size="XX-Large" Font-Underline="True" Text="Log In DEMO "></asp:Label>
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td style="text-align: center">
                        <asp:Label ID="Label2" runat="server" Font-Size="X-Large" Text="UserId :"></asp:Label>
                    </td>
                    <td style="text-align: center">
                        <asp:TextBox ID="TextBox1" runat="server" Font-Size="X-Large"></asp:TextBox>
                    </td>
                    <td></td>
                    <td></td>
                    <td></td>
                </tr>
                <tr>
                    <td></td>
                    <td style="text-align: center">
                        <asp:Label ID="Label3" runat="server" Font-Size="X-Large" Text="Password :"></asp:Label>
                    </td>
                    <td style="text-align: center">
                        <asp:TextBox ID="TextBox2" runat="server" Font-Size="X-Large" TextMode="Password"></asp:TextBox>
                    </td>
                    <td></td>
                    <td></td>
                    <td></td>
                </tr>
                <tr>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                </tr>
                <tr>
                    <td></td>
                    <td></td>
                    <td style="text-align: center">
                        <asp:Button ID="Button1" runat="server" BorderStyle="None" Font-Size="X-Large" OnClick="Button1_Click" Text="Log In" />
                    </td>
                    <td></td>
                    <td></td>
                    <td></td>
                </tr>
                <tr>
                    <td></td>
                    <td></td>
                    <td>
                        <asp:Label ID="Label4" runat="server" Font-Size="X-Large"></asp:Label>
                    </td>
                    <td></td>
                    <td></td>
                    <td></td>
                </tr>
            </table>

        </div>
    </form>
</body>
</html>

Now we have created the HTML Markup. Next move to code behind.

Create connection string in Web Config with the SQL Server.

<?xml version="1.0"?>

<configuration>
  <connectionStrings>
    <add name="con" connectionString="server=.;database=aspDb;integrated security=true;" />
  </connectionStrings>
  <system.web>
    <compilation debug="false" targetFramework="4.0" />
  </system.web>

</configuration>

Include below 2 namespaces in top of the code behind.

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

Write the below codes in button click event.

protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            string uid = TextBox1.Text;
            string pass = TextBox2.Text;

            con.Open();
            string qry = "select * from Userlogin where UserId='" + uid + "' and Password='" + pass + "'";
            SqlCommand cmd = new SqlCommand(qry, con);
            SqlDataReader sdr = cmd.ExecuteReader();
            if (sdr.Read())
            {
                lblSuccess.Text = "Login Sucess......!!";
            }
            else
            {
                lblSuccess.Text = "UserId & Password Is not correct Try again..!!";

            }
            con.Close();
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }

Complete Code Behind Codes:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;

public partial class CS_Source_Code : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ToString());

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            string uid = TextBox1.Text;
            string pass = TextBox2.Text;

            con.Open();
            string qry = "select * from Userlogin where UserId='" + uid + "' and Password='" + pass + "'";
            SqlCommand cmd = new SqlCommand(qry, con);
            SqlDataReader sdr = cmd.ExecuteReader();
            if (sdr.Read())
            {
                lblSuccess.Text = "Login Sucess......!!";
            }
            else
            {
                lblSuccess.Text = "UserId & Password Is not correct Try again..!!";

            }
            con.Close();
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }
}

When user enter his/her details i.e username and password, on login it will check the existence in database and if found then it will show the success message otherwise displays the error message to user.

Conclusion:

So, In this tutorial we have learned, Create Login Page In ASP.NET Web Application Using ASP.Net, C# And SQL Server

Download Source Codes