Cookies in ASP.NET

Oscuridad

New Member
Hi<BR>I am in the process of converting my existing Classic ASP site to ASP.NET (I am a complete newbie), and am having a bit of difficulty with a certain line in my global.asax<BR><BR>I just want to check if one of my Cookie Key exists/contains anything...<BR><BR>In Classic ASP I was using the line...<BR><BR>If Len(Trim(Request.Cookies("GB")("UserID"))) <> 0 Then<BR> ' Do something<BR>End If<BR><BR>However, when I go to launch my site, I get the following error against the above line:<BR><BR>Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.<BR><BR>Can anyone please tell me what I am doing wrong?<BR><BR>Thanks<BR><BR>Starnzy<BR><BR><BR>Starnzy,<BR>I've run into this using session variables as well. Basically, you need to first define a session or cookie variable in the collection before you try to access it. <BR><BR>A good way to do this is to try using "try":<BR>Try<BR>If Len(Trim(Request.Cookies("GB")("UserID"))) <> 0 Then<BR>' Do something<BR>End If<BR>Catch<BR> Response.Cookies("GB")("UserID") = "" <BR>End Try<BR><BR>Hope that helps,<BR>JohnThanks John, I'll give that a try.Try this also...<BR><BR>if isnothing(Request.Cookies("GB")("UserID")) = false then<BR> sCookie = Request.Cookies("GB")("UserID").value<BR>end if
 
Back
Top