Help: Deleting individual cookie subkeys

J

James

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
 
S

Stuart A Hill

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
 
J

James

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top