Solved

Check if a cookie value exists and then redirecting

Posted on 2010-11-17
2
231 Views
Last Modified: 2012-05-10
Hi - I need a bit of help with cookies.  I created a cookie called UserCookie that hold two values; a UserID and a Name.  

I want to check if one of the two values exists before a specific page loads.  If it doesn't exist I want to redirect them to the login page.  I wrote something like this:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Request.Cookies("UserCookie")("UserID") Is Nothing Then
            Response.Redirect("Default.aspx")
        End If
End Sub

However, when my page loads I get the following error:

Object reference not set to an instance of an object.

How can I fix this problem?
0
Comment
Question by:cdemott33
2 Comments
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 500 total points
ID: 34158352
You need to first test whether the cookie exists at all

If Request.Cookies("UserCookie") is nothing then
.. redirect
ElseIf Request.Cookies("UserCookie")("UserID")....
0
 

Author Closing Comment

by:cdemott33
ID: 34158518
Perfect... Thank you.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Resolve DNS query failed errors for Exchange
This process describes the steps required to Import and Export data from and to .pst files using Exchange 2010. We can use these steps to export data from a user to a .pst file, import data back to the same or a different user, or even import data t…
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now