What you want to see on DotNetFunda.com ?
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 15260 |  Welcome, Guest!   Register  Login
Home > Articles > ASP.NET > How to create a login page in ASP.NET (C#) using StoredProcedure.

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

1 vote(s)
Rating: 3 out of 5
Article posted by Rajarajah on 11/19/2012 | Views: 8517 | Category: ASP.NET | Level: Beginner | Points: 200 red flag


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

Download


 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();
}
}


If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

About Raja Rajan

Experience:0 year(s)
Home page:http://www.dotnetfunda.com
Member since:Monday, August 13, 2012
Level:Starter
Status: [Member]
Biography:LIFE IS EXPERIENCE
~kasadara karka~
 Responses
Posted by: Sheonarayan | Posted on: 19 Nov 2012 09:02:22 AM | 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 | Posted on: 19 Nov 2012 11:48:22 AM | Points: 25

@Sheonarayan How are you sir. Thank you for Providing the very good links.

Posted by: Laurenrodriguez | Posted on: 22 Nov 2012 06:39:52 AM | Points: 25

Here the mentioned clear concept about this - http://www.dotnetfunda.com/articles/article114.aspx

>> Write Response - Respond to this post and get points
Related Posts

Hi, in this article you are going to know how to read data form XML file and binding into grid view and updating the new record in the same XML file. Using Methods ReadXml() and WriteXml() Methods. In this article you also come to know how to write the data from DataSet to XML file.

Here in this section we are calculating the count of the records in group.

Diagnosing a software application is an art and this art has to be more skillful when you go on production. In development environment you have the complete VS IDE tool so the diagnosing becomes much easier. On production environment as a best practice you do not install visual studio IDE. So on production it’s like fighting with the lion without a knife.

Every time I get involve in a new project that I happen to inherit, I always come across sql code in the pages and input that is not trapped. Well some of this applications are used internally and some go to clients externaly. One programmer argued about how dangerous it is to write your sql statement in the pages. In this short article am going explain the reasons why is it not a good programming practice or dangerous to your users if you don’t trap the input or if you write your sql on your pages. It is because it leads to sql injection. What is SQl Injection?

After studying a lot on Adrotator control, I came to this point, and tried my best to give my knowledge on this control to my readers. I hope u all will like this article of mine.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/21/2013 10:43:38 AM