Solved

Check if a cookie value exists and then redirecting

Posted on 2010-11-17
2
235 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

MS Outlook is a world-class email client application that is mainly used for e-communication globally.  In this article, we will discuss the basic idea about MS Outlook, its advanced features, and types of MS Outlook File formats.
Read this checklist to learn more about the 15 things you should never include in an email signature.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

912 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

21 Experts available now in Live!

Get 1:1 Help Now