Thursday, 20 February 2014

How To Write a Simple Login Page In Asp.net

 In this article i will show you how you can create a simple login page by using sql sqerver.

So for this article here we go.
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-decoration: underline;
}</style>
</head>
<body style="background-color: White;">
<form id="Form1" runat="server">
<div>
<h3>
            
     
           <span class="style1"><strong>
LOGIN PAGE</strong></span></h3>
</div>
<div>
     User Id:
<asp:TextBox runat="server" Width="199px" ID="txtuserid"></asp:TextBox></div>
<div>
 </div>
<div>
Password :<asp:TextBox ID="txtpassword" TextMode="Password" runat="server" Width="199px"></asp:TextBox></div>
<div>
              
            
<asp:Button runat="server" Text="Login" ID="btnlogin" OnClick="btnlogin_Click" />
    
<asp:Button ID="Button1" runat="server" Text="Cancle" />
<br />
    
<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_NULLSON
GOSET QUOTED_IDENTIFIER ON
GOSET ANSI_PADDING ON
GOCREATE TABLE [dbo].[UserLogin](
[Id] [int] IDENTITY(1,1) NOT NULL,
[UserId] [varchar](50) NULL,
[Password] [varchar](50) NULL
 ) ON [PRIMARY]
GOSET 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 making 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 password entered in table.

Now run the page.

enter corect user id and password and press login button 



Now enter wrong user id and password



Tags: Asp.Net , C#.Net

No comments:

Post a Comment