How to create a login page in ASP.NET (C#) using StoredProcedure.

Rajarajah
Posted by in ASP.NET category on for Beginner level | Points: 200 | Views : 83873 red flag
Rating: 3 out of 5  
 1 vote(s)

This Article explains on how to create a login page using C# and will be helpful for the beginners learning ASP.NET


 Download source code for How to create a login page in ASP.NET (C#) using StoredProcedure.

Introduction

This Article explains on how to create a login page using C# and will be helpful for the beginners learning ASP.NET


Objective

Create a login page in ASP.NET (C#) using stored procedure


Using the code

1.CREATE DATABASE AND TABLE
****************************

USE [dbname]
GO

/****** Object: Table [dbo].[Tbl_User] Script Date: 11/19/2012 10:20:37 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[Tbl_User](
[id] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](50) NULL,
[UserName] [varchar](50) NULL,
[Password] [varchar](50) NULL,
[Gender] [varchar](1) NULL,
[City] [varchar](50) NULL
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

2.CREATE STORED PROCEDURE
**************************

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: RAJARAJAN

-- Description: LOGIN CHECK
-- =============================================
CREATE PROCEDURE [dbo].[Sp_loginCheck]
@UserName varchar(50),
@Password varchar(50)
as
BEGIN
SET NOCOUNT ON;

declare @log as varchar(50)

select @log=COUNT(id) from Tbl_User where UserName=@UserName AND [Password]=@Password

if(@log>0)

begin

select id,Name from Tbl_User where UserName=@UserName AND Password=@Password

end

else

begin

raiserror('Login Failed',16,1)

end

END

3.loginpage.aspx
******************

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Login_page.aspx.cs" Inherits="_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></title>
<style type="text/css">
.style1
{
width: 100%;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<br />
<table class="style1" bgcolor="#99CCFF">
<tr>
<td align="center">
<br />
<asp:Login ID="Login1" runat="server" BackColor="#F7F6F3" BorderColor="#E6E2D8"
BorderPadding="4" BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana"
Font-Size="0.8em" ForeColor="#333333" Height="148px"
onauthenticate="Login1_Authenticate" Width="295px">
<TextBoxStyle Font-Size="0.8em" />
<LoginButtonStyle BackColor="#FFFBFF" BorderColor="#CCCCCC" BorderStyle="Solid"
BorderWidth="1px" Font-Names="Verdana" Font-Size="0.8em" ForeColor="#284775" />
<InstructionTextStyle Font-Italic="True" ForeColor="Black" />
<TitleTextStyle BackColor="#5D7B9D" Font-Bold="True" Font-Size="0.9em"
ForeColor="White" />
</asp:Login>
<br />
<asp:Label ID="l_succ" runat="server" ForeColor="#FF0066"></asp:Label>
</td>
</tr>
</table>
<br />
<br />
</div>
</form>
</body>
</html>

4.login code behind(loginpage.aspx.cs)
***************************************

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

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
string ErrorMessage = "";
string id, FullName = "";
try
{

//SqlConnection con = new SqlConnection("Data Source=servername;Initial Catalog=databasename;User ID=userid;Password=password");

SqlConnection con = new SqlConnection("Data Source=USER-PC;Initial Catalog=db_user;User ID=sa;Password=nrr");
con.Open();
SqlCommand cmd = new SqlCommand("Sp_LoginCheck", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@UserName", Login1.UserName);
cmd.Parameters.Add("@Password", Login1.Password);
SqlDataReader read;
read = cmd.ExecuteReader();
read.Read();
id = read.GetValue(0).ToString();
FullName = read.GetValue(1).ToString();
read.Close();
con.Close();
HttpCookie c_id = new HttpCookie("id");
HttpCookie c_fullname = new HttpCookie("FullName");
c_id.Value = id;
c_fullname.Value = FullName;
Response.Cookies.Add(c_id);
Response.Cookies.Add(c_fullname);
if (id != " ")
{
Response.Redirect("Next_Page.aspx");
}
}
catch (Exception eee)
{
//if you want to display error message in UI just like to use l_succ lable.
//l_succ.Text = eee.Message;

ErrorMessage = eee.Message;
ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script>alert('"+ErrorMessage+"')</script>");

}

}
}

5.Next_Page.aspx
*****************

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Next_Page.aspx.cs" Inherits="Next_Page" %>

<!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></title>
<style type="text/css">
.style1
{
width: 100%;
}
.style2
{
width: 635px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>

<br />
<br />
<br />
<table class="style1" border="30" cellpadding="2" cellspacing="2" >
<tr>
<td class="style2">
<asp:Label ID="L_id" runat="server"></asp:Label>
</td>
<td>
<asp:Label ID="l_name" runat="server"></asp:Label>
</td>
</tr>
</table>
<br />
<br />
<br />
</div>
</form>
</body>
</html>

6.code behind(nextpage.aspx.cs)
******************************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Next_Page : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
L_id.Text = Request.Cookies["id"].Value.ToString();
l_name.Text = Request.Cookies["FullName"].Value.ToString();
}
}


Page copy protected against web site content infringement by Copyscape

About the Author

Rajarajah
Full Name: Raja Rajan
Member Level: Starter
Member Status: Member
Member Since: 8/13/2012 11:11:48 AM
Country: India
N.RAJA RAJAN 8608382317
http://www.dotnetfunda.com
LIFE IS EXPERIENCE ~kasadara karka~

Login to vote for this post.

Comments or Responses

Posted by: Sheonarayan on: 11/19/2012 | Points: 25
Hi Rajarajah,

I do not think this is the right approach to create login page in ASP.NET. The standard way to create Login page is using Forms authentication, read this article http://www.dotnetfunda.com/articles/article114.aspx.

Using inbuilt forms authentication to create login page has many benefits.

If you want roles based authentication to do, use this article http://www.dotnetfunda.com/articles/article141.aspx.

Thanks
Posted by: Nagarajasia on: 11/19/2012 | Points: 25
@Sheonarayan How are you sir. Thank you for Providing the very good links.
Posted by: Laurenrodriguez on: 11/22/2012 | Points: 25
Here the mentioned clear concept about this - http://www.dotnetfunda.com/articles/article114.aspx
Posted by: Priyagoel_24 on: 9/10/2013 | Points: 25
Server Error in '/payroll3' Application.
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

Source Error:

Line 33: password.Value = txtPWD.Text.Trim().ToString();
Line 34: cmd.Parameters.Add(password);
Line 35: cnn.Open();
Line 36: int result =(Int32)cmd.ExecuteScalar();
Line 37: cnn.Close();

Login to post response

Comment using Facebook(Author doesn't get notification)