Wrox Programmer Forums
|
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
 
Old September 8th, 2003, 10:32 AM
Registered User
 
Join Date: Sep 2003
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to meckeard
Default 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
 
Old September 8th, 2003, 11:10 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

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
 
Old September 8th, 2003, 11:14 AM
Registered User
 
Join Date: Sep 2003
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to meckeard
Default

Peter,

When I add the 's', I get an error.

I think the spelling is correct.

Thanks,
Mark

 
Old September 8th, 2003, 11:15 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

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.
 
Old September 8th, 2003, 11:18 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

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
 
Old September 8th, 2003, 12:12 PM
Registered User
 
Join Date: Sep 2003
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to meckeard
Default

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




 
Old September 8th, 2003, 12:36 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.
 
Old September 8th, 2003, 12:41 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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.





Similar Threads
Thread Thread Starter Forum Replies Last Post
XPath: set operation with a disjoint node set rich_unger XSLT 7 May 6th, 2008 09:24 AM
how Set Multiple values in cookies amol_0008 JSP Basics 1 May 31st, 2007 06:27 AM
Help: Cookies not getting set (c#) meetali ASP.NET 1.0 and 1.1 Professional 5 April 8th, 2004 01:13 PM
Help: Cookies not getting set (c#) meetali ASP.NET 1.0 and 1.1 Basics 0 April 7th, 2004 02:52 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.