By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,426 Members | 3,152 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,426 IT Pros & Developers. It's quick & easy.

Help: Deleting individual cookie subkeys

P: n/a
Help!

I've made a cookie with a key called 'List' and
subkey 'item3' and assigned it a value of "3"

Response.Cookies("List")("item3")="3"

How do I now delete the subkey "item3" without
deleting any other subkeys that might be in 'List'???
J
Nov 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Hi James,

This is a more complicated problem, deleting an individual subkey. You
cannot simply reset the expiration date for the cookie, because that would
remove the entire cookie instead of a single subkey. The solution instead is
to manipulate the cookie's Values collection, which holds the subkeys. First
recreate the cookie by getting it from the Request.Cookies object. You can
then call the Remove method of the Values collection, passing to the Remove
method the name of the subkey to delete. Then, as usual, you add the
modified cookie to the Response.Cookies collection so it will be sent in its
modified form back to the browser.
The following code shows how to delete a subkey. In the sample, the name of
the subkey to remove is specified in a variable.

Dim subkeyName As String
subkeyName = "userName"
Dim aCookie As HttpCookie = Request.Cookies("userInfo")
aCookie.Values.Remove(subkeyName)
aCookie.Expires = DateTime.Now.AddDays(1)
Response.Cookies.Add(aCookie)I hope this helps,Regards,Stuart
"James" <ja*********@ntlworld.com> wrote in message
news:5f*****************@newsfe3-win.ntli.net...
Help!

I've made a cookie with a key called 'List' and
subkey 'item3' and assigned it a value of "3"

Response.Cookies("List")("item3")="3"

How do I now delete the subkey "item3" without
deleting any other subkeys that might be in 'List'???
J

Nov 19 '05 #2

P: n/a
Hello Stuart,

I found the same info as you on the msdn website.
Needless to say I had difficulty getting the code to
work and hense my original post.

I've spent a day on the code to get cookies to act as
a temporary shopping cart. I'm still working on it - however,
this is donkey-work, I've found the solution(s).

The most challenging problem I've had of late. Soon as
I've finished the assignment this is all for, I will post the
useful stuff to a vbforum on the net to spread a little joy :)

Thanks for your reply,

James
"Stuart A Hill" <xy*@abc.com.bin> wrote in message
news:eg**************@TK2MSFTNGP15.phx.gbl...
Hi James,

This is a more complicated problem, deleting an individual subkey. You
cannot simply reset the expiration date for the cookie, because that would
remove the entire cookie instead of a single subkey. The solution instead is to manipulate the cookie's Values collection, which holds the subkeys. First recreate the cookie by getting it from the Request.Cookies object. You can
then call the Remove method of the Values collection, passing to the Remove method the name of the subkey to delete. Then, as usual, you add the
modified cookie to the Response.Cookies collection so it will be sent in its modified form back to the browser.
The following code shows how to delete a subkey. In the sample, the name of the subkey to remove is specified in a variable.

Dim subkeyName As String
subkeyName = "userName"
Dim aCookie As HttpCookie = Request.Cookies("userInfo")
aCookie.Values.Remove(subkeyName)
aCookie.Expires = DateTime.Now.AddDays(1)
Response.Cookies.Add(aCookie)I hope this helps,Regards,Stuart
"James" <ja*********@ntlworld.com> wrote in message
news:5f*****************@newsfe3-win.ntli.net...
Help!

I've made a cookie with a key called 'List' and
subkey 'item3' and assigned it a value of "3"

Response.Cookies("List")("item3")="3"

How do I now delete the subkey "item3" without
deleting any other subkeys that might be in 'List'???
J


Nov 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.