Getting lots of "Object reference not set to an instance of an object." errors on Page_Load - asp.net

we are getting a large amount of errors being reported from our website.
in Debug mode we are not seeing any and we cant seem to find a pattern.
about 1 in every 50-100 pageviews experience the error.
Any help would be greatly appreciated! thanks!
Error
Object reference not set to an instance of an object.
StackTrace
at _Default.Page_Load(Object sender, EventArgs e)
at System.Web.UI.Control.OnLoad(EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

Without specifics it may be hard to diagnose. You could try to add more debug info into your release build in your project options for release configuration - this may then give you a line number.
Add some logging into there either through log4net or trace.write to try to get some more info out of this.
Is page_load accessing any httpcontext, cache, session, or application values?

Related

Session Variables Work in Chrome, Firefox but Fail in IE11

We recently upgraded a web server to Win2012 (from 2008) and started using ActiveDirectory. The main web site uses an external order form written in C# (ASP.NET 2.0). Everything was working fine in the old server until we migrated. Now I get errors when trying to read Session variables in IE 11 (everything works with other browsers).
This is the error I get with the line causing it:
Server Error in '/' 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 974: private void WriteTextConfirmation ()
Line 975: {
Line 976: string sFolders_OrderForm = Session ["Folders_OrderForm"].ToString ();
Line 977: string sOrderNumber = Session ["OrderNumber"].ToString ();
Line 978:
Source File: d:\Web Sites\Order Form\SubmitOrder.aspx.cs Line: 976
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
SubmitOrder.WriteTextConfirmation() in d:\Web Sites\Order Form\SubmitOrder.aspx.cs:976
SubmitOrder.Page_Load(Object sender, EventArgs e) in d:\Web Sites\Order Form\SubmitOrder.aspx.cs:1189
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +24
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +41
System.Web.UI.Control.OnLoad(EventArgs e) +131
System.Web.UI.Control.LoadRecursive() +65
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2427
The problem is that the Session variables that are being read are null. They should be set in the Default form and passed for use in the SubmitOrder form. These variables work with FireFox and Chrome but fail in IE. I'm not sure why this would occur.
We had the same problem and setting the session state to cookieless in Web.config fixed our problem.
Since you are saying that it is browser-specific, it would have to be a setting with the browser. Maybe you have cookies turned off?
Update
I did some searching, and it looks like it could still be cookie related.
Session variable empty in iis7 application
Another common cause of this issue is when the server name contains
underscores (e.g. DEV_TEST). Underscores aren't allowed as host names,
and could interfere with the ability to set cookies.
This Server Fault question has a few answers saying the same thing:
https://serverfault.com/questions/178733/iis-7-0-does-not-keep-session-variables
And another SO question saying underscores:: IE8 does not keep Session Variables
More from Google: https://www.google.com/search?q=iis+application+session+variables+IE

ASP.NET Session state lost after exception

I've usually avoided in-proc session state, until this recent project. I think I have a problem where the state is lost after an unhandled exception occurs.
This in itself is odd, since exceptions in the context of a request don't down the worker process. It could be an exception on a background thread or a callback or something but, this is a simple site and there's nothing fancy going on.
The session ID remains the same and Session_Start is not called on the subsequent request (so the WP hasn't recycled).
I have quite a lot of logging enabled, and its plain to see that I get an exception from the data tier, its an HttpException inside a HttpUnhandleException, then when I browse immediately after to the home page, the request logs a warning about the missing state.
Worse. This doesn't happen on my dev box (IIS Express), only in QA.
Edit:
Here's a chunk of log:
2013-06-12 13:44:29,975 [23] FATAL MvcApplication 100.100.100.100 pxtgn GET An unhandled exception occurred in the web application. The full exception will be unrolled below.
2013-06-12 13:44:30,897 [23] FATAL MvcApplication 100.100.100.100 pxtgn GET Exception.Type: HttpUnhandledException
Exception.Source: System.Web
Exception.Message: Exception of type 'System.Web.HttpUnhandledException' was thrown.
Exception.StackTrace: at System.Web.UI.Page.HandleError(Exception e)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
at System.Web.HttpContext.InvokeCancellableCallback(WaitCallback callback, Object state)
at System.Web.UI.Page.LegacyAsyncPageBeginProcessRequest(HttpContext context, AsyncCallback callback, Object extraData)
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
Exception.Type: HttpException
Exception.Source: CompanyNameProductName.MvcCoreApp
Exception.Message: An error was reported by the data layer: 0Image not found1-10.1007_978-1-4471-4294-2_21-16
Exception.StackTrace: at CompanyName.CompanyNameProductName.imagedetail.Page_Load(Object sender, EventArgs e) in c:\DATA\Git\ProductName\CompanyNameProductNameMvc\imagedetail.aspx.cs:line 55
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
2013-06-12 13:45:07,913 [25] DEBUG StandardPage 100.100.100.100 pxtgn GET Initializing page 'default_aspx' for GET with query string:
2013-06-12 13:45:07,913 [25] ERROR StandardPage 100.100.100.100 pxtgn GET No User instance in session state. Check the log for signs that the session was started properly since a User object should aways exist in session state, setup via session start events in ASP.NET.
The last line shows the missing state condition. The pxtgn is the first 5 chars from the session ID. I hit a page for which I know the data layer will barf about the non-existent image ID. Then any page I hit next will cause my check to log the problem with the missing state. The check and state reference is in the code for the master page, so I can hit any page to see the problem. This also means the custom error page doesn't work - which is sort of how I came to find the bug.
I've now added a Session.Abandon() after the check and respond with a 500 to get me out of jail, but its quite an odd problem. I'm new to the codebase, and I think I've checked everywhere for the session being cleared or state being overwritten.
1 Thread is destroyed
2 Asynchronous environment
use httpcontext as a Parameter
not system.web.**.httpcontext

Invalid Cast Exception calling SqlMembershipProvider GetUser

I am implementing authorisation on a web application using the ASP.NET SqlMembershipProvider, but it is randomly throwing an invalid cast exception when calling the GetUser method.
I can log in, and navigate the pages all fine, but than at random points, boom, it craps out, and throws the exception:
Unable to cast object of type 'System.Int32' to type 'System.String'
Looking at the stack trace you can clearly see it is being caused by the Membership Provider:
[InvalidCastException: Unable to cast object of type 'System.Int32' to type 'System.String'.]
System.Data.SqlClient.SqlBuffer.get_String() +5002910
System.Data.SqlClient.SqlDataReader.GetString(Int32 i) +55
System.Web.Security.SqlMembershipProvider.GetUser(String username, Boolean userIsOnline) +1169
System.Web.Security.Membership.GetUser(String username, Boolean userIsOnline) +63
System.Web.Security.Membership.GetUser() +19
_Default.LoadLeadOverview() in \\file1\default.aspx.vb:169
_Default.Page_Load(Object sender, EventArgs e) in \\file1\default.aspx.vb:30
System.Web.UI.Control.OnLoad(EventArgs e) +91
System.Web.UI.Control.LoadRecursive() +74
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207
I have had a look around and found other people with the same issue, but no solution. Any thoughts would be welcome.
Whilst I haven't done anything to specifically resolve this issue, I haven't seen it in a couple of weeks. Looking back, I was testing the site in multiple browsers at the same time, with the same account, logging in and out a lot, and that seemed to be when then problem came up. Now all the CSS and other cross-browser stuff has been settled, I'm not using multi browsers so heavily, and not at the same time and it seems to be fine.
Is it possible some of your users have all digits as their username? It might be that auto type is boxing wrong somewhere along the way.

NullReferenceException in Page_Load

I have developed my project in asp.net 3.5. It works fine in localhost, when i published in the web sometimes its working fine but sometimes i am getting the following error. I think some error while conecting to the server but dont know exactly. Please help me to do it fine.
Server Error in '/' 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:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.] Result.Page_Load(Object sender, EventArgs e) +128 System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14 System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35 System.Web.UI.Control.OnLoad(EventArgs e) +99 System.Web.UI.Control.LoadRecursive() +50 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627
Enable debugging in your web.config file (Debug="True") and turn RemoteErrors="On". This will provide a much more detailed error message so that you can find what is throwing the NullReferenceException. Once solved, remember to reset these configurations.
Check the code inside your Page_Load method, its throwing a null refernce exception
HTTP Request pipeline is giving the issue here not the page.
you need to check and trace HTTPContext here

ASP.NET: "The resource class for this page was not found."

Myself and a colleague have recently upgraded to Windows Vista, and have been using it to develop ASP.NET applications with Visual Studio 2008 without many issues. And up until this morning, I had been managing to use local resources fine too.
However, this morning, my application suddenly stopped working with local resources, in the sense that any controls with a meta:resourcekey attribute suddenly display an empty string. At first, I thought it was something I had done to the application, so I created a brand new web application, added a label to an ASPX page, and generated a local resource from it. But running it has the same issue - the text simply isn't displayed. See attached screenshot for the example, and its output.
However, running it on my colleague's machine (who also has Vista), works without issue.
Incidently, if I try to get the value in the code behind, it throws this exception:
Server Error in '/Resources' Application. The resource class for this page was not found. Please check if the resource file exists and try again. 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.InvalidOperationException: The resource class for this page was not found. Please check if the resource file exists and try again.
Source Error:
Line 12: protected void Page_Load(object sender, EventArgs e) Line 13: { Line 14: object x = HttpContext.GetLocalResourceObject("~/Default.aspx", "lblTestResource1.Text"); Line 15: } Line 16: }
Source File: C:\inetpub\wwwroot.Net2\Standalone\Resources\Resources\Default.aspx.cs Line: 14
Stack Trace:
[InvalidOperationException: The resource class for this page was not found. Please check if the resource file exists and try again.] System.Web.Compilation.LocalResXResourceProvider.CreateResourceManager()
+2468866 System.Web.Compilation.BaseResXResourceProvider.EnsureResourceManager()
+20 System.Web.Compilation.BaseResXResourceProvider.GetObject(String resourceKey, CultureInfo culture) +15 System.Web.Compilation.ResourceExpressionBuilder.GetResourceObject(IResourceProvider resourceProvider, String resourceKey, CultureInfo culture, Type objType, String propName) +21 System.Web.HttpContext.GetLocalResourceObject(String virtualPath, String resourceKey, CultureInfo culture) +38 System.Web.HttpContext.GetLocalResourceObject(String virtualPath, String resourceKey) +6 Resources._Default.Page_Load(Object sender, EventArgs e) in C:\inetpub\wwwroot.Net2\Standalone\Resources\Resources\Default.aspx.cs:14 System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
+14 System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35 System.Web.UI.Control.OnLoad(EventArgs e) +99 System.Web.UI.Control.LoadRecursive()
+50 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627
Version Information: Microsoft .NET Framework Version:2.0.50727.3074; ASP.NET Version:2.0.50727.3074
Anyone have any idea? The weird thing is that using resources was working. I have tried restarting my computer, and clearing my Temporary ASP.NET files - but the issue remains.
Many thanks
Steve
Okay, I did a system restore to a couple of days previously, and it works fine now. Bizarre, as I (knowingly) made no changes to system files or anything.
As an extreme measure you could try a repair installation of the .net framework...
I've never encountered or even heard of this problem before so I'm afraid I don't have a better solution.

Resources