Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

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


View products that this article applies to.

This article was previously published under Q314461

↑ Back to the top


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.

↑ Back to the top


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.

↑ Back to the top


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
				

↑ Back to the top


Status

This behavior is by design.

↑ Back to the top


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()
    					

↑ Back to the top


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:

↑ Back to the top


Keywords: KB314461, kbstate, kbprb, kberrmsg, kbcookie

↑ Back to the top

Article Info
Article ID : 314461
Revision : 7
Created on : 7/11/2003
Published on : 7/11/2003
Exists online : False
Views : 552