Cookies/Cache/ViewState/Session

Discussion in 'ASP .Net' started by Alex Nitulescu, Feb 3, 2005.

  1. Sorry, I'm really confused - I'm only at the middle of my book and already I
    can count 4 methods to store values.

    Yes, I know that cookies can not store much, but what is the difference
    between storing data in the cache object, in a viewstate and in a Session.

    I can say

    Cache("MyData")=objDataSet
    as well as
    Session("MyData")=objDataSet
    or
    ViewState("MyData")=objDataSet

    right ? (actually, I'm not sure about ViewState working with this kind of
    data - I'll have to check it out). But still, is there a rule as to when
    should you use one or the other method of storing values ?

    I understand (imagine) that Cache and session work on the server, whereas
    cookie and viewstate work on the client. Is this right ?

    Please help me make some sens out of this - how could I chose one instead of
    the other ?

    Thank you
    Alex
     
    Alex Nitulescu, Feb 3, 2005
    #1
    1. Advertising

  2. Alex Nitulescu

    Scott Allen Guest

    Threre are a number of factors to consider in choosing where to store
    data - a primary consideration is how long the data has to be around.
    There is a good overview in the following article:
    http://msdn.microsoft.com/msdnmag/issues/03/04/ASPNETUserState/default.aspx

    HTH,

    --
    Scott
    http://www.OdeToCode.com/blogs/scott/

    On Wed, 2 Feb 2005 23:37:35 -0500, "Alex Nitulescu"
    <> wrote:

    >Sorry, I'm really confused - I'm only at the middle of my book and already I
    >can count 4 methods to store values.
    >
    >Yes, I know that cookies can not store much, but what is the difference
    >between storing data in the cache object, in a viewstate and in a Session.
    >
    >I can say
    >
    >Cache("MyData")=objDataSet
    >as well as
    >Session("MyData")=objDataSet
    >or
    >ViewState("MyData")=objDataSet
    >
    >right ? (actually, I'm not sure about ViewState working with this kind of
    >data - I'll have to check it out). But still, is there a rule as to when
    >should you use one or the other method of storing values ?
    >
    >I understand (imagine) that Cache and session work on the server, whereas
    >cookie and viewstate work on the client. Is this right ?
    >
    >Please help me make some sens out of this - how could I chose one instead of
    >the other ?
    >
    >Thank you
    >Alex
    >
     
    Scott Allen, Feb 3, 2005
    #2
    1. Advertising

  3. Thank you, Scott - that is perfect.


    I would have one more question, please, if I may:

    I have the following very simple colde (while learning about cookies and
    session state):

    Private Sub cmdAddCookie_Click(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles cmdAddCookie.Click

    Dim strCookieName As String
    Dim objRandom As New Random()

    strCookieName = "MyCookie" & objRandom.Next(Integer.MaxValue).ToString
    Dim objCookie As New HttpCookie(strCookieName)
    objCookie.Values("Name") = txtUsername.Text
    objCookie.Values("Pass") = txtPassword.Text
    objCookie.Expires = #12/25/2005#
    Response.Cookies.Add(objCookie)
    End Sub

    Private Sub Page_PreRender(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles MyBase.PreRender

    Dim strCookie As String
    Dim strKey As String

    lblResults.Text = ""
    For Each strCookie In Response.Cookies
    lblResults.Text &= "Cookie Name = " &
    Response.Cookies(strCookie).Name
    If Response.Cookies(strCookie).HasKeys Then
    For Each strKey In Response.Cookies(strCookie).Values
    lblResults.Text &= "<li>" & "Key = " & strKey & "/Value = "
    & Response.Cookies(strCookie).Values(strKey)
    Next strKey
    End If
    Next strCookie
    End Sub

    When I run my project, I see only
    Cookie Name = ASP.NET_SessionId

    I enter something in my two textboxes (username and password) and I press
    AddCookie

    Now, after the POST, I see something like:
    Cookie Name = MyCookie1328144184
    a.. Key = Name/Value = User 1
    a.. Key = Pass/Value = NewPass

    I expected to create a new cookie every time I run my project (that's why I
    append a random number, to change the cookie's name). However, each and
    every time I run the project, when I open this form, I see the exact same
    thing - Cookie Name = ASP.NET_SessionId. Am I *NOT* storing these cookies ?

    BTW, where are these cookies stored ? I searched my HD high & low.... no
    luck yet. I have all kind of things in "C:\Documents and
    Settings\Administrator\Local Settings\Temporary Internet Files\", but no
    "MyCookie".

    Thanks a lot, one more time.
    Alex. Nitulescu
     
    Alex Nitulescu, Feb 3, 2005
    #3
  4. Alex Nitulescu

    Karl Seguin Guest

    Alex,
    Your code seems right, not sure why it isn't working. Instead of dumping
    out the cookies in the PreRender, simply turn on tracing via <%@ Page
    trace=true %> whenever I click my button i see a cookie added to the
    cookies section of the trace information. Ur also correct about the path
    they are stored.

    You might wanna change the Expires value to equal something like
    System.DateTime.Now.AddMonths(6) that might do it.

    As for your original question, also take a look at:
    http://openmymind.net/DataStorage/index.html for some (hopefully) helpful
    information :)

    Karl

    --
    MY ASP.Net tutorials
    http://www.openmymind.net/


    "Alex Nitulescu" <> wrote in message
    news:...
    > Thank you, Scott - that is perfect.
    >
    >
    > I would have one more question, please, if I may:
    >
    > I have the following very simple colde (while learning about cookies and
    > session state):
    >
    > Private Sub cmdAddCookie_Click(ByVal sender As System.Object, ByVal e As
    > System.EventArgs) Handles cmdAddCookie.Click
    >
    > Dim strCookieName As String
    > Dim objRandom As New Random()
    >
    > strCookieName = "MyCookie" & objRandom.Next(Integer.MaxValue).ToString
    > Dim objCookie As New HttpCookie(strCookieName)
    > objCookie.Values("Name") = txtUsername.Text
    > objCookie.Values("Pass") = txtPassword.Text
    > objCookie.Expires = #12/25/2005#
    > Response.Cookies.Add(objCookie)
    > End Sub
    >
    > Private Sub Page_PreRender(ByVal sender As Object, ByVal e As
    > System.EventArgs) Handles MyBase.PreRender
    >
    > Dim strCookie As String
    > Dim strKey As String
    >
    > lblResults.Text = ""
    > For Each strCookie In Response.Cookies
    > lblResults.Text &= "Cookie Name = " &
    > Response.Cookies(strCookie).Name
    > If Response.Cookies(strCookie).HasKeys Then
    > For Each strKey In Response.Cookies(strCookie).Values
    > lblResults.Text &= "<li>" & "Key = " & strKey & "/Value =

    "
    > & Response.Cookies(strCookie).Values(strKey)
    > Next strKey
    > End If
    > Next strCookie
    > End Sub
    >
    > When I run my project, I see only
    > Cookie Name = ASP.NET_SessionId
    >
    > I enter something in my two textboxes (username and password) and I press
    > AddCookie
    >
    > Now, after the POST, I see something like:
    > Cookie Name = MyCookie1328144184
    > a.. Key = Name/Value = User 1
    > a.. Key = Pass/Value = NewPass
    >
    > I expected to create a new cookie every time I run my project (that's why

    I
    > append a random number, to change the cookie's name). However, each and
    > every time I run the project, when I open this form, I see the exact same
    > thing - Cookie Name = ASP.NET_SessionId. Am I *NOT* storing these cookies

    ?
    >
    > BTW, where are these cookies stored ? I searched my HD high & low.... no
    > luck yet. I have all kind of things in "C:\Documents and
    > Settings\Administrator\Local Settings\Temporary Internet Files\", but no
    > "MyCookie".
    >
    > Thanks a lot, one more time.
    > Alex. Nitulescu
    >
    >
     
    Karl Seguin, Feb 3, 2005
    #4
  5. Alex Nitulescu

    katalyst

    Joined:
    Jun 2, 2008
    Messages:
    2
    http://preview.tinyurl.com/ViewstatSession

    Read the link in the subject
     
    katalyst, Mar 20, 2010
    #5
    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. Mike
    Replies:
    7
    Views:
    4,196
    Hermit Dave
    Aug 30, 2004
  2. Dragon

    Cache Session and ViewState

    Dragon, Mar 1, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    1,646
    Dragon
    Mar 1, 2006
  3. Andy Fish
    Replies:
    3
    Views:
    6,612
    Fredrik Lindner
    Nov 6, 2003
  4. LeAnne
    Replies:
    2
    Views:
    3,959
    Alvin Bruney [MVP]
    Jul 6, 2006
  5. _Who
    Replies:
    7
    Views:
    2,776
Loading...

Share This Page