500pts: Context.items not working, can you help?

Posted on 2006-05-29
Last Modified: 2007-12-19
Hi there,

I wonder if anyone can help. I have just migrated an application from visual studio 2003 to visual studio 2005... It all went well... apart from one thing really...

Each page uses a method to transfer information to a web page that is used for errors i.e.

                        Context.Items[ "error_code" ] = iErrorCode.ToString();
                        Context.Items[ "error_description" ] = ex.Message;
                        Context.Items[ "error_page" ] = this.GetType().Name;
                        Context.Items[ "iduser" ] = "test"

                        Server.Transfer( "error.aspx" );

It appears to function correctly at this point, the user gets transfered to the error page... but this is where it doesn't work... In code... it basically does the following..

            string      strErrorCode            = Context.Items[ "error_code" ].ToString();
But it appears to be erroring on the first line i.e. the line above... gives the following error

{"Object reference not set to an instance of an object."} "NullReferenceException" was  unhandled by user code..

Strange... also the count of Context.Items is 0... i checked it with Intellisense and in the command window...

>? Context.Items.Count

Can anyone help?

Thanks in advance


Question by:ianinspain
    LVL 27

    Accepted Solution

    change server.transfer line to Server.Transfer( "error.aspx" ,true);
    it is false by default

    LVL 27

    Expert Comment

    one more thing, in the recieving page you have to cast the viewstate item to a string

    string strErrorCode  = ((string)(Context.Items("error_code" )));

    if you do a response.write it will output whatever in the error_code from the context item


    Author Comment

    Hi there, thanks... but it didn't help.... although i can understand what you are saying here..

    I was watching  my Auto watches... and i have all my context.items there... in fact the complete COUNT is 6
    and as soon as it does a       Server.Transfer( "error.aspx", true );

    I lose all my context.items ... in fact nothing is preserved....

    I was debugging and watched it... fill the context.items and then transfer to a new page ... so i know it went to a new page.. (error.aspx)

    Very strange......

    Any ideas?

    I presume in Vs 2005 something has changed......


    Author Comment

    Hi sammy,

    Just saw your other msg... Yes that is currently what happens on the error page... actually its done slightly different... But as i say the context.items seem to clear when it transfers...

    This is what is done in the error.aspx but it errros..... but due to the fact that context.items is empty and doesn't exist

    string      strErrorCode            = Context.Items[ "error_code" ].ToString();
                      string      strErrorDesc            = Context.Items[ "error_description" ].ToString();
                      string      strErrorPage            = Context.Items[ "error_page" ].ToString();
                      string      strIdUser                  = Context.Items[ "iduser" ].ToString();
    LVL 10

    Expert Comment

    Try Like This



    Author Comment

    Yes ... on the errro page .... the context items are lost...

    if i do the above.. on the error i get this

    >? HttpContext.Current.Items
    Count = 0

    This is really strange......

    Author Comment

    Wow! i found out the problem ... But im a little confused..

    Basically Page_Load was loading twice.... and on the second load it would blank out the context.items

    I have to set the AutoEventFireup to false... as it was set to true

    So it now works BUT when i create a new page ... it sets the  AutoEventFireup to true and hence i can execute the page_load .. no probs..

    Set it to false stops my NEW page from executing page_load ...

    What i can't understand is why settting autoeventfireup to false in my errror.aspx page doesn't stop it from launching page_load...

    Do you know why?

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
    Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now