Cookies/Cache/ViewState/Session

A

Alex Nitulescu

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
 
A

Alex Nitulescu

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
 
K

Karl Seguin

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
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top