 |
| ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

September 8th, 2003, 10:32 AM
|
|
Registered User
|
|
Join Date: Sep 2003
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Can't set cookies
Hi All,
I am having problems setting cookies.
Here is the code that I am using to set a cookie:
<code>
<%@ import Namespace="System.Web.HttpCookie" %>
<%@ import Namespace="System.Web.HttpCookieCollection" %>
<script runat="server">
Dim objCookie as new HttpCookie("myCookie", "Hello!")
Response.Cookies.Add(objCookie)
</script>
</code>
I get the following error:
Compiler Error Message: BC30188: Declaration expected.
What am I doing wrong? This seems like such a simple thing, but I cannot get it to work.
Thanks,
Mark
|
|

September 8th, 2003, 11:10 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
Missing a letter?
<%@ imports Namespace="System.Web.HttpCookie" %>
<%@ imports Namespace="System.Web.HttpCookieCollection" %>
I always work with code-behind projects, so your syntax may be correct. Please disregard if this is wrong.
Peter
|
|

September 8th, 2003, 11:14 AM
|
|
Registered User
|
|
Join Date: Sep 2003
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Peter,
When I add the 's', I get an error.
I think the spelling is correct.
Thanks,
Mark
|
|

September 8th, 2003, 11:15 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
Sorry, I was mistaken....
Your syntax is correct. Just looked it up. Odd that the "Import" syntax is different between code-behind and inline code.
|
|

September 8th, 2003, 11:18 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
Wait... I thought of something.
Try your code like this.
Dim objCookie as new System.Web.HttpCookie("myCookie", "Hello!")
Response.Cookies.Add(objCookie)
I think your problem is that you are importing the "HttpCookie" itself instead of it's parent class. You probably need to change the imports statement to
<%@ import Namespace="System.Web" %>
That's why it's saying "Declaration expected", it doesn't see "HttpCookie" on System.Web.HttpCookie or System.Web.HttpCookieCollection.
Peter
|
|

September 8th, 2003, 12:12 PM
|
|
Registered User
|
|
Join Date: Sep 2003
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I changed the code to the following, but still no luck:
<%@ import Namespace="System.Web" %>
<script runat="server">
Dim objCookie as new System.Web.HttpCookie("myCookie", "Hello!")
Response.Cookies.Add(objCookie)
</script>
I get the following error:
Compiler Error Message: BC30188: Declaration expected.
I even tried to add different imports. Still no luck.
Any more ideas?!?!?!
Thanks,
Mark
|
|

September 8th, 2003, 12:36 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi meckeard...
Ohhh! I am not used to VB, but you can do this in VB...
Code:
<%@ import Namespace="System.Web.HttpCookie" %>
<%@ import Namespace="System.Web.HttpCookieCollection" %>
<script runat="server">
sub Page_Load(Sender as Object, e as EventArgs)
Response.Cookies("testCookie").Value = "testContents"
Response.Write(Request.Cookies("testCookie").Value & "<BR>")
Response.Cookies("testCookie")("test01") = "value01"
Response.Cookies("testCookie")("test02") = "value02"
dim str as string
for each str in Response.Cookies("testCookie").Values
Response.Write(str & "<BR>")
next
end sub
</script>
If you want to get a reference to the cookie by declaring it... let me know.
Hope it helps! ;)
Jacob.
|
|

September 8th, 2003, 12:41 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi there,
Your problem is not caused by importing incorrect Cookie stuff, but by the fact you're using "classic ASP" coding: You are using in-line script, while ASP.NET code should be placed inside Functions or Subs. Try this:
Code:
<%@ import Namespace="System.Web.HttpCookie" %>
<script runat="server">
Public Sub Page_Load(sender as object,e as eventargs)
Dim objCookie as new HttpCookie("myCookie", "Hello!")
objCookie.Expires = DateTime.Now.Add(New TimeSpan(0,0,10,0))
Response.Cookies.Add(objCookie)
End Sub
</script>
This will create the cookie you had in your code, and set its Expiry property to Now + 10 minutes.
By using the Page_Load method, this code will execute when, eeuh, eeuh, the page loads ;)
HtH,
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
|
|
 |