•
•
•
•
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
![]() |
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
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 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.
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.
•
•
Join Date: Dec 2007
Posts: 15
Reputation:
Rep Power: 1
Solved Threads: 0
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;
}
}
}
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;
}
}
}
•
•
Join Date: Aug 2008
Posts: 1
Reputation:
Rep Power: 0
Solved Threads: 0
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?
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?
•
•
Join Date: Aug 2008
Posts: 3
Reputation:
Rep Power: 0
Solved Threads: 0
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;
}
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;
}
![]() |
•
•
•
•
•
•
•
•
DaniWeb C# Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- Updated : Simple ASP.Net Login Page (ASP.NET)
- Simple ASP.Net Login Page (Using VB.Net) (ASP.NET)
Other Threads in the C# Forum
- Previous Thread: Adding a new row in a Datagrid ???????????
- Next Thread: Adding custom rows at runtime from database


Linear Mode