Help: Deleting individual cookie subkeys

  • Thread starter Thread starter James
  • Start date Start date
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
 
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
 
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
 
Back
Top