User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C# section within the Software Development category of DaniWeb, a massive community of 402,623 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,174 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C# advertiser: Programming Forums
Views: 91337 | Replies: 59
Reply
Join Date: Feb 2007
Posts: 6
Reputation: vamshi is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
vamshi's Avatar
vamshi vamshi is offline Offline
Newbie Poster

Re: Simple ASP.Net Login Page using C#

  #51  
Feb 28th, 2007
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
Reply With Quote  
Join Date: Feb 2007
Posts: 6
Reputation: vamshi is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
vamshi's Avatar
vamshi vamshi is offline Offline
Newbie Poster

Re: Simple ASP.Net Login Page using C#

  #52  
Mar 1st, 2007
Hi again,well i figured out my errors,

1.the param values should be same in both asp and in access stored procedure.

2.if((bool)objReader.GetValue(0) == false)
needs to be changed to

if((int)objReader.GetValue(0) < 1)

Thanks again Paladine,GREAT Tutorials,i even went thru Simple Login Page using VB.NET,it was a great help where i learnt about the session and redirecting the user to the specific page...

thanks again.
Reply With Quote  
Join Date: Apr 2007
Posts: 2
Reputation: rahilameen is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
rahilameen rahilameen is offline Offline
Newbie Poster

Re: Simple ASP.Net Login Page using C#

  #53  
May 10th, 2007
hi sir, i am trying to develop a login page using C# but with MSAccess database.what should i do.how should i modify u r code? just by replacing SQL by OleDb or i should change the entire code?
please help me regarding this.
Reply With Quote  
Join Date: Feb 2007
Posts: 6
Reputation: vamshi is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
vamshi's Avatar
vamshi vamshi is offline Offline
Newbie Poster

Re: Simple ASP.Net Login Page using C#

  #54  
May 14th, 2007
hi Rahilameen,the above code wat i hav written executes for MSAccess Database.
Reply With Quote  
Join Date: Jun 2007
Posts: 1
Reputation: SuzanCha is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
SuzanCha SuzanCha is offline Offline
Newbie Poster

Re: Simple ASP.Net Login Page using C#

  #55  
Jun 27th, 2007
How to remove "Invalid Login, please try again!" message in label box after I entered a valid user name and password? Thanks for your assistance in advance...
Reply With Quote  
Join Date: Nov 2007
Posts: 6
Reputation: pcr is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
pcr pcr is offline Offline
Newbie Poster

Hello,i am newbie

  #56  
Nov 20th, 2007
hello, i am newbie .
can anyone please send me the code in c# and stored proc also with explanation.
Reply With Quote  
Join Date: Nov 2007
Posts: 6
Reputation: pcr is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
pcr pcr is offline Offline
Newbie Poster

Re: Simple ASP.Net Login Page using C#

  #57  
Nov 20th, 2007
hi i tried this but i am not getting output,if i give valid usename and password,its giving invalid
Reply With Quote  
Join Date: Dec 2007
Posts: 15
Reputation: bungek84 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
bungek84 bungek84 is offline Offline
Newbie Poster

Help Re: Simple ASP.Net Login Page using C#

  #58  
Dec 18th, 2007
hai everyone..
i'm beginner in c#..i really needed ur help for login page..i already copy the coding before and it successfully run..but rite now, i want 2 specify the page that user will go after they're log in. for example, if accesslevel = 1, it will go to index.aspx.. if accesslevel = 2, it will go to index2.aspx.. i really hope someone can teach me to solve this problem..
i

here i put my code 4 more info...

using System;
using System.Data;
using System.Data.Odbc;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.SessionState;
using System.Web.Security;
using System.Security.Principal;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace test
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

/* references from 1. http://www.programmingtalk.com/archi...hp/t-2066.html
* 2. http://www.daniweb.com/forums/thread24148.html */
protected void cmdSubmit_Click(object sender, EventArgs e)
{

if (Page.IsValid)
{
if (DBConnection(txtUserName.Text, txtPassword.Text))
{
Session["name"] = txtUserName.Text;
FormsAuthentication.RedirectFromLoginPage(txtUserName.Text, true);
Response.Redirect("home.aspx");
}
else
{
lblMessage2.Text = "Account information is incorrect!";
}
}

}

private bool DBConnection(String txtUserName, String txtPassword)
{
bool authenticated;
OdbcParameter mail;
OdbcParameter pswd;
//OdbcParameter accesslevel;
string myscalar;

OdbcConnection myConn = new OdbcConnection(ConfigurationManager.ConnectionStrings["hrTraining"].ConnectionString);
OdbcCommand cmd = new OdbcCommand("SELECT email, password, name, id, userlevel FROM hris WHERE (email = '" + txtUserName + "' AND password = '" + txtPassword + "')", myConn);

mail = cmd.Parameters.Add("?email", OdbcType.Char, 40);
mail.Value = txtUserName;

pswd = cmd.Parameters.Add("?password", OdbcType.VarChar, 40);
pswd.Value = txtPassword;

//accesslevel = cmd.Parameters.Add("?userlevel", OdbcType.Int);

myConn.Open();
//cmd.ExecuteNonQuery();

OdbcDataReader reader = cmd.ExecuteReader();

if (reader.Read())
{
myscalar = cmd.ExecuteScalar();
Session["userID"] = myscalar;
authenticated = true;
}
else
{
authenticated = false;
}

reader.Close();
myConn.Close();
myConn.Dispose();

return authenticated;
}
}
}
Reply With Quote  
Join Date: Aug 2008
Posts: 1
Reputation: azafiris is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
azafiris azafiris is offline Offline
Newbie Poster

Re: Simple ASP.Net Login Page using C#

  #59  
26 Days Ago
Help
I am new to asp .net #c and i am trying to use your simple asp.net login page using C# to learn more about asp .net #c. I have copied to see how it looks and i get the following error:
Parser Error Message: Could not load type 'NorthCSharp.WebForm1'.
What am i doing wrong? Can this code just be copied to show me how it looks?
Also, i understand the connection to database, i want to connect it to a ms Sql database which i have the connection string for but i want to create a sql command to do the username and password search. Can this be done with this code?
Reply With Quote  
Join Date: Aug 2008
Posts: 3
Reputation: csharpdeveloper is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
csharpdeveloper csharpdeveloper is offline Offline
Newbie Poster

Re: Simple ASP.Net Login Page using C#

  #60  
19 Days Ago
how do i implement cancel or exit button here????

i added it n did this in my aspx.cs but doesn' seem to work...
plz. help.. itz urgent

protected void cmdExit_Click(object sender, EventArgs e)
{
this.rvUserValidator.Enabled = false;
this.rvUserValidator.Dispose();

this.rvPasswordValidator.Enabled = false;
this.rvPasswordValidator.Dispose();

this.Validationsummary1.Visible = false;
this.lblMessage.Visible = false;
this.lblMessage.Enabled = false;

this.lblMessage2.Visible = false;
this.lblMessage2.Enabled = false;

}
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C# Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C# Forum

All times are GMT -4. The time now is 1:25 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC