1

I am running an ASP 4.5 application. On the main page the user writes his name and some other information and then goes to another page, where he should answer few questions and then click the button to finish the test. I send user info from one page to another using Session. When it takes longer than 20 mins for the user to answer the questions I get an exeption

Object reference not set to an instance of an object

[NullReferenceException: Object reference not set to an instance of an object] WebApplication1.Test.Button1_Click(Object sender, EventArgs e) +101 System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +154 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3803

I suppose that the only problem is the timeout, because everything works fine if the user does the test quickly. I've tried to add

<sessionState timeout="300" stateNetworkTimeout="300" mode="InProc" cookieless="false"/> and

<httpRuntime targetFramework="4.5" executionTimeout="300"  />

to web.config, but it didn't help. Could somebody please explain me what's wrong? Thanks

user2080209
  • 749
  • 3
  • 8
  • 25

1 Answers1

1

I suppose you are doing something like Session["username"].ToString() without checking to see if it even exists first. Instead do something like:

string userName = Session["username"] != null ? Session["username"].ToString() : null;

Though you don't need session for this... just post the form values and you don't need to worry about them being lost. It sounds like you are posting back to the original page, writing the posted values to session, then redirecting to Page 2. That's an extra trip... just post to Page 2 or even merge Page 2 with Page 1 and change visibilities on PostBack.

Option 2. When they postback Page 1, instead of writing to Session, write to a cookie.

Option 3: Or if they have created a username it seems more logical to create the user in the database, set an authentication cookie, and then proceed. At that point the user is logged-in. Then they can continue Step 2 of their profile.

MikeSmithDev
  • 15,731
  • 4
  • 58
  • 89
  • Yes, that's exactly what I am doing. But the problem is that I do not want my user to rewrite his username and the other info after he did it once on the main page. Is it possible to prolong the session? – user2080209 Mar 11 '15 at 01:12
  • Or if you are really set on using Session (I rarely ever use Session), see [this related SO post](http://stackoverflow.com/questions/3515947/losing-session-state). – MikeSmithDev Mar 11 '15 at 01:26