Introduction: In this
article I will explain with example how to
create a Log In/ Sign In page/form in
asp.net in both
the C# and VB.Net languages and using stored procedure and Sql server database .
In this example when user enter the credentials i.e.
username and password and click on login button then these values will be validated from the Sql server database. If matched then the log in attempt will be
successful otherwise user will get a message e.g. "Wrong
Username/Password"
I have also used the COLLATE Latin1_general_CS_AS to check case sensitive. e.g. Demo and demo will not same in if we use this. It will find exact match.
I have also used the COLLATE Latin1_general_CS_AS to check case sensitive. e.g. Demo and demo will not same in if we use this. It will find exact match.
Implementation: let’s create an asp.net application to
understand the concept.
First of all create a Sql
server database and name it "MyDataBase" and
create a table with the following fields and name it "Login_Tb"
Column
Name
|
Data
Type
|
Id
|
Int( Primary Key. So set Is
Identity=True)
|
UserName
|
varchar(100)
|
Password
|
varchar(100)
|
- Then create a Stored Procedure to check the log in attempt as:
CREATE PROCEDURE Login_Check_Sp
@username varchar(100),
@pwd varchar(100)
AS
BEGIN
select * from Login_Tb
where UserName COLLATE Latin1_general_CS_AS=@username
and [Password] COLLATE Latin1_general_CS_AS=@pwd
END
Create
Connection string in Web.Config file
<connectionStrings>
<add name="Empcon" connectionString="Data
Source=localhost;Initial Catalog=Test;Integrated Security=True"/>
</connectionStrings>
Design
Section of Web-Page:
In the <Form> tag of the design page (.aspx) place two
textbox controls and a Button and a label controls and design the log in page
as:
<div>
<fieldset style="width:380px">
<legend><strong>Login
Page in asp.net</strong></legend>
<table>
<tr>
<td> </td><td>
</td>
</tr>
<tr>
<td>User Name: * </td><td>
<asp:TextBox ID="txtUserName" runat="server" Width="200px"></asp:TextBox><br />
<asp:RequiredFieldValidator
ID="rfvUserName" runat="server" ErrorMessage="Please enter username"
Display="Dynamic" SetFocusOnError="true" ForeColor="Red"
ControlToValidate="txtUserName"></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td>Password: *</td><td>
<asp:TextBox ID="txtPwd" runat="server" TextMode="Password" Width="200px"></asp:TextBox><br />
<asp:RequiredFieldValidator
ID="rfvPwd" runat="server" ErrorMessage="Please enter password"
Display="Dynamic" SetFocusOnError="true" ForeColor="Red"
ControlToValidate="txtPwd"></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td> </td>
<td>
</td>
</tr>
<tr>
<td> </td>
<td>
<asp:Button ID="btnLogin" runat="server" Text="Login"
onclick="btnLogin_Click" /></td>
</tr>
<tr>
<td> </td>
<td>
<asp:Label ID="lblStatus" runat="server" Text=""></asp:Label>
</td>
</tr>
</table>
</fieldset>
</div>
Asp.Net C# code to create Login page/form and check for username and password
In the code behind file (.aspx.cs) write the code on Login Button’s click event as:
Include the following namespaces
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
write the following code:
protected void btnLogin_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
SqlDataAdapter adp = new SqlDataAdapter();
try
{
SqlConnection con = newSqlConnection(ConfigurationManager.ConnectionStrings["EmpCon"].ConnectionString);
SqlCommand cmd = new SqlCommand("Login_Check_Sp", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@username", txtUserName.Text.Trim());
cmd.Parameters.AddWithValue("@pwd", txtPwd.Text.Trim());
adp.SelectCommand = cmd;
adp.Fill(dt);
cmd.Dispose();
if (dt.Rows.Count > 0)
{
lblStatus.Text = "Login Successfull";
//Or in show messagebox using ScriptManager.RegisterStartupScript(this,
this.GetType(), "Message", "alert('Login Successfull');", true);
//Or write using Response.Write("Login Successfull");
//Or redirect using
Response.Redirect("Mypanel.aspx");
}
else
{
lblStatus.Text = "Wrong
Username/Password";
//Or show in
messagebox usingScriptManager.RegisterStartupScript(this, this.GetType(),
"Message", "alert('Wrong Username/Password');", true);
//Or write using
Response.Write("Wrong Username/Password");
}
}
catch (Exception ex)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('Oops!! following error occured : " + ex.Message.ToString() + "');", true);
// Response.Write("Oops!!
following error occured: "
+ex.Message.ToString());
}
finally
{
dt.Clear();
dt.Dispose();
adp.Dispose();
}
}
Asp.Net VB code to create Login page/form and check for username and password
Design the page as shown above in source code section but replace the line
<asp:Button ID="btnLogin" runat="server" Text="Login"
onclick="btnLogin_Click" />
with
<asp:Button ID="btnLogin" runat="server" Text="Login" />
In the code behind file ( .aspx.vb) write the code on Login Button’s click event as:
First
include the following namespaces
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Then
write the code as:
Protected Sub btnLogin_Click(sender As Object, e As System.EventArgs) Handles btnLogin.Click
Dim dt As New DataTable()
Dim adp As New SqlDataAdapter()
Try
Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("EmpCon").ConnectionString)
Dim cmd As New SqlCommand("Login_Check_Sp", con)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@username", txtUserName.Text.Trim())
cmd.Parameters.AddWithValue("@pwd", txtPwd.Text.Trim())
adp.SelectCommand = cmd
adp.Fill(dt)
cmd.Dispose()
If dt.Rows.Count > 0 Then
lblStatus.Text = "Login Successfull"
'Or in show messagebox
using ScriptManager.RegisterStartupScript(this,
this.GetType(), "Message", "alert('Login Successfull');", true);
'Or write using
Response.Write("Login Successfull");
'Or redirect using
Response.Redirect("Mypanel.aspx");
Else
lblStatus.Text = "Wrong
Username/Password"
'Or show in
messagebox usingScriptManager.RegisterStartupScript(this, this.GetType(),
"Message", "alert('Wrong Username/Password');", true);
'Or write using
Response.Write("Wrong Username/Password");
End If
Catch ex As Exception
ScriptManager.RegisterStartupScript(Me, Me.[GetType](), "Message", "alert('Oops!! following error occured : " & ex.Message.ToString() & "');", True)
' Response.Write("Oops!!
following error occured: "
+ex.Message.ToString());
Finally
dt.Clear()
dt.Dispose()
adp.Dispose()
End Try
End Sub
0 comments:
Post a Comment