views:

45

answers:

2

hi, here is my sample code. i m checking the code for null user ID, still it is getting executed....!

    if (!IsPostBack == true && Request.Cookies["UserID"] != null)
    {
        string userID = Request.Cookies["UserID"].Value;
        myPageBL.GetFriendRequests(userID); // this can never be null, but is taking null
    }
A: 

Request.Cookies["UserID"] != null should tell you whether the Cookie with name "UserID" exists whereas Request.Cookies["UserID"].Value tells you the actual Value named as "UserID" inside the cookie.

Subhash Dike
+1  A: 
Request.Cookies["UserID"]

Will return a HttpCookie object called "UserID", or null if it doesn't exist.

Request.Cookies["UserID"].Value 

Will return the Value of the HttpCookie.

So, what could be happening is that the Cookie is there, but the value is empty.

But still, i can't see how the Value could be null. string.Emtpy maybe, but not null.

RPM1984
i have used this code in many places, its only in a few places that i does not work. and sorry i could not understand ur question about string.. its a class System String string ... if i m clear here
Sourabh Sharma
Have a look at the links in my question. The default value for the cookies is a null reference. So you may be creating the cookie, but not setting the value, in which case the value will be null.
RPM1984