0

I make a code with the session in the payload I test if the error exist :

protected void Page_Load(object sender, EventArgs e)
     {
         if (String.IsNullOrEmpty(Session["id"].ToString())) QueryStringError.SessionNotFound(Response);
         else
        {

I redirect to some page if the session is not exist ... but I got these error after some period of time :

Server Error in '/Redcrescent' 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 11:     protected void Page_Load(object sender, EventArgs e)
    Line 12:     {
    Line 13:         if (String.IsNullOrEmpty(Session["id"].ToString())) QueryStringError.SessionNotFound(Response);
    Line 14:         else
    Line 15:         {

    Source File: c:\Users\Samy\Documents\Visual Studio 2010\WebSites\Redcrescent\User\UserPrivilegeManage.aspx.cs    Line: 13 

the session timeout is finish but why it gave me the error it should redirect not throw an error

in web.config file :

<sessionState cookieless="true"
      regenerateExpiredSessionId="true"
      timeout="525600" mode="InProc" stateNetworkTimeout="525600"
                  />

but still didn't work ... any idea ? how to make session never expired ? and how to solve these error ?

3 Answers3

0

You cannot execute ToString on null but exactly this you're doing if the session value is null here:

if (String.IsNullOrEmpty(Session["id"].ToString())) QueryStringError.SessionNotFound(Response);

You need to check for null separately:

object id = Session["id"];
if(id == null || String.IsNullOrEmpty(id.ToString())
{
    QueryStringError.SessionNotFound(Response);
}
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
0

You should check for session key first like:

if(Session["id"]!= null)

And then call its ToString method. The reason you are getting the exception (NRE) is because the key doesn't exits in session and you are trying to call ToString on it.

Habib
  • 219,104
  • 29
  • 407
  • 436
  • aha I got it ... it's work ... but that's my first problem the second is to make the session never expired ? –  Jan 22 '14 at 15:50
  • @SamySammour, you can't really do that. You can persist using cookies or some other mechanism, but you can't set unlimited time out on the session. It is also not a very good idea, since sessions are maintained per user. – Habib Jan 22 '14 at 15:51
  • @SamySammour, you may also see this http://stackoverflow.com/questions/1431733/keeping-asp-net-session-open-alive – Habib Jan 22 '14 at 15:54
  • its a web application and the session is activated when choose a user to edit and destroy after that ... so the it's not a problem if the session is stay for a days ... and after all the browser should close or you should choose another user . but the time for these session is about 1 min and it's not enough how could I increase it ? –  Jan 22 '14 at 15:58
0

replace

if (String.IsNullOrEmpty(Session["id"].ToString()))

with

if (String.IsNullOrEmpty(Session["id"]))
theLaw
  • 1,261
  • 2
  • 11
  • 23