Results 1 to 9 of 9

Thread: [RESOLVED] Cookies exists otherwise create it

  1. #1

    Thread Starter
    Fanatic Member aconybeare's Avatar
    Join Date
    Oct 2001
    Location
    UK
    Posts
    772

    Resolved [RESOLVED] Cookies exists otherwise create it

    Hi, I'm have a little trouble with my cookies

    I'm using cookies to hold session data. Mainly because I don't want the session to time out - If there is a way of using session vars without the session timing out then I'd love to hear about it.

    Anyway my cookie may or may not have been set depending on where the user has been on the site. when I try to set the value I'm getting an error Object reference not set. I'm positive it's the cookie because I can see the MS_No in the querystring and the spelling etc. is all okay.

    VB Code:
    1. If Not Page.IsPostBack Then
    2.   ' Get the cookies
    3.   Dim MS_No As HttpCookie = Request.Cookies.Item("MS_No")
    4.  
    5.   ' Check the query string for the ms #
    6.   If Not IsNothing(Request.QueryString("ms_no")) Then
    7.     If Request.QueryString("ms_no").ToString <> String.Empty Then
    8.       [HL="#FFFF00"]MS_No.Value = Request.QueryString("ms_no").ToString ' Set the instance value[/HL]
    9.       Response.Cookies("MS_No").Value = MS_No.Value ' Set the base value
    10.     End If
    11.   End If
    12. End If

    All help will be greatly appreciated

    Cheers Al

  2. #2
    Addicted Member
    Join Date
    Aug 2004
    Location
    Cape Town, South Africa
    Posts
    149

    Re: Cookies exists otherwise create it

    Is it a persistent cookie? Does it actually save to the hdd?
    Last edited by Patch21; Oct 24th, 2005 at 07:46 AM.

  3. #3

    Thread Starter
    Fanatic Member aconybeare's Avatar
    Join Date
    Oct 2001
    Location
    UK
    Posts
    772

    Re: Cookies exists otherwise create it

    Hi Patch,

    Thanks for your reply, will that return the existing cookie if there is one?

  4. #4
    Addicted Member
    Join Date
    Aug 2004
    Location
    Cape Town, South Africa
    Posts
    149

    Re: Cookies exists otherwise create it

    Yeah i think the code i posted wont, think I misunderstood your question. Can you see the cookie on the drive?

  5. #5
    Addicted Member
    Join Date
    Aug 2004
    Location
    Cape Town, South Africa
    Posts
    149

    Re: Cookies exists otherwise create it

    This is the code I use to check if a cookie exists:

    VB Code:
    1. 'This procedure checks if the cookie exists
    2.     Private Function KeyExists(ByRef aKey As String) As Boolean
    3.         Dim Context As HttpContext
    4.         Dim e As System.Collections.IEnumerator
    5.  
    6.         Context = HttpContext.Current
    7.         e = Context.Request.Cookies.GetEnumerator
    8.  
    9.         'Check if the Cookie exists
    10.         Do While e.MoveNext
    11.             If e.Current.Equals(aKey) Then
    12.                 Return True
    13.             End If
    14.         Loop
    15.  
    16.         Return False
    17.     End Function

  6. #6

    Thread Starter
    Fanatic Member aconybeare's Avatar
    Join Date
    Oct 2001
    Location
    UK
    Posts
    772

    Re: Cookies exists otherwise create it

    These cookies are session cookies so they don't actually get written, and by that I mean I mean I'm not setting the "expire" property

  7. #7

    Thread Starter
    Fanatic Member aconybeare's Avatar
    Join Date
    Oct 2001
    Location
    UK
    Posts
    772

    Re: Cookies exists otherwise create it

    How about this -

    VB Code:
    1. If IsNothing(MS_No) Then
    2.   MS_No = New HttpCookie("MS_No", Request.QueryString("ms_no").ToString)
    3. Else
    4.   MS_No.Value = Request.QueryString("ms_no").ToString
    5. End If

  8. #8
    Addicted Member
    Join Date
    Aug 2004
    Location
    Cape Town, South Africa
    Posts
    149

    Re: Cookies exists otherwise create it

    Dont see anything wrong with that...

  9. #9

    Thread Starter
    Fanatic Member aconybeare's Avatar
    Join Date
    Oct 2001
    Location
    UK
    Posts
    772

    Re: Cookies exists otherwise create it

    yar seems to be working, thanks for your help

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width