|
-
Oct 24th, 2005, 07:26 AM
#1
Thread Starter
Fanatic Member
[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:
If Not Page.IsPostBack Then
' Get the cookies
Dim MS_No As HttpCookie = Request.Cookies.Item("MS_No")
' Check the query string for the ms #
If Not IsNothing(Request.QueryString("ms_no")) Then
If Request.QueryString("ms_no").ToString <> String.Empty Then
[HL="#FFFF00"]MS_No.Value = Request.QueryString("ms_no").ToString ' Set the instance value[/HL]
Response.Cookies("MS_No").Value = MS_No.Value ' Set the base value
End If
End If
End If
All help will be greatly appreciated
Cheers Al
-
Oct 24th, 2005, 07:42 AM
#2
Addicted Member
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.
-
Oct 24th, 2005, 07:45 AM
#3
Thread Starter
Fanatic Member
Re: Cookies exists otherwise create it
Hi Patch,
Thanks for your reply, will that return the existing cookie if there is one?
-
Oct 24th, 2005, 07:47 AM
#4
Addicted Member
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?
-
Oct 24th, 2005, 07:49 AM
#5
Addicted Member
Re: Cookies exists otherwise create it
This is the code I use to check if a cookie exists:
VB Code:
'This procedure checks if the cookie exists
Private Function KeyExists(ByRef aKey As String) As Boolean
Dim Context As HttpContext
Dim e As System.Collections.IEnumerator
Context = HttpContext.Current
e = Context.Request.Cookies.GetEnumerator
'Check if the Cookie exists
Do While e.MoveNext
If e.Current.Equals(aKey) Then
Return True
End If
Loop
Return False
End Function
-
Oct 24th, 2005, 07:50 AM
#6
Thread Starter
Fanatic Member
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
-
Oct 24th, 2005, 07:59 AM
#7
Thread Starter
Fanatic Member
Re: Cookies exists otherwise create it
How about this -
VB Code:
If IsNothing(MS_No) Then
MS_No = New HttpCookie("MS_No", Request.QueryString("ms_no").ToString)
Else
MS_No.Value = Request.QueryString("ms_no").ToString
End If
-
Oct 24th, 2005, 08:01 AM
#8
Addicted Member
Re: Cookies exists otherwise create it
Dont see anything wrong with that...
-
Oct 24th, 2005, 08:11 AM
#9
Thread Starter
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|