Simple ASP.Net Login Page using C#

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2008
Posts: 1
Reputation: neeo is an unknown quantity at this point 
Solved Threads: 0
neeo neeo is offline Offline
Newbie Poster

Re: Simple ASP.Net Login Page using C#

 
0
  #61
Sep 24th, 2008
Originally Posted by vamshi View Post
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>
<
addkey="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!
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 4
Reputation: rajbav2000 is an unknown quantity at this point 
Solved Threads: 1
rajbav2000 rajbav2000 is offline Offline
Newbie Poster

Re: Simple ASP.Net Login Page using C#

 
0
  #62
Oct 23rd, 2008
i want coding for registeration process in asp.net using c# i want it in c# not html.so please will u able to reply me or u can send it to my mail id rajamani.bonton@gmail.com
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: Simple ASP.Net Login Page using C#

 
0
  #63
Oct 23rd, 2008
The previous poster DID give you code for aspx, Id suggest you quit moaning people arent doing all your work for you, and try doing it yourself.
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 4
Reputation: rajbav2000 is an unknown quantity at this point 
Solved Threads: 1
rajbav2000 rajbav2000 is offline Offline
Newbie Poster

Re: Simple ASP.Net Login Page using C#

 
0
  #64
Oct 23rd, 2008
i have tried my self and got the coding and i want coding uploading files if u have forward to me
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: Simple ASP.Net Login Page using C#

 
0
  #65
Oct 23rd, 2008
You are lazy, and just asking for others to do all your work for you.
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1
Reputation: gmcalab is an unknown quantity at this point 
Solved Threads: 0
gmcalab gmcalab is offline Offline
Newbie Poster

Re: Simple ASP.Net Login Page using C#

 
0
  #66
Feb 11th, 2009
When I try to do a simple logon based on this code... I get this error..
"Validation of viewstate MAC failed. If this application is hosted by a Web Farm or cluster, ensure that <machineKey> configuration specifies the same validationKey and validation algorithm. AutoGenerate cannot be used in a cluster."

When i disable enableViewStateMac = false it works and I dont get the error. What am I missing ?

Thanks MUCH!!!
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 1
Reputation: tami64 is an unknown quantity at this point 
Solved Threads: 0
tami64 tami64 is offline Offline
Newbie Poster

Re: Simple ASP.Net Login Page using C#

 
0
  #67
Aug 30th, 2009
hello
tanx Paladine
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 6
Reputation: Nishanthi is an unknown quantity at this point 
Solved Threads: 0
Nishanthi Nishanthi is offline Offline
Newbie Poster
 
0
  #68
Oct 14th, 2009
what is that sp_ValidateUser you are using can u give me the data related to it please
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC