Login Page Using Stored Procedure

In this blog, we will learn how to create a Login Page Using the Stored Procedure.

Stored Procedure 

CREATE PROCEDURE login_pro
(
    @UserName VARCHAR(50),
    @Password VARCHAR(50)
)
AS
BEGIN
    DECLARE @status INT

    IF EXISTS(SELECT * FROM Login WHERE UserName = @UserName AND Password = @Password)
        SET @status = 1
    ELSE
        SET @status = 0

    SELECT @status
END

Default.aspx 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Login_Page_Using_Stored_Procedure._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Label ID="Label1" runat="server" Text="Name" Font-Bold="True" Width="100px" BackColor="#FFFF66" ForeColor="#FF3300"></asp:Label>
            <asp:TextBox ID="TextBox_user_name" runat="server" ForeColor="#993300" Width="100px"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox_user_name" ErrorMessage="Please enter username"></asp:RequiredFieldValidator>
            <br />
            <asp:Label ID="Label2" runat="server" Text="Password" Font-Bold="True" Width="100px" BackColor="#FFFF66" ForeColor="#FF3300"></asp:Label>
            <asp:TextBox ID="TextBox_password" runat="server" ForeColor="#CC6600" TextMode="Password" Width="100px"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="TextBox_password" ErrorMessage="Please enter password"></asp:RequiredFieldValidator>
            <br />
            <asp:Button ID="btn_login" runat="server" Text="Login" Font-Bold="True" BackColor="#CCFF99" onclick="btn_login_Click" />
            <br />
            <asp:Label ID="lblmessage" runat="server" Font-Bold="True" ForeColor="#FF3300"></asp:Label>
            <br />
        </div>
    </form>
</body>
</html>

Default.aspx.cs 

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

namespace Login_Page_Using_Stored_Procedure
{
    public partial class _Default : System.Web.UI.Page
    {
        string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        SqlCommand com;
        SqlParameter UserName, Password;

        protected void btn_login_Click(object sender, EventArgs e)
        {
            UserName = new SqlParameter();
            Password = new SqlParameter();
            SqlConnection con = new SqlConnection(strConnString);

            com = new SqlCommand();
            com.Connection = con;
            con.Open();
            Session["UserName"] = TextBox_user_name.Text;
            com.CommandType = CommandType.StoredProcedure;
            com.CommandText = "login_pro";

            UserName.SqlDbType = SqlDbType.VarChar;
            UserName.Size = 50;
            UserName.ParameterName = "@UserName";
            UserName.Value = TextBox_user_name.Text.ToString();
            UserName.Direction = ParameterDirection.Input;

            Password.SqlDbType = SqlDbType.VarChar;
            Password.Size = 50;
            Password.ParameterName = "@Password";
            Password.Value = TextBox_password.Text.ToString();
            Password.Direction = ParameterDirection.Input;

            com.Parameters.Add(UserName);
            com.Parameters.Add(Password);

            int status;
            status = Convert.ToInt16(com.ExecuteScalar());

            if (status == 1)
            {
                Response.Redirect("Welcome.aspx");
            }
            else
            {
                lblmessage.Text = "Invalid UserName and Password...";
            }
            con.Close();
        }
    }
}

Welcome. aspx 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Welcome.aspx.cs" Inherits="Login_Page_Using_Stored_Procedure.Welcome" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Label ID="lb1" runat="server" Font-Bold="True" ForeColor="#FF3300"></asp:Label><br />
        </div>
    </form>
</body>
</html>

Welcome.aspx.cs 

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace Login_Page_Using_Stored_Procedure
{
    public partial class Welcome : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            lb1.Text = "WELCOME :: " + Session["UserName"];
        }
    }
}

Thanks for reading

Next Recommended Reading Login Page In MVC Using Entity Framework