I keep getting the following error when i try to log into a page that i have created "Object reference not set to an instance of an object.", this is where i think the problem is but im not sure what to do with it;
<tdclass="form_label">Username :</td>
<td><asp:TextBoxid="txt_Username"runat="server"Width="159px"></asp:TextBox></td>
</tr>
<tr>
<tdclass="form_label">Password:</td>
<td><asp:TextBoxid="txt_Password"runat="server"TextMode="Password"Width="159px"></asp:TextBox></td>
</tr>
<tr>
<td></td>
<td>aaa<%=txt_Username +"\n"%><%=strLoginMsg +"\n"%><inputtype="submit"value="Login"class="button"id="btnLogin"name="Submit1"runat="server"/></td>
</tr>
</table>
and the code behind file is below;
.cs file
publicclass index : System.Web.UI.Page{
protected System.Web.UI.WebControls.TextBox txt_Username;protected System.Web.UI.WebControls.TextBox txt_Password;
protected System.Web.UI.HtmlControls.HtmlInputButton btnLogin;protectedstring strLoginMsg =@dotnet.itags.org."";privatevoid Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if(Request.QueryString.Count >0){
if(Request.QueryString["log_out"].ToString() =="1"){
strLoginMsg = strLoginMsg +"Log out is successful!";Session.RemoveAll();
Session.Clear();
Session["UserAccess"] =null;}
else
{
strLoginMsg = strLoginMsg +"Your session has expired...";Session["UserAccess"] =null;}
}
else
{
strLoginMsg ="";}
}
#region Web Form Designer generated codeoverrideprotectedvoid 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>
privatevoid InitializeComponent(){
this.btnLogin.ServerClick +=new System.EventHandler(this.btnLogin_ServerClick);this.Load +=new System.EventHandler(this.Page_Load);
}
#endregion
privatevoid CheckLogin(){
clsxxx objxxx =new clsxxx();
try
{
//declare an instance to the web services structure
clsxxx.structDBParameter[] arrParam =new clsxxx.structDBParameter[2];
//setting structures members
arrParam[0] =new clsxxx.structDBParameter();strLoginMsg = strLoginMsg +this.txt_Username.Text.ToString().Trim();
arrParam[0] =new clsxxx.structDBParameter();arrParam[0].strParamName ="@dotnet.itags.org.Username";arrParam[0].DBtypeParamType = SqlDbType.Char;
arrParam[0].intParamSize = 50;
arrParam[0].objParamDirection = ParameterDirection.Input;
arrParam[0].strParamValue =this.txt_Username.Text.ToString().Trim();//Request["txt_Rep_Forename"];
//TimeStamp out param
arrParam[1] =new clsxxx.structDBParameter();arrParam[1].strParamName ="@dotnet.itags.org.Password";
arrParam[1].DBtypeParamType = SqlDbType.Char;
arrParam[1].intParamSize = 50;
arrParam[1].objParamDirection = ParameterDirection.Input;
arrParam[1].strParamValue =this.txt_Password.Text.ToString().Trim();
int intCheckReturn = 0;
DataSet dsetRecordFound = objxxx.GetDataSet(ref intCheckReturn,"xxx_Login",ref arrParam);
if(dsetRecordFound.Tables[0].Rows.Count>0)
{
Session["UserAccess"] = dsetRecordFound.Tables[0].Rows[0]["Access_Right_ID"].ToString().Trim();
Session["UserFullName"]= dsetRecordFound.Tables[0].Rows[0]["User_Forename"].ToString().Trim() +" " + dsetRecordFound.Tables[0].Rows[0]["User_Surname"].ToString().Trim() ;
Session["UserID"] = dsetRecordFound.Tables[0].Rows[0]["User_ID"].ToString().Trim();Session["Username"] = dsetRecordFound.Tables[0].Rows[0]["Username"].ToString().Trim();Session.Timeout = 60;
Response.Redirect("home.aspx",false);
}
else
{
strLoginMsg = strLoginMsg +"Invalid login";
}
}
catch (ThreadAbortException ex){
throw;}
catch (Exception ex1){
Response.Write(ex1.Message);
}}
privatevoid btnLogin_ServerClick(object sender, System.EventArgs e)
{
if(Page.IsValid){
this.CheckLogin();}
}
privatevoid txt_Username_ServerChange(object sender, System.EventArgs e){
}
}
}
Can you post all your source for the .aspx page?
hi thanks for replying, this is the full code;
<%@. Page language="c#" Codebehind="index.aspx.cs" AutoEventWireup="false" Inherits="xxx.index" %>
<!-- #include file='includes/interface/header_simple_html.aspx' -->
<SCRIPT LANGUAGE="JavaScript" type="text/javascript">
<!-- Begin
if (window != top) top.location.href = location.href;
self.moveTo(0,0);
self.resizeTo(screen.availWidth,screen.availHeight);
// End -->
</SCRIPT>
<form name="login" runat="server" action="">
<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td bgcolor="#ffffff" align="right"><img src="http://pics.10026.com/?src=images/logo.gif" width="222" height="102" alt="" border="0"></td>
</tr>
<tr>
<td bgcolor="#f4a430" > </td>
</tr>
<tr>
<td valign="middle" align="center" height="40%" >
<!-- <form name="login_form" method="post" enctype="multipart/form-data" action=""> -->
<input type="hidden" name="browser" />
<input type="hidden" name="action" value="check_login"/>
<input type="hidden" name="num_tries" value="0"/>
<input type="hidden" name="test" />
<table width="500" border="0" align="center" cellpadding="0" cellspacing="5">
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td class="form_label">Username :</td>
<td><asp:TextBox id="txt_Username" runat="server" Width="159px"></asp:TextBox></td>
</tr>
<tr>
<td class="form_label">Password:</td>
<td><asp:TextBox id="txt_Password" runat="server" TextMode="Password" Width="159px"></asp:TextBox></td>
</tr>
<tr>
<td></td>
<td >aaa<%=txt_Username + "\n"%><%=strLoginMsg + "\n"%> <input type="submit" value="Login" class="button" id="btnLogin" name="Submit1" runat="server"/></td>
</tr>
</table>
<!-- </form> -->
</td>
</tr>
<tr>
<td height="60%"> </td>
</tr>
<tr>
<td bgcolor="#f4a430" > </td>
</tr>
</table>
</form>
<!--# include file='includes/interface/footer_simple_html.aspx' -->
Does anyone know where I am going wrong and what i have to do?
Hi im still having this problem, does anyone know where i am going wrong?
Hi,
Based on my understanding, your web application is using .net framework 1.X. When the user want to log in this page, the user get the error message above. If I have misunderstood you, please feel free to let me know.
The error message "Object reference not set to an instance of an object." indicates that we use the null object in the code. To better understand your issue, could you please confirm the following information:
Please make sure that all the objects that we are using are not null before we use them on this page. For example, we should check if "dsetRecordFound" is null before we use it by the following code:Hi thanks for replying, I am using visual studio 2005, with the 2.0 framework. And yes you are right because the error occurs at the log-in page. The error that appears shows up on the webpage itself and doesnt take me to an error page. "Object reference not set to an instance of an object." basically shows up on the log in page of the website. I have checked that the objjects are not null. thank you
Hi,
Thanks for your response.
When the error page appears, could you please post the complete error message here, such as Description, Exception Details, Source Error, Stack Trace etc. For example:
Server Error in '/CSharpASPNET' Application.
Object reference not set to an instance of an object.
Description:An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details:System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 9: protected void Page_Load(object sender, EventArgs e)Line 10: {Line 11: string ss= Request.QueryString["studentIDs"].ToString();Line 12: }Line 13:
Source File:e:\testProject\CSharpASPNET\MultiView\Default3.aspx Line:11
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.] ASP.multiview_default3_aspx.Page_Load(Object sender, EventArgs e) in e:\testProject\CSharpASPNET\MultiView\Default3.aspx:11 System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +31 System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +68 System.Web.UI.Control.OnLoad(EventArgs e) +88 System.Web.UI.Control.LoadRecursive() +74 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3035
We should make sure that the object isn't null when we use it. For example, Seesion, QueryString etc.
I hope this helps.
Thats the problem its not showing the error in that format above, instead it appears on the login page itself, which makes it harder to pinpoint where its coming from.
Just to add, this is a website i'm actually restructuring so that code definitely works, what I have done is copied the original code into a new folder and renamed it, do you think this may be affecting it?,
Hi,
Thanks for your response.
Do you run your web application on IIS? If so, please make sure that the customErrors's mode is On in the web.config.
Was your original application running with .net framework 1.X and now you run it with .net framework 2.0? If so, we need to migrate it to asp.net 2.0 web application. For more information, seehttp://msdn2.microsoft.com/en-us/library/aa479567.aspx.
If we copy the web application to another folder, we need to make sure that the resources are correct, such as the reference, connection string.
I hope this helps.
Hi thanks for your response, i appreciate it. I belkieve you have led me in the right direction because the code i'm working on was built on an older version of visual studio. I am now working towards converting it using the web application tool. Thank you
0 comments:
Post a Comment