In this article I
will explain how to implement login screen using asp.net
OR
How to Check
Username and Password Exists in database using asp.net.
Login screen is
common for all the websites before access the website. In this article I explained
very simple way to create login screen. User can enter the user name and
password. If user details existing in Database then we need to redirect user to
welcome page otherwise we need to display “Login Failed!” message. In this scenario User name
and password fields are required.
We need to create a
table in Database. Below is sample table structure.
First we need to establish a connection in
Web.config file.
<connectionStrings>
<add name="connectionString" connectionString="Data Source=LocalHost;uid=sa1;password=Con@123;Initial Catalog=practice"/>
</connectionStrings>
Below is the page
design.
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager runat="server" ID="smLogin"></asp:ScriptManager>
<div>
<table align="center">
<tr>
<td>
<b><u style="font-size: large">Login</u></b>
</td>
</tr>
<tr>
<td>User Name:</td>
<td>
<asp:TextBox ID="txtUserName" runat="server" Width="150px">
</asp:TextBox>
<asp:RequiredFieldValidator runat="server" ID="rfvUserName"
ControlToValidate="txtUserName" ErrorMessage="User Name is mandatory" ValidationGroup="Login"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>Password :</td>
<td>
<asp:TextBox runat="server" ID="txtPassword" TextMode="Password" Width="150px"></asp:TextBox>
<asp:RequiredFieldValidator runat="server" ID="rfvPassword"
ControlToValidate="txtPassword" ErrorMessage="Password is mandatory" ValidationGroup="Login"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
<asp:Button runat="server" ID="btnLogin" Text="Login" OnClick="btnLogin_Click" ValidationGroup="Login"/>
</td>
<td>
<asp:Button runat="server" ID="btnCancel" Text="Cancel" OnClick="btnCancel_Click" /></td>
</tr>
</table>
</div>
</form>
</body>
</html>
Below is the total code for Login Screen.
using
System;
using
System.Configuration;
using
System.Data;
using
System.Data.SqlClient;
public partial class Login :
System.Web.UI.Page
{
SqlConnection conCascading = new SqlConnection(ConfigurationManager.ConnectionStrings["Connection"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnLogin_Click(object sender, EventArgs e)
{
conCascading.Open();
SqlCommand
cmdLogin = new SqlCommand("select LoginID from tblLogin where
UserName='" + txtUserName.Text + "'and Password='" + txtPassword.Text + "'", conCascading);
SqlDataAdapter daLogin = new SqlDataAdapter(cmdLogin);
DataSet
dsLogin = new DataSet();
daLogin.Fill(dsLogin);
conCascading.Close();
if (dsLogin.Tables.Count > 0
&& dsLogin.Tables[0].Rows.Count > 0)
{
Session["UserId"] = Convert.ToInt32(dsLogin.Tables[0].Rows[0]["LoginID"].ToString());
Response.Redirect("Home.aspx");
}
else
{
ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Invalid
Username and Password')</script>");
}
}
protected void btnCancel_Click(object sender, EventArgs e)
{
txtUserName.Text = string.Empty;
txtPassword.Text = string.Empty;
}
}
Output:
Step 1 : User Enter Username and Password and click Login button.
Step 2 : If the user is valid user then
Step 3 : Invalid user then
No comments:
Post a Comment