Wednesday, 5 June 2013

Simple loginCheck Username and Password availability in database

Simple loginCheck Username and Password availability in database

Introduction

In this post I will explain how to implement simple login form using asp.net and I will explain how to Check Username and Password Exists in database using asp.net .

Description: 

One day I got mail from one of the reader he has asked about how to implement simple login form to check username and password details of user in database. If user details exist in database then we need to redirect user to welcome page otherwise we need to display “Invalid Username/Password”. Mostly it’s common for all the websites before access the website. I know that many of them feel it’s very easy but for the people who have started learning .NET they don’t know how to implement this because of that I decided to write post to help for the persons who is in need with this requirement. 

To implement this one first design table like this

ColumnName
DataType
UserId
Int(set identity property=true)
UserName
varchar(50)
Password
varchar(50)
FirstName
varchar(50)
LastName
varchar(50)

After completion of table creation enter some dummy data or use this link to create user registration form to insert userdetails in database.

Once data entered in table design your aspx page like this


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Login Form</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>
Username:
</td>
<td>
<asp:TextBox ID="txtUserName" runat="server"/>
<asp:RequiredFieldValidator ID="rfvUser" ErrorMessage="Please enter Username"ControlToValidate="txtUserName" runat="server" />
</td>
</tr>
<tr>
<td>
Password:
</td>
<td>
<asp:TextBox ID="txtPWD" runat="server" TextMode="Password"/>
<asp:RequiredFieldValidator ID="rfvPWD" runat="server" ControlToValidate="txtPWD"ErrorMessage="Please enter Password"/>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" onclick="btnSubmit_Click" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
After that add the following namespaces in code behind

C# Code

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
After add namespaces write the following code in code behind

protected void btnSubmit_Click(object sender, EventArgs e)
{
SqlConnection con = newSqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand("select * from UserInformation where UserName =@username and Password=@password",con);
cmd.Parameters.AddWithValue("@username", txtUserName.Text);
cmd.Parameters.AddWithValue("@password", txtPWD.Text);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if(dt.Rows.Count>0)
{
Response.Redirect("Details.aspx");
}
else
{
ClientScript.RegisterStartupScript(Page.GetType(), "validation""<script language='javascript'>alert('Invalid Username and Password')</script>");
}
}

VB.NET Code

Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration

Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As ObjectByVal e As EventArgs)
End Sub
Protected Sub btnSubmit_Click(ByVal sender As ObjectByVal e As EventArgs)
Dim con As NewSqlConnection(ConfigurationManager.ConnectionStrings("dbconnection").ConnectionString)
con.Open()
Dim cmd As New SqlCommand("select * from UserInformation where UserName =@username and Password=@password", con)
cmd.Parameters.AddWithValue("@username", txtUserName.Text)
cmd.Parameters.AddWithValue("@password", txtPWD.Text)
Dim da As New SqlDataAdapter(cmd)
Dim dt As New DataTable()
da.Fill(dt)
If dt.Rows.Count > 0 Then
Response.Redirect("Details.aspx")
Else
ClientScript.RegisterStartupScript(Page.[GetType](), "validation""<script language='javascript'>alert('Invalid Username and Password')</script>")
End If
End Sub
End Class

Here don’t forgot to set the connection string in web.config file here I am getting database connection from web.config file for that reason you need to set the connectionstring in web.config file like this

<connectionStrings>
<add name="dbconnection" connectionString="Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB"/>
</connectionStrings>
Demo

No comments:

Post a Comment

Post a Comment