|
 |
aspx_beginners thread: my first cookie
Message #1 by "Jordan, Andy" <Andy.Jordan@g...> on Wed, 24 Apr 2002 13:10:18 +0100
|
|
Hi,
Can anyone help?
I'm having trouble with by first attempt at using cookies from 'Beginning
ASP.NET using VB.NET' Chapter 10 'Try it out - using cookies'.
I'm not convinced that my first page (cookie1.aspx is actually saving a
cookie to my hard disk). If I click the link to cookie2.aspx from within
cookie1.aspx after I have pressed the save cookie button then I get the
second page with the background I have selected. However, if I close my
browser down and then try to load coookie2.aspx I get the following error:
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set
to an instance of an object.
Source Error:
Line 21: <html>
Line 22:
Line 23: <body bgcolor="<% = Request.Cookies("Background").Value %>">
Line 24:
Line 25: </body>
Here are the aspx files I used:
Cookie1.aspx
<%@ page language="vb" %>
<script language="vb" runat="server">
Sub Page_Load(Sender As Object, E As EventArgs)
If Not IsPostBack Then
mybutton.Text = "Save Cookie"
mydropdownlist.Items.Add("Blue")
mydropdownlist.Items.Add("Red")
mydropdownlist.Items.Add("Gray")
End If
End Sub
Public Sub Click(ByVal Sender As Object, ByVal E As
System.EventArgs)
Dim MyCookie As New HttpCookie("Background")
MyCookie.Value = mydropdownlist.SelectedItem.Text
Response.Cookies.Add(MyCookie)
End Sub
</script>
<html>
<body>
<form id="CookieForm" method="post" runat="server">
<asp:dropdownlist id="mydropdownlist" runat="server"
/>
<asp:button id="mybutton" runat="server"
onclick="Click" />
<br />
<a href="cookie2.aspx">cookie2.aspx</a>
</form>
</body>
</html>
Cookie2.aspx
<%@ page language="vb" %>
<script language="vb" runat="server">
Sub Page_Load(Sender As Object, E As EventArgs)
Response.Cache.SetExpires(DateTime.Now)
End Sub
</script>
<html>
<body bgcolor="<% = Request.Cookies("Background").Value %>">
</body>
</html>
Thanks,
Andy Jordan
Message #2 by Imar@S... on Wed, 24 Apr 2002 18:59:13
|
|
By default, a cookie will be cleared when the user closes the browser.
You'll need to set the Expires property explicitly, like this:
Dim dt As DateTime = DateTime.Now()
Dim ts As New TimeSpan(0,0,10,0)
Response.Cookies("MyCookie").Expires = dt.Add(ts)
This will set the cookie to expire in 10 minutes. The Expires property
expects a DateTime object, so there are other ways of setting it than
using the TimeSpan.
HtH
Imar
> Hi,
Can anyone help?
I'm having trouble with by first attempt at using cookies from 'Beginning
ASP.NET using VB.NET' Chapter 10 'Try it out - using cookies'.
I'm not convinced that my first page (cookie1.aspx is actually saving a
cookie to my hard disk). If I click the link to cookie2.aspx from within
cookie1.aspx after I have pressed the save cookie button then I get the
second page with the background I have selected. However, if I close my
browser down and then try to load coookie2.aspx I get the following error:
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set
to an instance of an object.
Source Error:
Line 21: <html>
Line 22:
Line 23: <body bgcolor="<% = Request.Cookies("Background").Value %>">
Line 24:
Line 25: </body>
Here are the aspx files I used:
Cookie1.aspx
<%@ page language="vb" %>
<script language="vb" runat="server">
Sub Page_Load(Sender As Object, E As EventArgs)
If Not IsPostBack Then
mybutton.Text = "Save Cookie"
mydropdownlist.Items.Add("Blue")
mydropdownlist.Items.Add("Red")
mydropdownlist.Items.Add("Gray")
End If
End Sub
Public Sub Click(ByVal Sender As Object, ByVal E As
System.EventArgs)
Dim MyCookie As New HttpCookie("Background")
MyCookie.Value = mydropdownlist.SelectedItem.Text
Response.Cookies.Add(MyCookie)
End Sub
</script>
<html>
<body>
<form id="CookieForm" method="post" runat="server">
<asp:dropdownlist id="mydropdownlist"
runat="server"
/>
<asp:button id="mybutton" runat="server"
onclick="Click" />
<br />
<a href="cookie2.aspx">cookie2.aspx</a>
</form>
</body>
</html>
Cookie2.aspx
<%@ page language="vb" %>
<script language="vb" runat="server">
Sub Page_Load(Sender As Object, E As EventArgs)
Response.Cache.SetExpires(DateTime.Now)
End Sub
</script>
<html>
<body bgcolor="<% = Request.Cookies("Background").Value %>">
</body>
</html>
Thanks,
Andy Jordan
Message #3 by "Jordan, Andy" <Andy.Jordan@g...> on Fri, 26 Apr 2002 09:43:54 +0100
|
|
Hi Imar,
I've added your code, but I still get the same problem. Code for
cookie1.aspx is pasted below and the page can be viewed at:
http://hosting.msugs.ch/pragna/cookie1.aspx
Thanks,
Andy
<%@ page language="vb" %>
<script language="vb" runat="server">
Sub Page_Load(Sender As Object, E As EventArgs)
If Not IsPostBack Then
mybutton.Text = "Save Cookie"
mydropdownlist.Items.Add("Blue")
mydropdownlist.Items.Add("Red")
mydropdownlist.Items.Add("Gray")
End If
End Sub
Public Sub Click(ByVal Sender As Object, ByVal E As
System.EventArgs)
Dim MyCookie As New HttpCookie("Background")
MyCookie.Value = mydropdownlist.SelectedItem.Text
Response.Cookies.Add(MyCookie)
Dim dt As DateTime = DateTime.Now()
Dim ts As New TimeSpan(0,0,10,0)
Response.Cookies("MyCookie").Expires = dt.Add(ts)
End Sub
</script>
<html>
<body>
<form id="CookieForm" method="post" runat="server">
<asp:dropdownlist id="mydropdownlist" runat="server"
/>
<asp:button id="mybutton" runat="server"
onclick="Click" />
<br />
<a href="cookie2.aspx">cookie2.aspx</a>
</form>
</body>
</html>
-----Original Message-----
From: Imar@S... [mailto:Imar@S...]
Sent: 24 April 2002 19:59
To: aspx_beginners
Subject: [aspx_beginners] Re: my first cookie
By default, a cookie will be cleared when the user closes the browser.
You'll need to set the Expires property explicitly, like this:
Dim dt As DateTime = DateTime.Now()
Dim ts As New TimeSpan(0,0,10,0)
Response.Cookies("MyCookie").Expires = dt.Add(ts)
This will set the cookie to expire in 10 minutes. The Expires property
expects a DateTime object, so there are other ways of setting it than
using the TimeSpan.
HtH
Imar
> Hi,
Can anyone help?
I'm having trouble with by first attempt at using cookies from 'Beginning
ASP.NET using VB.NET' Chapter 10 'Try it out - using cookies'.
I'm not convinced that my first page (cookie1.aspx is actually saving a
cookie to my hard disk). If I click the link to cookie2.aspx from within
cookie1.aspx after I have pressed the save cookie button then I get the
second page with the background I have selected. However, if I close my
browser down and then try to load coookie2.aspx I get the following error:
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set
to an instance of an object.
Source Error:
Line 21: <html>
Line 22:
Line 23: <body bgcolor="<% = Request.Cookies("Background").Value %>">
Line 24:
Line 25: </body>
Here are the aspx files I used:
Cookie1.aspx
<%@ page language="vb" %>
<script language="vb" runat="server">
Sub Page_Load(Sender As Object, E As EventArgs)
If Not IsPostBack Then
mybutton.Text = "Save Cookie"
mydropdownlist.Items.Add("Blue")
mydropdownlist.Items.Add("Red")
mydropdownlist.Items.Add("Gray")
End If
End Sub
Public Sub Click(ByVal Sender As Object, ByVal E As
System.EventArgs)
Dim MyCookie As New HttpCookie("Background")
MyCookie.Value = mydropdownlist.SelectedItem.Text
Response.Cookies.Add(MyCookie)
End Sub
</script>
<html>
<body>
<form id="CookieForm" method="post" runat="server">
<asp:dropdownlist id="mydropdownlist"
runat="server"
/>
<asp:button id="mybutton" runat="server"
onclick="Click" />
<br />
<a href="cookie2.aspx">cookie2.aspx</a>
</form>
</body>
</html>
Cookie2.aspx
<%@ page language="vb" %>
<script language="vb" runat="server">
Sub Page_Load(Sender As Object, E As EventArgs)
Response.Cache.SetExpires(DateTime.Now)
End Sub
</script>
<html>
<body bgcolor="<% = Request.Cookies("Background").Value %>">
</body>
</html>
Thanks,
Andy Jordan
Message #4 by Imar@S... on Fri, 26 Apr 2002 10:04:13
|
|
This is caused by the fact that you are setting two cookies: "BackGround"
and "MyCookie".
Change it to this:
Dim dt As DateTime = DateTime.Now()
Dim ts As New TimeSpan(0,0,10,0)
Dim MyCookie As New HttpCookie("Background")
MyCookie.Value = mydropdownlist.SelectedItem.Text
MyCookie.Expires = dt.Add(ts)
Response.Cookies.Add(MyCookie)
This will expire the cookie "BackGround" after 10 minutes
HtH
Imar
> By default, a cookie will be cleared when the user closes the browser.
Y> ou'll need to set the Expires property explicitly, like this:
> Dim dt As DateTime = DateTime.Now()
D> im ts As New TimeSpan(0,0,10,0)
>
R> esponse.Cookies("MyCookie").Expires = dt.Add(ts)
> This will set the cookie to expire in 10 minutes. The Expires property
e> xpects a DateTime object, so there are other ways of setting it than
u> sing the TimeSpan.
> HtH
> Imar
>
> > Hi,
> Can anyone help?
I> 'm having trouble with by first attempt at using cookies from 'Beginning
A> SP.NET using VB.NET' Chapter 10 'Try it out - using cookies'.
I> 'm not convinced that my first page (cookie1.aspx is actually saving a
c> ookie to my hard disk). If I click the link to cookie2.aspx from within
c> ookie1.aspx after I have pressed the save cookie button then I get the
s> econd page with the background I have selected. However, if I close my
b> rowser down and then try to load coookie2.aspx I get the following
error:
> Object reference not set to an instance of an object.
D> escription: An unhandled exception occurred during the execution of the
c> urrent web request. Please review the stack trace for more information
a> bout the error and where it originated in the code.
> Exception Details: System.NullReferenceException: Object reference not
set
t> o an instance of an object.
> Source Error:
L> ine 21: <html>
L> ine 22:
L> ine 23: <body bgcolor="<% = Request.Cookies("Background").Value %>">
> Line 24:
L> ine 25: </body>
> Here are the aspx files I used:
C> ookie1.aspx
<> %@ page language="vb" %>
> <script language="vb" runat="server">
> Sub Page_Load(Sender As Object, E As EventArgs)
> If Not IsPostBack Then
> mybutton.Text = "Save Cookie"
> mydropdownlist.Items.Add("Blue")
> mydropdownlist.Items.Add("Red")
> mydropdownlist.Items.Add("Gray")
> End If
> End Sub
> Public Sub Click(ByVal Sender As Object, ByVal E As
S> ystem.EventArgs)
> Dim MyCookie As New HttpCookie("Background")
> MyCookie.Value = mydropdownlist.SelectedItem.Text
> Response.Cookies.Add(MyCookie)
> End Sub
> </script>
> <html>
> <body>
> <form id="CookieForm" method="post" runat="server">
> <asp:dropdownlist id="mydropdownlist"
r> unat="server"
/> >
> <asp:button id="mybutton" runat="server"
o> nclick="Click" />
> <br />
> <a href="cookie2.aspx">cookie2.aspx</a>
> </form>
> </body>
<> /html>
> Cookie2.aspx
<> %@ page language="vb" %>
> <script language="vb" runat="server">
> Sub Page_Load(Sender As Object, E As EventArgs)
> Response.Cache.SetExpires(DateTime.Now)
> End Sub
> </script>
> <html>
> <body bgcolor="<% = Request.Cookies("Background").Value %>">
> </body>
<> /html>
>
T> hanks,
A> ndy Jordan
>
Message #5 by "Jordan, Andy" <Andy.Jordan@g...> on Fri, 26 Apr 2002 10:20:29 +0100
|
|
Imar,
Thanks for your help this works. I'm completely new to programming and I've
got confused between the cookie object in my code (MyCookie) and the name in
the cookie file stored on the users hard disk (Background). I think I've got
it now.
Thanks,
Andy
-----Original Message-----
From: Imar@S... [mailto:Imar@S...]
Sent: 26 April 2002 11:04
To: aspx_beginners
Subject: [aspx_beginners] Re: my first cookie
This is caused by the fact that you are setting two cookies: "BackGround"
and "MyCookie".
Change it to this:
Dim dt As DateTime = DateTime.Now()
Dim ts As New TimeSpan(0,0,10,0)
Dim MyCookie As New HttpCookie("Background")
MyCookie.Value = mydropdownlist.SelectedItem.Text
MyCookie.Expires = dt.Add(ts)
Response.Cookies.Add(MyCookie)
This will expire the cookie "BackGround" after 10 minutes
HtH
Imar
> By default, a cookie will be cleared when the user closes the browser.
Y> ou'll need to set the Expires property explicitly, like this:
> Dim dt As DateTime = DateTime.Now()
D> im ts As New TimeSpan(0,0,10,0)
>
R> esponse.Cookies("MyCookie").Expires = dt.Add(ts)
> This will set the cookie to expire in 10 minutes. The Expires property
e> xpects a DateTime object, so there are other ways of setting it than
u> sing the TimeSpan.
> HtH
> Imar
>
> > Hi,
> Can anyone help?
I> 'm having trouble with by first attempt at using cookies from 'Beginning
A> SP.NET using VB.NET' Chapter 10 'Try it out - using cookies'.
I> 'm not convinced that my first page (cookie1.aspx is actually saving a
c> ookie to my hard disk). If I click the link to cookie2.aspx from within
c> ookie1.aspx after I have pressed the save cookie button then I get the
s> econd page with the background I have selected. However, if I close my
b> rowser down and then try to load coookie2.aspx I get the following
error:
> Object reference not set to an instance of an object.
D> escription: An unhandled exception occurred during the execution of the
c> urrent web request. Please review the stack trace for more information
a> bout the error and where it originated in the code.
> Exception Details: System.NullReferenceException: Object reference not
set
t> o an instance of an object.
> Source Error:
L> ine 21: <html>
L> ine 22:
L> ine 23: <body bgcolor="<% = Request.Cookies("Background").Value %>">
> Line 24:
L> ine 25: </body>
> Here are the aspx files I used:
C> ookie1.aspx
<> %@ page language="vb" %>
> <script language="vb" runat="server">
> Sub Page_Load(Sender As Object, E As EventArgs)
> If Not IsPostBack Then
> mybutton.Text = "Save Cookie"
> mydropdownlist.Items.Add("Blue")
> mydropdownlist.Items.Add("Red")
> mydropdownlist.Items.Add("Gray")
> End If
> End Sub
> Public Sub Click(ByVal Sender As Object, ByVal E As
S> ystem.EventArgs)
> Dim MyCookie As New HttpCookie("Background")
> MyCookie.Value = mydropdownlist.SelectedItem.Text
> Response.Cookies.Add(MyCookie)
> End Sub
> </script>
> <html>
> <body>
> <form id="CookieForm" method="post" runat="server">
> <asp:dropdownlist id="mydropdownlist"
r> unat="server"
/> >
> <asp:button id="mybutton" runat="server"
o> nclick="Click" />
> <br />
> <a href="cookie2.aspx">cookie2.aspx</a>
> </form>
> </body>
<> /html>
> Cookie2.aspx
<> %@ page language="vb" %>
> <script language="vb" runat="server">
> Sub Page_Load(Sender As Object, E As EventArgs)
> Response.Cache.SetExpires(DateTime.Now)
> End Sub
> </script>
> <html>
> <body bgcolor="<% = Request.Cookies("Background").Value %>">
> </body>
<> /html>
>
T> hanks,
A> ndy Jordan
>
|
|
 |