PRB: A NULL Value Is Returned for Cookies That Do Not Exist (314461)



The information in this article applies to:

  • Microsoft ASP.NET (included with the .NET Framework 1.1)
  • Microsoft ASP.NET (included with the .NET Framework) 1.0

This article was previously published under Q314461

SYMPTOMS

When you reference a cookie that does not exist in ASP.NET code, you may receive an error message that is similar to the following:
System.NullReferenceException: Object reference not set to an instance of an object.

CAUSE

This behavior can occur because a NULL value is received when you request a cookie that does not exist in ASP.NET. This is consistent with how managed collections work in ASP.NET. In ASP Classic, the request object returns an empty string if a requested cookie does not exist.

RESOLUTION

To resolve this behavior, check for NULL values that are associated with cookies, as shown in the following sample codes:

Microsoft Visual C# .NET

	HttpCookie cookie = Request.Cookies["SomeCookie"];

	if(cookie == null)
	{
		//CookieValue represents a WebForm Label control
		CookieValue.Text = "Cookie does not exist!";
	}
	else
	{
		//CookieValue represents a WebForm Label control
		CookieValue.Text = cookie.Value;
	}
				

Microsoft Visual Basic .NET

        Dim cookie As HttpCookie = Request.Cookies("SomeCookie")

        If cookie Is Nothing Then

            'CookieValue represents a WebForm Label control
            CookieValue.Text = "Cookie does not exist!"
        Else

            'CookieValue represents a WebForm Label control
            CookieValue.Text = cookie.Value
        End If
				

STATUS

This behavior is by design.

MORE INFORMATION

Steps to Reproduce the Behavior

  1. Add a new Web Form to your Visual Basic .NET or Visual C# .NET Web application as follows:
    1. Open Visual Basic .NET.
    2. On the File menu, point to New, and then click Project.
    3. In the New Project dialog box, click Visual C# under Project Type, and then click ASP.NET Web Application under Templates.
  2. Add the following code to the Web Form:

    Visual C# .NET
    <%@ Debug=true %>
    <html>
    <script language="C#" runat="server">
         void Page_Load(object sender, System.EventArgs e)
         {
    		txtUserName.Text = Request.Cookies["testing"].Value.ToString();
         }
    </script>
    <body>
    	<form runat=server ID="Form1">
    		<asp:textbox id="txtUserName" runat=Server/>
    	</form>
    </body>
    </html>
    						
    Visual Basic .NET
    <%@ Debug=true %>
    <html>
    <script language="VB" runat="server">
         Sub Page_Load(Sender As Object, E As EventArgs)
    	txtUserName.Text = Request.Cookies("testing").Value.ToString()
         End Sub
    </script>
    <body>
    	<form runat=server>
    		<asp:textbox id="txtUserName" runat=Server/>
    	</form>
    </body>
    </html>
    					
  3. Save and view the .aspx page. The browser displays the error message that is referenced in the "Symptoms" section of this article, and the following line of code as the source of the problem:
    txtUserName.Text = Request.Cookies("testing").Value.ToString()
    					

REFERENCES

For more information about the HTTPCookie class, the HTTPCookieCollection class, and the exhibited behavior for the cookie collection that is described in this article, visit the following Microsoft Web sites:

Modification Type:MinorLast Reviewed:7/11/2003
Keywords:kbCookie kberrmsg kbprb kbState KB314461 kbAudDeveloper