Welcome to Dream.In.Code
Getting Help is Easy!

Join 136,438 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,341 people online right now. Registration is fast and FREE... Join Now!




Cookie error: Object Ref not set to an instance

 
Reply to this topicStart new topic

Cookie error: Object Ref not set to an instance

L2wis
27 Aug, 2008 - 02:35 PM
Post #1

New D.I.C Head
*

Joined: 7 Mar, 2007
Posts: 4


My Contributions
This one is really driving me up the bend! I've found lots of information on the net about this error and it seems a very common one. I found code example:
CODE

If Not (Request.Cookies("userInfo")("userName") Is Nothing) Then
      MsgBox(Request.Cookies("userInfo")("userName"))
End If


which is ment to be the correct way to check if there is a cookie before you mess around with the contents.....

HOWEVER!!!

I get the dreaded object reference error if there is no cookie what so ever! on the line:
CODE

If Not (Request.Cookies("userInfo")("userName") Is Nothing) Then


I know this is happening because if i create the cookie i recieve no error at all...
please help me this is so driving me up the wall!
User is offlineProfile CardPM
+Quote Post

Footsie
RE: Cookie Error: Object Ref Not Set To An Instance
28 Aug, 2008 - 12:25 AM
Post #2

D.I.C Regular
Group Icon

Joined: 20 Sep, 2007
Posts: 305



Thanked: 4 times
Dream Kudos: 50
My Contributions
You are getting that error because the cookie doesn't exist (as you pointed out yourself) and you are trying to retrieve it.

Usually when you test to see if there is a cookie you need to add the cookie if it is not found, or you can display an error message.

Your logic would then go:

1] Does the cookie exist?
2] If yes, retrieve the value..
3] If no, display an error message.. or create the cookie, then retrieve it and do something..

You are just missing the third step.

This post has been edited by Footsie: 28 Aug, 2008 - 12:29 AM
User is online!Profile CardPM
+Quote Post

eclipsed4utoo
RE: Cookie Error: Object Ref Not Set To An Instance
28 Aug, 2008 - 06:14 AM
Post #3

D.I.C Regular
Group Icon

Joined: 21 Mar, 2008
Posts: 363



Thanked: 19 times
Dream Kudos: 25
My Contributions
I use the following code to check to see for a cookie is null. it's C#.

c#

if (Server.HtmlEncode(Request.Cookies["Settings"]["ProductSelected"]) != null)
{
productSelected = Server.HtmlEncode(Request.Cookies["Settings"]["ProductSelected"]).ToString();
}


my only difference is that I use "Server.HtmlEncode".

This post has been edited by eclipsed4utoo: 28 Aug, 2008 - 06:19 AM
User is online!Profile CardPM
+Quote Post

L2wis
RE: Cookie Error: Object Ref Not Set To An Instance
31 Aug, 2008 - 10:19 AM
Post #4

New D.I.C Head
*

Joined: 7 Mar, 2007
Posts: 4


My Contributions
QUOTE(eclipsed4utoo @ 28 Aug, 2008 - 07:14 AM) *

I use the following code to check to see for a cookie is null. it's C#.

c#

if (Server.HtmlEncode(Request.Cookies["Settings"]["ProductSelected"]) != null)
{
productSelected = Server.HtmlEncode(Request.Cookies["Settings"]["ProductSelected"]).ToString();
}


my only difference is that I use "Server.HtmlEncode".


Hey guys! I'm really sorry i've not responded to this quicker... i suscribed to the thread with auto email notification but not recieved any emails so assumed no one had replied!

my code is unfortuantly in VB sad.gif But i'll try and do something simular to the example above and i'll post back if i find anything... I think i got all confused because the code i was using was example code of how to check for cookie presence... (which is wrong lol)

I'll post back if im still banging my head against a wall. cheers =D

EDIT*

I found what was going wrong!!! =(

The following code works:
CODE

If Not Request.Cookies("userInfo") Is Nothing Then

but this code is bad:
CODE

If Not Request.Cookies("userInfo")("username") Is Nothing Then


This post has been edited by L2wis: 31 Aug, 2008 - 10:32 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 02:06PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month