My task is to create login control without using login control in tools. I shouldnt use sqldatasource or any other. I should use only data sets, data adapters and data readers etc.
U had created table login with fields username(varcha r(50)), password(varcha r(50)), firstname(varch ar(50)), lastname(varcha r(50)).
U had to display username , first name, last name in default.aspx page after login.
One more condition is that, if user fails login attempt more than 3 times, you hav to disable login button.
I had written code for login.aspx as
N" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
<script runat="server"& gt;
//int i = 1;
protected void LgnBtn_Click(ob ject sender, EventArgs e)
{
//Application["sessioncou nt"] = 0;
SqlConnection con = null;
try
{
con= LoginData.GetCo nnection();
SqlCommand cmd = new SqlCommand("sel ect UserName from Login where UserName = @UserName and Password = @Password", con);
cmd.Parameters. Add("@UserName" , SqlDbType.VarCh ar, 50).Value = UIDText.Text;
cmd.Parameters. Add("@Password" , SqlDbType.VarCh ar, 50).Value = PWDText.Text;
SqlCommand a = new SqlCommand("sel ect FirstName from Login where UserName = @UserName and Password = @Password", con);
a.Parameters.Ad d("@UserName" , SqlDbType.VarCh ar, 50).Value = UIDText.Text;
a.Parameters.Ad d("@Password" , SqlDbType.VarCh ar, 50).Value = PWDText.Text;
SqlCommand b = new SqlCommand("sel ect LastName from Login where UserName = @UserName and Password = @Password", con);
b.Parameters.Ad d("@UserName" , SqlDbType.VarCh ar, 50).Value = UIDText.Text;
b.Parameters.Ad d("@Password" , SqlDbType.VarCh ar, 50).Value = PWDText.Text;
String UserName = (String)cmd.Exe cuteScalar();
String FirstName = (String)a.Execu teScalar();
String LastName = (String)b.Execu teScalar();
//if (!Page.IsPostBa ck)
//int i = 1;
if (UserName == null/*(FirstName == null)||(LastNam e==null)*/ )
{
LoginLbl.Text = "Invalid Login!";
if (((int)Applicat ion["sessioncou nt"]) >=3/*i>=4*/)
{
LgnBtn.Enabled = false;
LoginLbl.Text = "Please Contact Admin for Password Recovery";
//Session.Clear() ;
}
//i++;
Application["sessioncou nt"] = (int)Applicatio n["sessioncou nt"] + 1;
}
else
{
Session.Add("Us erName", UserName/*UIDText.Text*/);
Session.Add("Fi rstName", FirstName);
Session.Add("La stName", LastName);
Response.Redire ct("default.asp x");
}
}
catch (Exception ex)
{
LoginLbl.Text = ex.Message;
}
finally
{
con.Close();
}
}
protected void Page_Load(objec t sender, EventArgs e)
{
//if (!Page.IsPostBa ck)
//{
// Session.Clear() ;
//}
//Application["sessioncou nt"] = (int)Applicatio n["sessioncou nt"] + 1;
//if ((int)Applicati on["sessioncou nt"] > 3)
//{
// LgnBtn.Enabled = false;
// LoginLbl.Text = "Please Contact Admin for Password Recovery";
//}
}
//protected void Page_Unload(obj ect sender, EventArgs e)
//{
// Application["sessioncou nt"] = 0;
//}
protected void Page_Disposed(o bject sender, EventArgs e)
{
//int i = 0;
//Session.Abandon ();
Session.Clear() ;
// Application.Loc k();
// Application["sessioncou nt"] = 0;
// Application.UnL ock();
}
//protected void Page_AbortTrans action(object sender, EventArgs e)
//{
// Application["sessioncou nt"] = 0;
//}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
"Head1" runat="server">
"form1" runat="server">
"text-align: left">
"center" colspan="2"> "font-size: 14pt">Login
"width: 119px"> User ID: "UIDText" runat="server" > "RequiredFieldV alidator1" runat="server" ErrorMessage="P lease Enter LoginID" ControlToValida te="UIDText">
"width: 119px"> Password: "PWDText" runat="server"> "RequiredFieldV alidator2" runat="server" ErrorMessage="P lease Enter Password" ControlToValida te="PWDText">
"server" Text="Login" ID="LgnBtn" OnClick="LgnBtn _Click">
"LoginLbl" runat="server" Text="">
code for default.aspx
"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
<html xmlns="http://www.w3.org/1999/xhtml" >
"Head1" runat="server">
"form1" runat="server">
"width: 191px; height: 336px" valign="top">
"Label4" runat="server" Text="">
"Label1" runat="server" Text="">
"Label2" runat="server" Text="">
"LoginStatu s1" runat="server" OnLoggingOut="L oginStatus1_Log gingOut"> "width: 788px; height: 336px" valign="top">
"Label3" runat="server" Font-Size="X-Large">
Script for default.aspx.cs
using System;
using System.Data;
using System.Configur ation;
using System.Web;
using System.Web.Secu rity;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.W ebControls.WebP arts;
using System.Web.UI.H tmlControls;
using System.Data.Sql Client;
public partial class _Default : System.Web.UI.P age
{
protected void Page_Load(objec t sender, EventArgs e)
{
if (!Page.IsPostBa ck)
{
Label4.Text = Session["UserName"].ToString();
Label1.Text = Session["FirstName"].ToString();
Label2.Text = Session["LastName"].ToString();
Label3.Text = "Welcome " + Session["FirstName"].ToString() + " " + Session["LastName"].ToString();
}
}
protected void LoginStatus1_Lo ggingOut(object sender, LoginCancelEven tArgs e)
{
Session.Abandon ();
Response.Redire ct("login.aspx" );
//FormsAuthentica tion.SignOut();
}
}
Code for global.asax
void Application_Sta rt(object sender, EventArgs e)
{
Application["sessioncou nt"] = 0;
}
void Application_End (object sender, EventArgs e)
{
Application["sessioncou nt"] = 0;// Code that runs on application shutdown
}
void Application_Err or(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
}
void Session_Start(o bject sender, EventArgs e)
{
int v;
Application.Loc k();
v = Convert.ToInt32 (Application["sessioncou nt"]);
v = v + 1;
Application["sessioncou nt"] = v;
Application.UnL ock();
}
void Session_End(obj ect sender, EventArgs e)
{
int v;
Application.Loc k();
Application["sessioncou nt"] = 0;
Application.UnL ock();
// Code that runs when a session ends.
// Note: The Session_End event is raised only when the sessionstate mode
// is set to InProc in the Web.config file. If session mode is set to StateServer
// or SQLServer, the event is not raised.
}
Code for data connection
using System;
using System.Data;
using System.Configur ation;
using System.Web;
using System.Web.Secu rity;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.W ebControls.WebP arts;
using System.Web.UI.H tmlControls;
using System.Data.Sql Client;
/// <summary>
/// Summary description for LoginData
/// </summary>
public class LoginData
{
public static SqlConnection GetConnection()
{
SqlConnection con = new SqlConnection(" Data Source=SAIBABA; Initial Catalog=OLoginC trl;User ID=sa; pwd=sa1");
con.Open();
return con;
}
}
. I had done this using connecting datasource three times and sessions for disabling. Everything is working good. But problem comes at login attempt failed. For first time it is working in better way, but ven i close the application and running again the application, if fail atleast one time the button is disabling the button. Plz solve this problem. Urgent.
and next thing is i had to use datasets, data adapters and data readers etc for getting the user details and pass details of user to default.aspx by not using sessions. So plz solve this problem as soon as possible
Vineeth
U had created table login with fields username(varcha r(50)), password(varcha r(50)), firstname(varch ar(50)), lastname(varcha r(50)).
U had to display username , first name, last name in default.aspx page after login.
One more condition is that, if user fails login attempt more than 3 times, you hav to disable login button.
I had written code for login.aspx as
N" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
<script runat="server"& gt;
//int i = 1;
protected void LgnBtn_Click(ob ject sender, EventArgs e)
{
//Application["sessioncou nt"] = 0;
SqlConnection con = null;
try
{
con= LoginData.GetCo nnection();
SqlCommand cmd = new SqlCommand("sel ect UserName from Login where UserName = @UserName and Password = @Password", con);
cmd.Parameters. Add("@UserName" , SqlDbType.VarCh ar, 50).Value = UIDText.Text;
cmd.Parameters. Add("@Password" , SqlDbType.VarCh ar, 50).Value = PWDText.Text;
SqlCommand a = new SqlCommand("sel ect FirstName from Login where UserName = @UserName and Password = @Password", con);
a.Parameters.Ad d("@UserName" , SqlDbType.VarCh ar, 50).Value = UIDText.Text;
a.Parameters.Ad d("@Password" , SqlDbType.VarCh ar, 50).Value = PWDText.Text;
SqlCommand b = new SqlCommand("sel ect LastName from Login where UserName = @UserName and Password = @Password", con);
b.Parameters.Ad d("@UserName" , SqlDbType.VarCh ar, 50).Value = UIDText.Text;
b.Parameters.Ad d("@Password" , SqlDbType.VarCh ar, 50).Value = PWDText.Text;
String UserName = (String)cmd.Exe cuteScalar();
String FirstName = (String)a.Execu teScalar();
String LastName = (String)b.Execu teScalar();
//if (!Page.IsPostBa ck)
//int i = 1;
if (UserName == null/*(FirstName == null)||(LastNam e==null)*/ )
{
LoginLbl.Text = "Invalid Login!";
if (((int)Applicat ion["sessioncou nt"]) >=3/*i>=4*/)
{
LgnBtn.Enabled = false;
LoginLbl.Text = "Please Contact Admin for Password Recovery";
//Session.Clear() ;
}
//i++;
Application["sessioncou nt"] = (int)Applicatio n["sessioncou nt"] + 1;
}
else
{
Session.Add("Us erName", UserName/*UIDText.Text*/);
Session.Add("Fi rstName", FirstName);
Session.Add("La stName", LastName);
Response.Redire ct("default.asp x");
}
}
catch (Exception ex)
{
LoginLbl.Text = ex.Message;
}
finally
{
con.Close();
}
}
protected void Page_Load(objec t sender, EventArgs e)
{
//if (!Page.IsPostBa ck)
//{
// Session.Clear() ;
//}
//Application["sessioncou nt"] = (int)Applicatio n["sessioncou nt"] + 1;
//if ((int)Applicati on["sessioncou nt"] > 3)
//{
// LgnBtn.Enabled = false;
// LoginLbl.Text = "Please Contact Admin for Password Recovery";
//}
}
//protected void Page_Unload(obj ect sender, EventArgs e)
//{
// Application["sessioncou nt"] = 0;
//}
protected void Page_Disposed(o bject sender, EventArgs e)
{
//int i = 0;
//Session.Abandon ();
Session.Clear() ;
// Application.Loc k();
// Application["sessioncou nt"] = 0;
// Application.UnL ock();
}
//protected void Page_AbortTrans action(object sender, EventArgs e)
//{
// Application["sessioncou nt"] = 0;
//}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
"Head1" runat="server">
"form1" runat="server">
"text-align: left">
"center" colspan="2"> "font-size: 14pt">Login
"width: 119px"> User ID: "UIDText" runat="server" > "RequiredFieldV alidator1" runat="server" ErrorMessage="P lease Enter LoginID" ControlToValida te="UIDText">
"width: 119px"> Password: "PWDText" runat="server"> "RequiredFieldV alidator2" runat="server" ErrorMessage="P lease Enter Password" ControlToValida te="PWDText">
"server" Text="Login" ID="LgnBtn" OnClick="LgnBtn _Click">
"LoginLbl" runat="server" Text="">
code for default.aspx
"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
<html xmlns="http://www.w3.org/1999/xhtml" >
"Head1" runat="server">
"form1" runat="server">
"width: 191px; height: 336px" valign="top">
"Label4" runat="server" Text="">
"Label1" runat="server" Text="">
"Label2" runat="server" Text="">
"LoginStatu s1" runat="server" OnLoggingOut="L oginStatus1_Log gingOut"> "width: 788px; height: 336px" valign="top">
"Label3" runat="server" Font-Size="X-Large">
Script for default.aspx.cs
using System;
using System.Data;
using System.Configur ation;
using System.Web;
using System.Web.Secu rity;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.W ebControls.WebP arts;
using System.Web.UI.H tmlControls;
using System.Data.Sql Client;
public partial class _Default : System.Web.UI.P age
{
protected void Page_Load(objec t sender, EventArgs e)
{
if (!Page.IsPostBa ck)
{
Label4.Text = Session["UserName"].ToString();
Label1.Text = Session["FirstName"].ToString();
Label2.Text = Session["LastName"].ToString();
Label3.Text = "Welcome " + Session["FirstName"].ToString() + " " + Session["LastName"].ToString();
}
}
protected void LoginStatus1_Lo ggingOut(object sender, LoginCancelEven tArgs e)
{
Session.Abandon ();
Response.Redire ct("login.aspx" );
//FormsAuthentica tion.SignOut();
}
}
Code for global.asax
void Application_Sta rt(object sender, EventArgs e)
{
Application["sessioncou nt"] = 0;
}
void Application_End (object sender, EventArgs e)
{
Application["sessioncou nt"] = 0;// Code that runs on application shutdown
}
void Application_Err or(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
}
void Session_Start(o bject sender, EventArgs e)
{
int v;
Application.Loc k();
v = Convert.ToInt32 (Application["sessioncou nt"]);
v = v + 1;
Application["sessioncou nt"] = v;
Application.UnL ock();
}
void Session_End(obj ect sender, EventArgs e)
{
int v;
Application.Loc k();
Application["sessioncou nt"] = 0;
Application.UnL ock();
// Code that runs when a session ends.
// Note: The Session_End event is raised only when the sessionstate mode
// is set to InProc in the Web.config file. If session mode is set to StateServer
// or SQLServer, the event is not raised.
}
Code for data connection
using System;
using System.Data;
using System.Configur ation;
using System.Web;
using System.Web.Secu rity;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.W ebControls.WebP arts;
using System.Web.UI.H tmlControls;
using System.Data.Sql Client;
/// <summary>
/// Summary description for LoginData
/// </summary>
public class LoginData
{
public static SqlConnection GetConnection()
{
SqlConnection con = new SqlConnection(" Data Source=SAIBABA; Initial Catalog=OLoginC trl;User ID=sa; pwd=sa1");
con.Open();
return con;
}
}
. I had done this using connecting datasource three times and sessions for disabling. Everything is working good. But problem comes at login attempt failed. For first time it is working in better way, but ven i close the application and running again the application, if fail atleast one time the button is disabling the button. Plz solve this problem. Urgent.
and next thing is i had to use datasets, data adapters and data readers etc for getting the user details and pass details of user to default.aspx by not using sessions. So plz solve this problem as soon as possible
Vineeth
Comment