1

I am building an ASP.NET webpage that will allow to register a new Account inside CRM 2011. Everything seems to work, but after around half an hour the code stops working, without anyone touching it. The page shows a

"Server Error in '/' Application. Object reference not set to an instance of an object."

Republishing the code solves it, but what is happening under the hood? The limited time makes me think it might be related to a login issue, but the login is performed when the page is loaded with credentials stored in the Web.config file.

In the ASPX page I have

void Page_Load ( object sender , EventArgs e )
{
    LoadValues ( sender , e );

    if (!IsPostBack)
    {
        Session["PageRefresh"] = System.DateTime.Now.ToString();
    }
}

protected void Page_PreRender(object sender, EventArgs e)
{
    ViewState["PageRefresh"] = Session["PageRefresh"];
    stateView = DateTime.Parse(Session["PageRefresh"].ToString());
}

I am using IsPostBack in two places:

if ((StatVar.firstExec == true || (DateTime.Parse(Session["PageRefresh"].ToString()) != stateView)) && !IsPostBack)
    ContactDropDownList.Items.Add ( new ListItem ( " " , "0" ) );

if ( (StatVar.firstExec == true || (DateTime.Parse(Session["PageRefresh"].ToString()) != stateView)) && !IsPostBack)
    ContactDropDownList.Items.Add ( new ListItem ( value , indString ) );
7
  • 6
    Impossible to tell w/o any code. Make sure you log all exceptions and post the full details here. Commented Aug 26, 2013 at 10:21
  • Probably you store something on the session, which then gets expired? Commented Aug 26, 2013 at 10:21
  • Check the state of the application pool when you get this error. Commented Aug 26, 2013 at 10:22
  • 3
    code code code. And the trace error normally shows also at which line in the code the error appears Commented Aug 26, 2013 at 10:38
  • I added the code. Since I edited the trace I cannot reproduce the error. But I still need to identify it. Commented Aug 26, 2013 at 13:40

2 Answers 2

1

This sounds like it is related to a login time out, the default time out I believe is 20 mins. I know you mention the login being taken care of in the web config and on page load but without seeing code it's impossible to see if you've got that in a !IsPostBack statement or something else that is stopping the login.

With the limited info available and from the description of the error it sounds like the user is logged out and some element (possibly related to being logged in, membership details or something) is being queried and returns null.

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

Comments

0

Your prerender uses session directly without checking if it is null. Sessions are meant to expire automatically after a preset duration and by default the duration is 20 mins. Try the code below

protected void Page_PreRender(object sender, EventArgs e)
{
   if(Session["PageRefresh"] != null)
   {
        ViewState["PageRefresh"] = Session["PageRefresh"];
        stateView = DateTime.Parse(Session["PageRefresh"].ToString());
   }
}

4 Comments

That made the code avoid the above mentioned error. But elements are not being added to the Dropdownlist
Why is it that you are writing code in pre render part? Why not use the page load?
For some reason the C# code was adding elements to the dropdown list every time the validation failed. So I have to ensure it does it only on the first page load.
remove the !IsPostBack part of your Page_Load event

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.