Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
|
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category. ** PLEASE BE SPECIFIC WITH YOUR QUESTION ** When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 9th, 2004, 10:46 PM
Authorized User
 
Join Date: Aug 2004
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default Cookie Error when run web page test.aspx C#.net

Hi every body,

My code as following:

private void Page_Load(object sender, System.EventArgs e)
{
                 HttpCookie myCookie = new HttpCookie("city_id");
            myCookie = Request.Cookies["city_id"];
            int city_id = 1;
            if(myCookie != null)
            {
                Session.Add("city_id",myCookie.Value);
                   city_id=int.ParsemyCookie.Value.ToString());
            }
            else
            {
                // Set the cookie value
                myCookie.Value = "1";
                // Add the cookie
                Response.Cookies.Add(myCookie);
                Session.Add("city_id",myCookie.Value);
            }
}

When I build no errors. but I run it occur error as following:

Object reference not set to an instance of an object.
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:


Line 40: {
Line 41: // Set the cookie value
Line 42: myCookie.Value = "1";
Line 43: // Add the cookie
Line 44: Response.Cookies.Add(myCookie);


Source File: e:\ndv4\test.aspx.cs Line: 42

Stack Trace:


[NullReferenceException: Object reference not set to an instance of an object.]

 Why is it occur that? How is fix this error?

Thank you so much,

Best regards.

Minh Phan.


 
Old August 10th, 2004, 01:23 AM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

in your code....
Code:
if(myCookie != null)
            {
            }
            else
            { 
here your cookie is null so Object reference error occurs
                // Set the cookie value
                myCookie.Value = "1";
            }
--------------------------------------------
Mehdi.:)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Run VS.NET 2003 dataset aspx files on VS 2005 cJeffreywang ASP.NET 2.0 Basics 0 October 16th, 2007 07:27 PM
html(aspx) web page sivkum Javascript 1 January 11th, 2007 01:53 AM
Occur error when run aspx file bapechun ASP.NET 1.0 and 1.1 Basics 11 May 19th, 2004 07:24 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.