How to pass
parameters to stored procedure in asp.net
OR
How to implement
login screen using asp.net
OR
How to Check
Username and Password Exists in database using asp.net.
In this article I
will explain how to pass parameters to stored procedure in asp.net.
In this scenario I
have created one login screen. In this screen user can enter username, password
and click Login button. Here username and password are the parameters to
Database.
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.
Stored Procedure: Need to write below stored procedure in Database
CREATE PROCEDURE [dbo].[CheckUserCredentials]
@username nvarchar(50),
@password nvarchar(50)
AS
BEGIN
select LoginID from tblLogin where UserName=@username and Password=@password
END
@username nvarchar(50),
@password nvarchar(50)
AS
BEGIN
select LoginID from tblLogin where UserName=@username and Password=@password
END
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> Login with Parameters</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 with Parameters</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 LoginSP :
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("CheckUserCredentials", conCascading);
cmdLogin.CommandType = CommandType.StoredProcedure;
cmdLogin.Parameters.Add("@UserName", txtUserName.Text);
cmdLogin.Parameters.Add("@Password", txtPassword.Text);
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;
}
No comments:
Post a Comment