3

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

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Krusty40k
  • 87
  • 8
  • show the code that includes Anzeigeeinstellungen.aspx.cs, line 97 – Igor Apr 09 '13 at 17:31
  • That path is just a artifact of building on your machine and doesn't mean the server is running in from your machine. You just need to look at line 97 in that file to determine what might be wrong. – juharr Apr 09 '13 at 17:33
  • [NullReferenceException Wiki](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net) – Nick Freeman Apr 09 '13 at 17:34
  • @Krusty40k - please, edit your question adding the code to it and show which line is 97 – Igor Apr 09 '13 at 17:37
  • Check server (IIS) settings that Cookies are enabled. Suspect that Cookies is some how null. – paparazzo Apr 09 '13 at 18:06
  • Good idea, but cookies are enabled – Krusty40k Apr 09 '13 at 18:17
  • 3
    Welcome to Stack Overflow! Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Apr 09 '13 at 18:39
  • Look like the cookie itself hasn't been set by anything. Use your browser developer console to see if any cookie header is sent on your first GET request. – Neil Moss Apr 09 '13 at 18:41
  • 3
    If you solved your problem, please post your solution as an answer. – Ryan Gates Jun 17 '13 at 20:21

1 Answers1

2

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 )

Community
  • 1
  • 1
rene
  • 41,474
  • 78
  • 114
  • 152