Showing posts with label cookie. Show all posts
Showing posts with label cookie. Show all posts

Saturday, March 24, 2012

Trouble with a simple cookie

Hey guys - hoping you can help me out here. I'm using asp.net 2 (VB) and
trying to set and retrieve values from a cookie. I've done this a few times
before w/o a problem but I seem to be overlooking something here. I'm even
following my previous working examples and I can't see the different.
I have a page that will remember the user's settings using a cookie. Ther
Calendar Events, recent orders, etc will be displayed or hidden based upon
the checkboxes they select (but I haven't gotten that far yet). I'm getting
an error at:
Object reference not set to an instance of an object.
strMyEvents = objCookie.Values("MyEvents")
After using the code below, I tried to search the PC for a cookie and
couldn't find it which makes me think it doesn't exist. Yet, when I
response.redirect the value of the cookie just to test, it indeed finds
"True"
Any ideas'
Here's a simplified version of my code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
If Page.IsPostBack = False Then
Dim objCookie As HttpCookie
Dim strMyEvents As String
objCookie = Page.Request.Cookies("Intranet")
If IsNothing(objCookie) = True Then
SetCookie("True")
End If
strMyEvents = objCookie.Values("MyEvents")
lblTest.Text = strMyEvents
End If
End Sub
Protected Sub SetCookie(ByVal strMyEvents As String)
Dim objCookie As HttpCookie
objCookie = New HttpCookie("Intranet")
objCookie.Expires = DateTime.MaxValue
objCookie.Values("MyEvents") = "True"
'Response.Redirect(objCookie.Values("MyEvents"))
End SubThe cookie should show up under the domain name you are hitting in your
temporary internet files. You have to refresh the folder after hitting the
page to see the cookie (a bit strange compared to other folders, but many of
the profile folders are not standard windows folders).
You cannot access the cookies until you redirect as the information is not
sent to the server until it is requesting another page. As it works, you are
definitely getting the cookie through.
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
http://gregorybeamer.spaces.live.com
****************************************
*********
Think outside the box!
****************************************
*********
"Groove" <noway@.noemail.com> wrote in message
news:%23xgCynXDHHA.4396@.TK2MSFTNGP02.phx.gbl...
> Hey guys - hoping you can help me out here. I'm using asp.net 2 (VB) and
> trying to set and retrieve values from a cookie. I've done this a few
> times before w/o a problem but I seem to be overlooking something here.
> I'm even following my previous working examples and I can't see the
> different.
> I have a page that will remember the user's settings using a cookie. Ther
> Calendar Events, recent orders, etc will be displayed or hidden based upon
> the checkboxes they select (but I haven't gotten that far yet). I'm
> getting an error at:
> Object reference not set to an instance of an object.
> strMyEvents = objCookie.Values("MyEvents")
> After using the code below, I tried to search the PC for a cookie and
> couldn't find it which makes me think it doesn't exist. Yet, when I
> response.redirect the value of the cookie just to test, it indeed finds
> "True"
> Any ideas'
> Here's a simplified version of my code:
>
> Protected Sub Page_Load(ByVal sender As Object, ByVal e As
> System.EventArgs)
> If Page.IsPostBack = False Then
> Dim objCookie As HttpCookie
> Dim strMyEvents As String
> objCookie = Page.Request.Cookies("Intranet")
>
> If IsNothing(objCookie) = True Then
> SetCookie("True")
> End If
>
> strMyEvents = objCookie.Values("MyEvents")
> lblTest.Text = strMyEvents
>
> End If
> End Sub
>
>
> Protected Sub SetCookie(ByVal strMyEvents As String)
> Dim objCookie As HttpCookie
> objCookie = New HttpCookie("Intranet")
> objCookie.Expires = DateTime.MaxValue
> objCookie.Values("MyEvents") = "True"
> 'Response.Redirect(objCookie.Values("MyEvents"))
>
> End Sub
>
Thanks for your help. I've triple checked everything...and still, no
cookie. Can you think of anything that might prevent cookies from being
set? I've never had problems with other sites.
WinXP Pro, IE7.
"Cowboy (Gregory A. Beamer)" <NoSpamMgbworld@.comcast.netNoSpamM> wrote in
message news:ufUwJXYDHHA.4404@.TK2MSFTNGP03.phx.gbl...
> The cookie should show up under the domain name you are hitting in your
> temporary internet files. You have to refresh the folder after hitting the
> page to see the cookie (a bit strange compared to other folders, but many
> of the profile folders are not standard windows folders).
> You cannot access the cookies until you redirect as the information is not
> sent to the server until it is requesting another page. As it works, you
> are definitely getting the cookie through.
> --
> Gregory A. Beamer
> MVP; MCP: +I, SE, SD, DBA
> http://gregorybeamer.spaces.live.com
> ****************************************
*********
> Think outside the box!
> ****************************************
*********
> "Groove" <noway@.noemail.com> wrote in message
> news:%23xgCynXDHHA.4396@.TK2MSFTNGP02.phx.gbl...
>
As far as i know there is a limit on the numebr of cookies that can be
set. i thinks its 20. I had this problem a while back. You may need to
do the response.cookies("MainCookie")("Subcookie") to set the values.
I hope this helps
Groove wrote:
> Thanks for your help. I've triple checked everything...and still, no
> cookie. Can you think of anything that might prevent cookies from being
> set? I've never had problems with other sites.
> WinXP Pro, IE7.
>
> --
>
>
>
> "Cowboy (Gregory A. Beamer)" <NoSpamMgbworld@.comcast.netNoSpamM> wrote in
> message news:ufUwJXYDHHA.4404@.TK2MSFTNGP03.phx.gbl...

Trouble with a simple cookie

Hey guys - hoping you can help me out here. I'm using asp.net 2 (VB) and
trying to set and retrieve values from a cookie. I've done this a few times
before w/o a problem but I seem to be overlooking something here. I'm even
following my previous working examples and I can't see the different.

I have a page that will remember the user's settings using a cookie. Ther
Calendar Events, recent orders, etc will be displayed or hidden based upon
the checkboxes they select (but I haven't gotten that far yet). I'm getting
an error at:

Object reference not set to an instance of an object.
strMyEvents = objCookie.Values("MyEvents")

After using the code below, I tried to search the PC for a cookie and
couldn't find it which makes me think it doesn't exist. Yet, when I
response.redirect the value of the cookie just to test, it indeed finds
"True"

Any ideas??

Here's a simplified version of my code:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)

If Page.IsPostBack = False Then

Dim objCookie As HttpCookie
Dim strMyEvents As String

objCookie = Page.Request.Cookies("Intranet")

If IsNothing(objCookie) = True Then
SetCookie("True")
End If

strMyEvents = objCookie.Values("MyEvents")

lblTest.Text = strMyEvents

End If

End Sub

Protected Sub SetCookie(ByVal strMyEvents As String)

Dim objCookie As HttpCookie

objCookie = New HttpCookie("Intranet")
objCookie.Expires = DateTime.MaxValue

objCookie.Values("MyEvents") = "True"

'Response.Redirect(objCookie.Values("MyEvents"))

End SubThe cookie should show up under the domain name you are hitting in your
temporary internet files. You have to refresh the folder after hitting the
page to see the cookie (a bit strange compared to other folders, but many of
the profile folders are not standard windows folders).

You cannot access the cookies until you redirect as the information is not
sent to the server until it is requesting another page. As it works, you are
definitely getting the cookie through.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
http://gregorybeamer.spaces.live.com
*************************************************
Think outside the box!
*************************************************
"Groove" <noway@.noemail.comwrote in message
news:%23xgCynXDHHA.4396@.TK2MSFTNGP02.phx.gbl...

Quote:

Originally Posted by

Hey guys - hoping you can help me out here. I'm using asp.net 2 (VB) and
trying to set and retrieve values from a cookie. I've done this a few
times before w/o a problem but I seem to be overlooking something here.
I'm even following my previous working examples and I can't see the
different.
>
I have a page that will remember the user's settings using a cookie. Ther
Calendar Events, recent orders, etc will be displayed or hidden based upon
the checkboxes they select (but I haven't gotten that far yet). I'm
getting an error at:
>
Object reference not set to an instance of an object.
strMyEvents = objCookie.Values("MyEvents")
>
After using the code below, I tried to search the PC for a cookie and
couldn't find it which makes me think it doesn't exist. Yet, when I
response.redirect the value of the cookie just to test, it indeed finds
"True"
>
Any ideas??
>
Here's a simplified version of my code:
>
>
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs)
>
If Page.IsPostBack = False Then
>
Dim objCookie As HttpCookie
Dim strMyEvents As String
>
objCookie = Page.Request.Cookies("Intranet")
>
>
If IsNothing(objCookie) = True Then
SetCookie("True")
End If
>
>
strMyEvents = objCookie.Values("MyEvents")
>
lblTest.Text = strMyEvents
>
>
End If
>
End Sub
>
>
>
>
>
Protected Sub SetCookie(ByVal strMyEvents As String)
>
Dim objCookie As HttpCookie
>
objCookie = New HttpCookie("Intranet")
objCookie.Expires = DateTime.MaxValue
>
objCookie.Values("MyEvents") = "True"
>
'Response.Redirect(objCookie.Values("MyEvents"))
>
>
End Sub
>


Thanks for your help. I've triple checked everything...and still, no
cookie. Can you think of anything that might prevent cookies from being
set? I've never had problems with other sites.

WinXP Pro, IE7.

--

"Cowboy (Gregory A. Beamer)" <NoSpamMgbworld@.comcast.netNoSpamMwrote in
message news:ufUwJXYDHHA.4404@.TK2MSFTNGP03.phx.gbl...

Quote:

Originally Posted by

The cookie should show up under the domain name you are hitting in your
temporary internet files. You have to refresh the folder after hitting the
page to see the cookie (a bit strange compared to other folders, but many
of the profile folders are not standard windows folders).
>
You cannot access the cookies until you redirect as the information is not
sent to the server until it is requesting another page. As it works, you
are definitely getting the cookie through.
>
--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
http://gregorybeamer.spaces.live.com
>
*************************************************
Think outside the box!
*************************************************
"Groove" <noway@.noemail.comwrote in message
news:%23xgCynXDHHA.4396@.TK2MSFTNGP02.phx.gbl...

Quote:

Originally Posted by

>Hey guys - hoping you can help me out here. I'm using asp.net 2 (VB) and
>trying to set and retrieve values from a cookie. I've done this a few
>times before w/o a problem but I seem to be overlooking something here.
>I'm even following my previous working examples and I can't see the
>different.
>>
>I have a page that will remember the user's settings using a cookie.
>Ther Calendar Events, recent orders, etc will be displayed or hidden
>based upon the checkboxes they select (but I haven't gotten that far
>yet). I'm getting an error at:
>>
>Object reference not set to an instance of an object.
>strMyEvents = objCookie.Values("MyEvents")
>>
>After using the code below, I tried to search the PC for a cookie and
>couldn't find it which makes me think it doesn't exist. Yet, when I
>response.redirect the value of the cookie just to test, it indeed finds
>"True"
>>
>Any ideas??
>>
>Here's a simplified version of my code:
>>
>>
>Protected Sub Page_Load(ByVal sender As Object, ByVal e As
>System.EventArgs)
>>
> If Page.IsPostBack = False Then
>>
> Dim objCookie As HttpCookie
> Dim strMyEvents As String
>>
>objCookie = Page.Request.Cookies("Intranet")
>>
>>
>If IsNothing(objCookie) = True Then
> SetCookie("True")
>End If
>>
>>
>strMyEvents = objCookie.Values("MyEvents")
>>
>lblTest.Text = strMyEvents
>>
>>
>End If
>>
>End Sub
>>
>>
>>
>>
>>
>Protected Sub SetCookie(ByVal strMyEvents As String)
>>
>Dim objCookie As HttpCookie
>>
>objCookie = New HttpCookie("Intranet")
>objCookie.Expires = DateTime.MaxValue
>>
>objCookie.Values("MyEvents") = "True"
>>
>'Response.Redirect(objCookie.Values("MyEvents"))
>>
>>
>End Sub
>>


>
>


As far as i know there is a limit on the numebr of cookies that can be
set. i thinks its 20. I had this problem a while back. You may need to
do the response.cookies("MainCookie")("Subcookie") to set the values.

I hope this helps

Groove wrote:

Quote:

Originally Posted by

Thanks for your help. I've triple checked everything...and still, no
cookie. Can you think of anything that might prevent cookies from being
set? I've never had problems with other sites.
>
WinXP Pro, IE7.
>
>
--
>
>
>
>
>
>
"Cowboy (Gregory A. Beamer)" <NoSpamMgbworld@.comcast.netNoSpamMwrote in
message news:ufUwJXYDHHA.4404@.TK2MSFTNGP03.phx.gbl...

Quote:

Originally Posted by

The cookie should show up under the domain name you are hitting in your
temporary internet files. You have to refresh the folder after hitting the
page to see the cookie (a bit strange compared to other folders, but many
of the profile folders are not standard windows folders).

You cannot access the cookies until you redirect as the information is not
sent to the server until it is requesting another page. As it works, you
are definitely getting the cookie through.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
http://gregorybeamer.spaces.live.com
*************************************************
Think outside the box!
*************************************************
"Groove" <noway@.noemail.comwrote in message
news:%23xgCynXDHHA.4396@.TK2MSFTNGP02.phx.gbl...

Quote:

Originally Posted by

Hey guys - hoping you can help me out here. I'm using asp.net 2 (VB) and
trying to set and retrieve values from a cookie. I've done this a few
times before w/o a problem but I seem to be overlooking something here.
I'm even following my previous working examples and I can't see the
different.
>
I have a page that will remember the user's settings using a cookie.
Ther Calendar Events, recent orders, etc will be displayed or hidden
based upon the checkboxes they select (but I haven't gotten that far
yet). I'm getting an error at:
>
Object reference not set to an instance of an object.
strMyEvents = objCookie.Values("MyEvents")
>
After using the code below, I tried to search the PC for a cookie and
couldn't find it which makes me think it doesn't exist. Yet, when I
response.redirect the value of the cookie just to test, it indeed finds
"True"
>
Any ideas??
>
Here's a simplified version of my code:
>
>
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs)
>
If Page.IsPostBack = False Then
>
Dim objCookie As HttpCookie
Dim strMyEvents As String
>
objCookie = Page.Request.Cookies("Intranet")
>
>
If IsNothing(objCookie) = True Then
SetCookie("True")
End If
>
>
strMyEvents = objCookie.Values("MyEvents")
>
lblTest.Text = strMyEvents
>
>
End If
>
End Sub
>
>
>
>
>
Protected Sub SetCookie(ByVal strMyEvents As String)
>
Dim objCookie As HttpCookie
>
objCookie = New HttpCookie("Intranet")
objCookie.Expires = DateTime.MaxValue
>
objCookie.Values("MyEvents") = "True"
>
'Response.Redirect(objCookie.Values("MyEvents"))
>
>
End Sub
>


Trouble with changing a value in a cookie.

I wanted to change a value in a existing cookie. I read the old value, modify it and to write the new value in the old cookie. Here is the code:

public bool SetCookie(string cookiename, string cookievalue, int iDaysToExpire) //create the cookie
{
try
{
HttpCookie objCookie = new HttpCookie(cookiename);
Response.Cookies.Clear();
Response.Cookies.Add(objCookie);
objCookie.Values.Add(cookiename, cookievalue); // adding the value to the cookie
DateTime dtExpiry = DateTime.Now.AddDays(iDaysToExpire); //expires
Response.Cookies[cookiename].Expires = dtExpiry;
}
catch (Exception e)
{
return false;
}
return true;
}

public string GetCookie(string cookiename)// method which gives the value from the cookie
{
string cookyval = "";
try
{
cookyval = Request.Cookies[cookiename].Value;
}
catch (Exception e)
{
cookyval = "";
}
return cookyval;
}

protected void Page_Load(object sender, EventArgs e)
{

String t, s,t1;
int i = 0;
s = "";
t = "";

t = GetCookie("test");//reading the cookie
//removing the name and getting the value
try
{
lbl1.Text = t; //
t1 = t.Remove(0, 9);
i = Int32.Parse(t1);
i = i + 1; //the value is modified
}
catch (Exception w) { lbl3.Text = "Convert isnt working."; }
s = Convert.ToString(i);

//writing the new value in the cookie
SetCookie("test", s, 1);

//testing to see if the new value is written
t1 = GetCookie("test");
lbl2.Text = s;
lbl3.Text = t1;
}
}What is your question, exactly? This code re-sets the cookie each time it runs. It seems to return exactly what you're setting it to ("test=0"). (Is that the value that you want it to have?)

Tuesday, March 13, 2012

Troubles with cookies

Hi,

I've some troubles using cookies in my ASP.NET application (C#).
I write a cookie in a page which is in the /login subfolder of the web and I
set an appropriate expire date. But if I intend to read that cookie from
other pages which lie directly at the root folder of the web app, I only get
an empty value and the expire date "1/1/1". What is wrong? Can't I read all
cookies of the web no matter where they were set? Can anyone help me?

AzatUsually when you issue a cookie, it's available site-wide. The Path property
indicates what level in the app the cookie belongs to and it defaults to
"/", which means the cookie is available site wide.

I'm wondering if either the client has disabled cookies or if the time on
the client machine is out of synch with the server.

-Brock
DevelopMentor
http://staff.develop.com/ballen

> Hi,
> I've some troubles using cookies in my ASP.NET application (C#).
> I write a cookie in a page which is in the /login subfolder of the web
> and I
> set an appropriate expire date. But if I intend to read that cookie
> from
> other pages which lie directly at the root folder of the web app, I
> only get
> an empty value and the expire date "1/1/1". What is wrong? Can't I
> read all
> cookies of the web no matter where they were set? Can anyone help me?
> Azat