Monday, 4 March 2013

How To Write a Simple Login Page In Asp.net

How To Write a Simple Login Page In Asp.net


First create a new asp.net project and add a new page. in this page add textbox and button control for creating the 
login form. Now create the click event of the login button.

<%@ Page Title="Home Page" Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"
Inherits="Simple_login_page._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head id="Head1" runat="server">
<title></title>
<link href="~/Styles/Site.css" rel="stylesheet" type="text/css" />
<style type="text/css">
.style1
{
text-decorationunderline;
}
</style>
</head>
<body style="background-color: White;">
<form id="Form1" runat="server">
<div>
<h3>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span class="style1"><strong>
LOGIN PAGE</strong></span></h3>
</div>
<div>
&nbsp;&nbsp;&nbsp;&nbsp; User Id:
<asp:TextBox runat="server" Width="199px" ID="txtuserid"></asp:TextBox></div>
<div>
&nbsp;</div>
<div>
Password :
<asp:TextBox ID="txtpassword" TextMode="Password" runat="server" Width="199px"></asp:TextBox></div>
<div>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<asp:Button runat="server" Text="Login" ID="btnlogin" OnClick="btnlogin_Click" />
&nbsp;&nbsp;&nbsp;&nbsp;
<asp:Button ID="Button1" runat="server" Text="Cancle" />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;
<asp:Label ID="lblmessage" runat="server" Style="color: #FF0000" Text=""></asp:Label>
</div>
</form>
</body>
</html>
you code page will look like as shown below.

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.Data;
namespace Simple_login_page
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnlogin_Click(object sender, EventArgs e)
{

}
}
Now open your sel server and create a new table named userlogin

GO
/****** Object: Table [dbo].[UserLogin] Script Date: 04/16/2013 22:59:00 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[UserLogin](
[Id] [int] IDENTITY(1,1) NOT NULL,
[UserId] [varchar](50) NULL,
[Password] [varchar](50) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO


Now add the value in table eg: user id :demo and password: demo.

Now again come to your vs and come to your .cs page. Add the below code on buton click event of the page.

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.Data;
namespace Simple_login_page
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnlogin_Click(object sender, EventArgs e)
{
DataTable objdt = new DataTable();
SqlConnection con = new SqlConnection("Server=NIMISHA-PC\\SQLEXPRESS;Database=DemoArticles;Uid=so;Pwd=123;");
SqlDataAdapter objdat = new SqlDataAdapter("Select * from UserLogin where UserId=`" + txtuserid.Text + "` and Password=`" + txtpassword.Text + "`", con);
objdat.Fill(objdt);
if (objdt.Rows.Count > 0)
{
lblmessage.Text = "Successfull login";
}
else
{
lblmessage.Text = "Unsuccessfull Login.";
}
}
}
}

In above code i have first created instance for makin connection with sql server after that we have used sqladaptor to execute the sql query for verifying the user id and password for database table.

Here is the user id and passwod enterdd in table.

Now run the page.


enter corect user id and password and press login button



Now enter wrong user id and password



_______________________________

_______________________________

No comments:

Post a Comment