G
Guest
hello all,
I am new to C# and ASP, I am trying to build and applicationwith a login in
page. I followed an example from a book to create his page but I am havin
problem when i run the program.
I know there has been a thread like this in this forum but the suggestion I
have tried does not correct this problem. please have a look at the code belo
and help me.need help urgently. you help is much appreciated.
When i run the run I get this error in the browser, Object reference not set
to an instance of an object"
The error is on the line coloured red - dataReader =
oleDbDataAdapter1.SelectCommand.ExecuteReader();
'------code from program---------
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
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;
using System.Data.OleDb;
namespace WebsupportProject
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
/// allow authorised users to login
public class Login : System.Web.UI.Page
{
protected System.Data.OleDb.OleDbDataAdapter oleDbDataAdapter1;
protected System.Data.OleDb.OleDbCommand oleDbSelectCommand1;
protected System.Data.OleDb.OleDbCommand oleDbInsertCommand1;
protected System.Data.OleDb.OleDbCommand oleDbUpdateCommand1;
protected System.Data.OleDb.OleDbCommand oleDbDeleteCommand1;
protected System.Data.OleDb.OleDbConnection oleDbConnection1;
protected System.Data.OleDb.OleDbDataReader dataReader;
//web form labels
protected System.Web.UI.WebControls.Label lbpassword;
protected System.Web.UI.WebControls.Label lbauthorisedusername;
protected System.Web.UI.WebControls.Label lbprompt;
protected System.Web.UI.WebControls.DropDownList authorisedusernameList;
protected System.Web.UI.WebControls.Button submitButton;
protected System.Web.UI.WebControls.RequiredFieldValidator
requiredPasswordValidator;
protected System.Web.UI.WebControls.CustomValidator invalidPasswordValidator;
protected System.Web.UI.WebControls.TextBox passwordTxtBox;
protected System.Web.UI.WebControls.Label promptlabel;
protected System.Web.UI.WebControls.DropDownList authourisedusernameList;
protected System.Web.UI.WebControls.Label passwordLabel;
protected System.Web.UI.WebControls.TextBox passwordTextBox;
//Page Loader
//Reading the list of user names from authoriseduser table
private void Page_Load(object sender, System.EventArgs e)
{
if ( !IsPostBack )
{
//open Database
oleDbConnection1.Open();
//execute query
dataReader = oleDbDataAdapter1.SelectCommand.ExecuteReader(); ' line of error
// add first authorisedusername to the list
while ( dataReader.Read() )
authorisedusernameList.Items.Add( dataReader.GetString( 0 ) );
// close database connection
oleDbConnection1.Close();
}
// Put user code to initialize the page here
} // end of Page_Load
private void invalidPasswordValidator_ServerValidate(object source,
System.Web.UI.WebControls.ServerValidateEventArgs args )
{
//open database connection
oleDbConnection1.Open();
oleDbDataAdapter1.SelectCommand.CommandText = "SELECT * FROM AuthorisedUsers
WHERE LoginName = '" +
Request.Form[ "authorisedusernameList" ].ToString() + "'";
dataReader = oleDbDataAdapter1.SelectCommand.ExecuteReader();
dataReader.Read();
//if password is correct create authentication ticketfor user
if ( args.Value == dataReader.GetString( 1 ) )
{
FormsAuthentication.SetAuthCookie(
Request.Form[ "authorisedusernameList" ], false);
Session.Add( "name", Request.Form[ "authorisedusernameList" ].ToString());
Response.Redirect( "LoggedCall.apsx" );
}
else
args.IsValid=false;
//close database connection
oleDbConnection1.Close();
}// end method invalidPasswordValidator_ServerValidate
#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.oleDbDataAdapter1 = new System.Data.OleDb.OleDbDataAdapter();
this.oleDbSelectCommand1 = new System.Data.OleDb.OleDbCommand();
this.oleDbInsertCommand1 = new System.Data.OleDb.OleDbCommand();
this.oleDbUpdateCommand1 = new System.Data.OleDb.OleDbCommand();
this.oleDbDeleteCommand1 = new System.Data.OleDb.OleDbCommand();
this.oleDbConnection1 = new System.Data.OleDb.OleDbConnection();
//
// oleDbConnection1
//
this.oleDbConnection1.ConnectionString = @"Integrated Security=SSPI;Packet
Size=4096;Data Source=""ERYCKOP-002"";Tag with column collation when
possible=False;Initial Catalog=Project;Use Procedure for Prepare=1;Auto
Translate=True;Persist Security
Info=False;Provider=""SQLOLEDB.1"";Workstation ID=""ERYCKOP-002"";Use
Encryption for Data=False";
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}//end class login
}
----end of code------
any help is much needed.
Thank you
I am new to C# and ASP, I am trying to build and applicationwith a login in
page. I followed an example from a book to create his page but I am havin
problem when i run the program.
I know there has been a thread like this in this forum but the suggestion I
have tried does not correct this problem. please have a look at the code belo
and help me.need help urgently. you help is much appreciated.
When i run the run I get this error in the browser, Object reference not set
to an instance of an object"
The error is on the line coloured red - dataReader =
oleDbDataAdapter1.SelectCommand.ExecuteReader();
'------code from program---------
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
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;
using System.Data.OleDb;
namespace WebsupportProject
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
/// allow authorised users to login
public class Login : System.Web.UI.Page
{
protected System.Data.OleDb.OleDbDataAdapter oleDbDataAdapter1;
protected System.Data.OleDb.OleDbCommand oleDbSelectCommand1;
protected System.Data.OleDb.OleDbCommand oleDbInsertCommand1;
protected System.Data.OleDb.OleDbCommand oleDbUpdateCommand1;
protected System.Data.OleDb.OleDbCommand oleDbDeleteCommand1;
protected System.Data.OleDb.OleDbConnection oleDbConnection1;
protected System.Data.OleDb.OleDbDataReader dataReader;
//web form labels
protected System.Web.UI.WebControls.Label lbpassword;
protected System.Web.UI.WebControls.Label lbauthorisedusername;
protected System.Web.UI.WebControls.Label lbprompt;
protected System.Web.UI.WebControls.DropDownList authorisedusernameList;
protected System.Web.UI.WebControls.Button submitButton;
protected System.Web.UI.WebControls.RequiredFieldValidator
requiredPasswordValidator;
protected System.Web.UI.WebControls.CustomValidator invalidPasswordValidator;
protected System.Web.UI.WebControls.TextBox passwordTxtBox;
protected System.Web.UI.WebControls.Label promptlabel;
protected System.Web.UI.WebControls.DropDownList authourisedusernameList;
protected System.Web.UI.WebControls.Label passwordLabel;
protected System.Web.UI.WebControls.TextBox passwordTextBox;
//Page Loader
//Reading the list of user names from authoriseduser table
private void Page_Load(object sender, System.EventArgs e)
{
if ( !IsPostBack )
{
//open Database
oleDbConnection1.Open();
//execute query
dataReader = oleDbDataAdapter1.SelectCommand.ExecuteReader(); ' line of error
// add first authorisedusername to the list
while ( dataReader.Read() )
authorisedusernameList.Items.Add( dataReader.GetString( 0 ) );
// close database connection
oleDbConnection1.Close();
}
// Put user code to initialize the page here
} // end of Page_Load
private void invalidPasswordValidator_ServerValidate(object source,
System.Web.UI.WebControls.ServerValidateEventArgs args )
{
//open database connection
oleDbConnection1.Open();
oleDbDataAdapter1.SelectCommand.CommandText = "SELECT * FROM AuthorisedUsers
WHERE LoginName = '" +
Request.Form[ "authorisedusernameList" ].ToString() + "'";
dataReader = oleDbDataAdapter1.SelectCommand.ExecuteReader();
dataReader.Read();
//if password is correct create authentication ticketfor user
if ( args.Value == dataReader.GetString( 1 ) )
{
FormsAuthentication.SetAuthCookie(
Request.Form[ "authorisedusernameList" ], false);
Session.Add( "name", Request.Form[ "authorisedusernameList" ].ToString());
Response.Redirect( "LoggedCall.apsx" );
}
else
args.IsValid=false;
//close database connection
oleDbConnection1.Close();
}// end method invalidPasswordValidator_ServerValidate
#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.oleDbDataAdapter1 = new System.Data.OleDb.OleDbDataAdapter();
this.oleDbSelectCommand1 = new System.Data.OleDb.OleDbCommand();
this.oleDbInsertCommand1 = new System.Data.OleDb.OleDbCommand();
this.oleDbUpdateCommand1 = new System.Data.OleDb.OleDbCommand();
this.oleDbDeleteCommand1 = new System.Data.OleDb.OleDbCommand();
this.oleDbConnection1 = new System.Data.OleDb.OleDbConnection();
//
// oleDbConnection1
//
this.oleDbConnection1.ConnectionString = @"Integrated Security=SSPI;Packet
Size=4096;Data Source=""ERYCKOP-002"";Tag with column collation when
possible=False;Initial Catalog=Project;Use Procedure for Prepare=1;Auto
Translate=True;Persist Security
Info=False;Provider=""SQLOLEDB.1"";Workstation ID=""ERYCKOP-002"";Use
Encryption for Data=False";
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}//end class login
}
----end of code------
any help is much needed.
Thank you