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(varchar(50)), password(varchar(50)), firstname(varchar(50)), lastname(varchar(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.dtd">
<script runat="server">
//int i = 1;
protected void LgnBtn_Click(object sender, EventArgs e)
{
//Application["sessioncount"] = 0;
SqlConnection con = null;
try
{
con= LoginData.GetConnection();
SqlCommand cmd = new SqlCommand("select UserName from Login where UserName = @UserName and Password = @Password", con);
cmd.Parameters.Add("@UserName", SqlDbType.VarChar, 50).Value = UIDText.Text;
cmd.Parameters.Add("@Password", SqlDbType.VarChar, 50).Value = PWDText.Text;
SqlCommand a = new SqlCommand("select FirstName from Login where UserName = @UserName and Password = @Password", con);
a.Parameters.Add("@UserName", SqlDbType.VarChar, 50).Value = UIDText.Text;
a.Parameters.Add("@Password", SqlDbType.VarChar, 50).Value = PWDText.Text;
SqlCommand b = new SqlCommand("select LastName from Login where UserName = @UserName and Password = @Password", con);
b.Parameters.Add("@UserName", SqlDbType.VarChar, 50).Value = UIDText.Text;
b.Parameters.Add("@Password", SqlDbType.VarChar, 50).Value = PWDText.Text;
String UserName = (String)cmd.ExecuteScalar();
String FirstName = (String)a.ExecuteScalar();
String LastName = (String)b.ExecuteScalar();
//if (!Page.IsPostBack)
//int i = 1;
if (UserName == null/*(FirstName == null)||(LastName==null)*/ )
{
LoginLbl.Text = "Invalid Login!";
if (((int)Application["sessioncount"]) >=3/*i>=4*/)
{
LgnBtn.Enabled = false;
LoginLbl.Text = "Please Contact Admin for Password Recovery";
//Session.Clear();
}
//i++;
Application["sessioncount"] = (int)Application["sessioncount"] + 1;
}
else
{
Session.Add("UserName", UserName/*UIDText.Text*/);
Session.Add("FirstName", FirstName);
Session.Add("LastName", LastName);
Response.Redirect("default.aspx");
}
}
catch (Exception ex)
{
LoginLbl.Text = ex.Message;
}
finally
{
con.Close();
}
}
protected void Page_Load(object sender, EventArgs e)
{
//if (!Page.IsPostBack)
//{
// Session.Clear();
//}
//Application["sessioncount"] = (int)Application["sessioncount"] + 1;
//if ((int)Application["sessioncount"] > 3)
//{
// LgnBtn.Enabled = false;
// LoginLbl.Text = "Please Contact Admin for Password Recovery";
//}
}
//protected void Page_Unload(object sender, EventArgs e)
//{
// Application["sessioncount"] = 0;
//}
protected void Page_Disposed(object sender, EventArgs e)
{
//int i = 0;
//Session.Abandon();
Session.Clear();
// Application.Lock();
// Application["sessioncount"] = 0;
// Application.UnLock();
}
//protected void Page_AbortTransaction(object sender, EventArgs e)
//{
// Application["sessioncount"] = 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" > "RequiredFieldValidator1" runat="server" ErrorMessage="Please Enter LoginID" ControlToValidate="UIDText">
"width: 119px"> Password: "PWDText" runat="server"> "RequiredFieldValidator2" runat="server" ErrorMessage="Please Enter Password" ControlToValidate="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.dtd">
<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="">
"LoginStatus1" runat="server" OnLoggingOut="LoginStatus1_LoggingOut"> "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.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
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_LoggingOut(object sender, LoginCancelEventArgs e)
{
Session.Abandon();
Response.Redirect("login.aspx");
//FormsAuthentication.SignOut();
}
}
Code for global.asax
void Application_Start(object sender, EventArgs e)
{
Application["sessioncount"] = 0;
}
void Application_End(object sender, EventArgs e)
{
Application["sessioncount"] = 0;// Code that runs on application shutdown
}
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
}
void Session_Start(object sender, EventArgs e)
{
int v;
Application.Lock();
v = Convert.ToInt32(Application["sessioncount"]);
v = v + 1;
Application["sessioncount"] = v;
Application.UnLock();
}
void Session_End(object sender, EventArgs e)
{
int v;
Application.Lock();
Application["sessioncount"] = 0;
Application.UnLock();
// 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.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
/// <summary>
/// Summary description for LoginData
/// </summary>
public class LoginData
{
public static SqlConnection GetConnection()
{
SqlConnection con = new SqlConnection("Data Source=SAIBABA;Initial Catalog=OLoginCtrl;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