Help: Deleting individual cookie subkeys

Discussion in 'ASP .Net' started by James, Jan 19, 2005.

  1. James

    James Guest

    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
     
    James, Jan 19, 2005
    #1
    1. Advertising

  2. Re: Deleting individual cookie subkeys

    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" <> wrote in message
    news:5fjHd.1649$...
    > 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
    >
    >
     
    Stuart A Hill, Jan 19, 2005
    #2
    1. Advertising

  3. James

    James Guest

    Re: Deleting individual cookie subkeys

    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" <> wrote in message
    news:egRBNyh$...
    > 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" <> wrote in message
    > news:5fjHd.1649$...
    > > 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
    > >
    > >

    >
    >
     
    James, Jan 19, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joerg Jooss
    Replies:
    4
    Views:
    774
    macro
    Apr 10, 2006
  2. Prashant
    Replies:
    2
    Views:
    320
    jacob navia
    Jun 30, 2004
  3. Harry Barker
    Replies:
    2
    Views:
    544
    Alf P. Steinbach
    Apr 19, 2006
  4. Floris Bruynooghe

    Ignoring windows registry PythonPath subkeys

    Floris Bruynooghe, Apr 4, 2008, in forum: Python
    Replies:
    1
    Views:
    300
    Thomas Heller
    Apr 4, 2008
  5. crea
    Replies:
    2
    Views:
    441
    Nobody
    Dec 28, 2012
Loading...

Share This Page