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

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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?
Or if you are really set on using Session (I rarely ever use Session), see this related SO post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.