Krusty40k
Krusty40k

Reputation: 87

NullReferenceException on 2nd Page_Load

At first I'd like to say: Yes I know that there are many Questions that are similar to mine, but not the same.

When I start one of my 12 sites on my developer-machine everything works wonderful, and also on the server 11 of them work without a problem.

When I start the 12th site it first works fine, but when it cause a postback (Button, DropDownList with AutoPostBack, etc... ) I get the following error:

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:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:


[NullReferenceException: Object reference not set to an instance of an object.]
   Infoscreen.Anzeigeeinstellungen.Page_Load(Object sender, EventArgs e) in C:\Users\Krusty\Desktop\Schule\Diplomarbeit\Infoscreen\Infoscreen\Anzeigeeinstellungen.aspx.cs:97
   System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +24
   System.Web.UI.Control.LoadRecursive() +70
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3047

the path (C:\Users\Krusty\Desktop\Schule\Diplomarbeit\Infoscreen\Infoscreen\Anzeigeeinstellungen.aspx.cs) is the one where the file was on my developer-machine. but why?? I never hardcoded any path in my program, and even recreating the site didn't work.

What shall i do? Any tips/hints would be appreciated.

EDIT:

91    if (!Page.IsPostBack)
92    { 
93        Response.Cookies["Infoscreen_Anzeigeeinstellungen_Ausgewählte_Abteilung"].Value =  ausgewählte_Abteilung.ToString(); 
94    }
95    else
96    { 
97        ausgewählte_Abteilung = Request.Cookies["Infoscreen_Anzeigeeinstellungen_Ausgewählte_Abteilung"].Value; 
98    }

EDIT:

Yes, IIS is configured to use Cookies

EDIT:

SOLVED! in VisualStudio2010 Server the char 'ä' works...
in IIS7 it doesn't...
so the cookie never gets set propperly and the get request hangs up

named the cookie "Infoscreen_Anzeigeeinstellungen_Ausgewaehlte_Abteilung" and it works fine now

can be closed

Upvotes: 3

Views: 1271

Answers (1)

rene
rene

Reputation: 42444

As you already found out your self but just for future reference:

In your code for handling the cookie the 'name' is allowed in c# (using a-umlaut) but as per RFC2616 the token for a cookie MUST contain a subset of US-ASCII chars.

if (!Page.IsPostBack)
    { 
        Response.Cookies["Infoscreen_Anzeigeeinstellungen_Ausgewählte_Abteilung"].Value =  ausgewählte_Abteilung.ToString(); 
    }
    else
    { 
        ausgewählte_Abteilung = Request.Cookies["Infoscreen_Anzeigeeinstellungen_Ausgewählte_Abteilung"].Value; 
    }

So a way to have a safe Cookies key in case your cookiekey is generated based on form/controlnames could be:

static string TokenRFC2616(string key)
{
    const string separators = "()|<>@,;:\\\"/[]?={} ";
    var chars = from ch in key.Normalize(NormalizationForm.FormD)
            where CharUnicodeInfo.GetUnicodeCategory(ch) 
                     != UnicodeCategory.NonSpacingMark &&
                  separators.IndexOf(ch)==-1
            select ch;
    return String.Concat(chars);
}

string cookiekey = TokenRFC2616(
       "Infoscreen_Anzeigeeinstellungen_Ausgewählte_Abteilung");
if (!Page.IsPostBack)
{ 
   Response.Cookies[cookieKey].Value =  ausgewählte_Abteilung.ToString(); 
}
else
{ 
    ausgewählte_Abteilung = Request.Cookies[cookieKey].Value; 
}

(in the above sample the cookie name will be Infoscreen_Anzeigeeinstellungen_Ausgewahlte_Abteilung )

Upvotes: 2

Related Questions