Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Persistant cookie not persistant 1

Status
Not open for further replies.

dpdoug

Programmer
Joined
Nov 27, 2002
Messages
455
Location
US
I have an UserGroupID number that I need to be able to use throughout the whole session so that I can control the security access in each page the user visits.

I need this UserGroupID to persist so I figure the best way is use it in a cookie that has a long expiration date so that I don't lose it. After a successful login, I set the cookie and the UserGroupID accessed from the cookie and used in the page to set the access levels.

When I leave that page, go to another and then come back, the cookie is accessed again for the UserGroupID. But this time I get an error saying 'Object reference not set to an instance of an object'. In other words, the cookie went away for some reason.

For some reason after the cookie is read for the first time, and then the program tries to read that cookie the next time, it gives me that error.

The way I resolved this issue was that I set the value in a global variable in a general module and it holds the value to throughout the session. I don't think this is the best solution, but I don't want to use a session variable either, since they can time out.

I need to have a reliable way of saving the state between page changes. In my situation, what would be the best way to go about it? Any suggestions?

Thanks in advance.
 
Can you post code on how you set the value of the cookie and how you retrieved it. Unless the computer that the browser was on didn't allow cookies, this method should work.

I had problems with the global variable theory - I believe that it is set for the application so if two people use it, they will both have the same UserGroupID variable.

Session Variables aren't all bad but they do timeout but you can change when they expire and are not saved from visit to visit.

Another option is if you have them login, you can store the UserGroupID in a database and look up the value based on the User Login.

Hope this helps.

Hope everyone is having a great day!

Thanks - Jennifer
 
Here I set the value of the cookie at login:
Code:
Response.Cookies("UserGroupID").Value = u.UserGroupID
Response.Cookies("UserGroupID").Expires = Today.AddDays(2)

When I get to this page I need to retrieve the UserGroupID, but I get this error:

Code:
Object reference not set to an instance of an object.

Line 41:    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
[red]Line 42:       Dim uid As Integer = Request.Cookies("UserGroupID").Value[/red]
Line 43:       acc = sec.GetSecurityAccess(52, uid)
 
Where do you set the cookie?
Marty
 
This is how I set the cookie and haven't had any problems.

It doesn't look like anything is wrong depending on when you set the cookie as cappmgr asked. If you set it at a particular time you could be setting it back to a blank.

I call the following code after the login has been verified during a button click event.

Code:
        Try
            Dim objCookie As HttpCookie = New HttpCookie("CookieName")
            Response.Cookies.Clear()
            Response.Cookies.Add(objCookie)
            objCookie.Values.Add("CookieName", txtUserID.Text)
            Dim dtExpiry As DateTime = DateTime.Now.AddDays(45)
            Response.Cookies("CookieName").Expires = dtExpiry
        Catch ex As Exception
        End Try

Hope everyone is having a great day!

Thanks - Jennifer
 
Is there anything wrong with setting the cookie like this?

Code:
Response.Cookies("UserGroupID").Value = 101
Response.Cookies("UserGroupID").Expires = Today.AddDays(2)

And retreiving it like this?

Code:
Dim ugid as Integer = Request.Cookies("UserGroupID").Value


 
Your code seems fine. The question is where and when are you setting your cookie.
Marty
 
I set the cookie when the user logs in in the index.aspx page. I need the value to persist throughout the session.

I retrieve this value everytime a new page is loaded so that I can pass the value to a function the returns other values that I need to restrict what the logged in user can do on that particular page.

 
Do you set it via a button event? If it is on Page Load or something like that then the cookie could be reset to blank upon reposting.

Hope everyone is having a great day!

Thanks - Jennifer
 
Your code worked here.
Code:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    If Page.IsPostBack Then
        Dim uid As Integer = Request.Cookies("userID").Value
        Label2.Text = uid
    Else
        Label1.Text = "Playing with Cookies"
        Response.Cookies("userID").Value = 101
        Response.Cookies("userID").Expires = Today.AddDays(2)
    End If
End Sub
Is there is a conditional in your code that prevents the cookie from being created. Did you step through it and see the cookie created.
Does your browser accept cookies.

I even set a variable with no value
Dim testID As Integer
and set the cookie to testId and it posted back as 0.

Do you delete the cookie anywhere
Response.Cookies("userID").Expires = Today.AddDays(-2)

I don't know.
Marty
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top