hi paladine, i'm new to .net and was working on this login page,thanks a lot for ur tutorial...
i'm using MS access data base,database name is bank,i dont have any user id and password set.
</system.web>
<appSettings>
<add key="ConnectionString" value="User Id=;Password=;Data Source=bank;"/>
</appSettings>
in the code behind file
using System;
using System.Configuration;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.Security;
namespace login
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.TextBox txtUserName;
protected System.Web.UI.WebControls.TextBox txtPassword;
protected System.Web.UI.WebControls.RequiredFieldValidator rvUserValidator;
protected System.Web.UI.WebControls.Button cmdSubmit;
protected System.Web.UI.WebControls.ValidationSummary Validationsummary1;
protected System.Web.UI.WebControls.Label lblMessage;
protected System.Web.UI.WebControls.Label lblMessage2;
protected System.Web.UI.WebControls.RequiredFieldValidator rvPasswordValidator;
public System.Data.OleDb.OleDbConnection conn;
public System.Data.OleDb.OleDbCommand dataCmd;
public System.Data.OleDb.OleDbDataReader objReader;
public System.Data.OleDb.OleDbDataReader dataRead;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
conn=new OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\vamshi\\test\\bank.mdb");
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.txtUserName.TextChanged += new System.EventHandler(this.TextBox1_TextChanged);
this.cmdSubmit.Click += new System.EventHandler(this.cmdSubmit_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void TextBox1_TextChanged(object sender, System.EventArgs e)
{
}
private void cmdSubmit_Click(object sender, System.EventArgs e)
{
if (Page.IsValid)
{
if (DBConnection(txtUserName.Text.Trim(), txtPassword.Text.Trim()))
{
FormsAuthentication.RedirectFromLoginPage (txtUserName.Text, false);
}
else
{
lblMessage.Text = "Invalid Login, please try again!";
}
}
}
private bool DBConnection(string txtUser, string txtPass)
{
if ( ConfigurationSettings.AppSettings ["ConnectionString"]== "") throw new Exception ("Connection String not found");
else
{
//SqlConnection myConn = new SqlConnection(ConfigurationSettings.AppSettings["strConn"]);
//SqlCommand myCmd = new SqlCommand("sp_ValidateUser", myConn);
conn=new OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\vamshi\\test\\bank.mdb");
conn.Open();
dataCmd = new OleDbCommand("Query1" ,conn);
dataCmd.CommandType = CommandType.StoredProcedure;
OleDbParameter objParam1;
OleDbParameter objParam2;
//OleDbParameter returnParam;
//OleDbCommand comm = new OleDb.OleDbCommand(sql, conn);
objParam1=dataCmd.Parameters.Add("@userid",OleDbType.VarChar);
objParam2=dataCmd.Parameters.Add("@password",OleDbType.VarChar);
//returnParam=dataCmd.Parameters.Add("@Num_of_User",OleDbType.Integer);
objParam1.Direction = ParameterDirection.Input;
objParam2.Direction = ParameterDirection.Input;
//returnParam.Direction = ParameterDirection.ReturnValue;
//OleDbDataReader reader = dataCmd.ExecuteReader();
//SqlParameter objParam1;
//SqlParameter objParam2;
//SqlParameter returnParam;
//objParam1 = myCmd.Parameters.Add ("@UserName", SqlDbType.VarChar);
//objParam2 = myCmd.Parameters.Add ("@Password", SqlDbType.VarChar);
//returnParam = myCmd.Parameters.Add ("@Num_of_User", SqlDbType.Int);
objParam1.Value = txtUserName.Text;
objParam2.Value = txtPassword.Text;
try
{
if (conn.State.Equals(ConnectionState.Closed))
{
conn.Open();
dataCmd.ExecuteNonQuery();
}
//if ((int)returnParam.Value < 1)
objReader = dataCmd.ExecuteReader(CommandBehavior.CloseConnection);
while(objReader.Read())
{
if((bool)objReader.GetValue(0) == false)
{
lblMessage.Text = "Invalid Login!";
//return false;
}
else
{
conn.Close();
return true;
}
}
}
catch (Exception ex)
{
lblMessage2.Text = ex + "Error Connecting to the database";
}
}
return false;
}
}
}
when i compile i get an error connecting to the database...
can u plz help me with this..
thank u in advance
HI Vamshi
The VS 2005 have a readymade tool in its toolbox called as “Login”.
You just have to Drag & Drop that on your .aspx page
After drag & drop you can change that to your desired design by clicking on Auto Format
Then we need to go to the ‘Properties’ of that Login Control and click on Events.
There we can see a event called Authenticate, Double click on it.
Once that is done. You have to write the C# code in that event
This is the code its work fine try out
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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.Web.Configuration;
using System.Data.SqlClient;
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
try
{
string uname = Login1.UserName.Trim();//Get User Name From the control
string password = Login1.Password.Trim();//Get Password From The Control
bool flag=AuthenticateUser(uname,password);
if (flag == true)
{
e.Authenticated = true;
Login1.DestinationPageUrl = "Employee.aspx";
}
else
{e.Authenticated = false;}
}
catch(Exception)
{e.Authenticated=false; }
}
public bool AuthenticateUser(string uname, string password)
{
bool bflag = false;
string connectionString = WebConfigurationManager.ConnectionStrings["TraveLineConnectionString"].ConnectionString;
string sql = "select * from Employees where EmpID ='" + uname + "' AND Password ='" + password + "'";
DataSet EmpDS = new DataSet();
SqlConnection con;
SqlDataAdapter da;
try
{
con = new SqlConnection(connectionString);
con.Open();
da = new SqlDataAdapter(sql, con);
da.Fill(EmpDS);
con.Close();
}
catch (Exception)
{
EmpDS = null;
}
if (EmpDS != null)
{
if(EmpDS.Tables[0].Rows.Count > 0)
bflag = true;
}
return bflag;
}
hope this is will help u
Its So Simple isn’t it!