Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Declaration expected. when reading a Session variable. 1

Status
Not open for further replies.

qwert231

Programmer
Sep 4, 2001
756
US
All I want to do is get this variable from one page to another. What am I doing wrong?

Code:
Dim nowJob, ybJob As String
nowJob = Session("nowJob") ' This is the line generating the error.
ybJob = Session("ybJob")
 
what is the error That'l do donkey, that'l do
[bravo] Mark
 
Error: Declaration expected.

The Session variable is not set from within this page. Also, the Session variable may not be set by the time this page is accessed, in which case the text I would normally have show would not...
 
it is not the session that is making the error it's the string.

A session variable if not set returns a blank string ie ""

However, your syntax for the above code is fine. Is it a runtime error or compile time? That'l do donkey, that'l do
[bravo] Mark
 
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: BC30188: Declaration expected.


PS, thanks for being so responsive...
 
what other code do you have on the page. I copied what you posted and it ran fine on my machine.

try commenting out the trouble line and running it see if that works.

Also try declaring your strings on separate lines. I am kind of just trying things that have worked in the past so we can see what is causeing the error. That'l do donkey, that'l do
[bravo] Mark
 
I decided to try a different approach for this. Here is the entire page. Still get the same error on that line calling the session variable.

Code:
<%@ Page Language=&quot;VB&quot; Debug=&quot;True&quot; %>
<script runat=&quot;server&quot;>

    nowJob.Text = Session(&quot;nowJob&quot;)
'Here
Code:
    ybJob.Text = Session(&quot;ybJob&quot;)
'Or Here
Code:
    if Session(&quot;ybJob&quot;) = &quot;&quot; then ybLbl.Visible = False

</script>
<html>
<head>
</head>
<body>
    <form runat=&quot;server&quot;>
        <p>
            <asp:Label id=&quot;ybLbl&quot; runat=&quot;server&quot; Font-Size=&quot;Larger&quot; Font-Bold=&quot;True&quot;>Yearbooks</asp:Label>
        </p>
        <p>
            <asp:Label id=&quot;packLbl&quot; runat=&quot;server&quot; Font-Size=&quot;Larger&quot; Font-Bold=&quot;True&quot;>Packages</asp:Label>
        </p>
        <asp:Label id=&quot;nowJob&quot; runat=&quot;server&quot;>Label</asp:Label>
        <br />
        <asp:Label id=&quot;ybJob&quot; runat=&quot;server&quot;>Label</asp:Label>
    </form>
</body>
</html>
 
Check this out:
Compiler Error Message: BC30689: Statement cannot appear outside of a method body.

Line 4: 'nowJob.Text = Session(&quot;nowJob&quot;)
Line 5: 'ybJob.Text = Session(&quot;ybJob&quot;)
Line 6: if Session(&quot;ybJob&quot;) = &quot;&quot; then ybLbl.Visible = False
Line 7:
Line 8: </script>
 
1. you need an end if on your if statment.

2. you are still trying to program in the old school asp style.

With the asp.net stuff things are object oriented. You need events and procedures for things to run.

If I may make a recomendation you should very much consider makeing use of code behind pages. This would solve alot of problems. Not only would your code be enscapsulated in a dll, but it makes it easier to separate code from design.

That said to fix your error you need to put the code into a page_load event. I used an inline page this time and recreated your problems. The following code is the quick fix to your page. The solution would be to follow the masses with code behind techniques.


<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;>
<%@ Page Language=&quot;VB&quot; Debug=&quot;True&quot; %>
<script runat=&quot;server&quot;>
Private Sub Page_Load
nowJob.Text = Session(&quot;nowJob&quot;) 'Here
ybJob.Text = Session(&quot;ybJob&quot;) 'Or Here
if Session(&quot;ybJob&quot;) = &quot;&quot; then
ybLbl.Visible = False
end if
end Sub
</script>

<html>
<head>
<title>HTMLPage1</title>
<meta name=&quot;vs_defaultClientScript&quot; content=&quot;JavaScript&quot;>
<meta name=&quot;vs_targetSchema&quot; content=&quot; <meta name=&quot;GENERATOR&quot; content=&quot;Microsoft Visual Studio.NET 7.0&quot;>
<meta name=&quot;ProgId&quot; content=&quot;VisualStudio.HTML&quot;>
<meta name=&quot;Originator&quot; content=&quot;Microsoft Visual Studio.NET 7.0&quot;>
</head>
<body MS_POSITIONING=&quot;GridLayout&quot;>
<form runat=&quot;server&quot;>
<p>
<asp:Label id=&quot;ybLbl&quot; runat=&quot;server&quot; Font-Size=&quot;Larger&quot; Font-Bold=&quot;True&quot;>Yearbooks</asp:Label>
</p>
<p>
<asp:Label id=&quot;packLbl&quot; runat=&quot;server&quot; Font-Size=&quot;Larger&quot; Font-Bold=&quot;True&quot;>Packages</asp:Label>
</p>
<asp:Label id=&quot;nowJob&quot; runat=&quot;server&quot;>Label</asp:Label>
<br />
<asp:Label id=&quot;ybJob&quot; runat=&quot;server&quot;>Label</asp:Label>
</form>

</body>
</html>


A quick note: should you desregard my advice Plz make a note in any future posts that you are using spaggetti inline code. You questions will be answered sooner.
That'l do donkey, that'l do
[bravo] Mark
 
The challenge of supporting a spahgetti coded ASP page while trying to learn and develope .Net... You were correct, I was a fool. Thank you for your kind patience.

If you could only see what I was doing...
 
whoa! Hold on there bud. I was not calling you a fool. If you can switch over thats great.

It was just that most of the posts here are using code behind and when a spahgetti code post comes up there are certain things which are different.

So as I said if you aren't going to use code behind just give us here at the forum a heads up in your post.

What I gave you there did work didn't it? Just wanted to make sure. That'l do donkey, that'l do
[bravo] Mark
 
Don't worry, I was not bothered by your post. I am just having a bad day. I am spending it supporting the old code (ASP) when I should be making nice, tite, cleaner .Net code.

Actually, I am using Web Matrix, which separates the code from the html nicely. I suppose I could simply move the code to a codebehind, but then I have twice as many files, and since I am the only one working on them, I figure it doesn't matter too much. I am however, using one codebehind file with common code throughout my pages, so that should improve things also.
 
This is courtesy of Paul
Oh yes...

It's infinitely more elegant and manageable than the traditional spaghetti code methods of asp classic.

As far as deployment, consider this:

Let's say that you have a project with 5 directories, multiple subdirectories in each, and say... 3 pages on the root. Just for example.

Ok. Now, you need to go into these pages and make coding changes. Maybe the name of your main db has changed from &quot;almostReadyToKillClients&quot; to &quot;definitelyReadyToKillClients&quot;.

Ok, then. Using code-behind pages, you simply go through and make your changes to the code pages, and only the code pages. Test it... everything looks good.

Now, in asp classic, you would have had to deploy all the little individual pages where you made coding changes, right? Well, with the code-behind method, the only thing you have to release to make the change on your live site is the project .dll itself.

In fact, you don't even have to have the .vb or .cs files on the live server at all... All you need are the UI .aspx pages, and the /bin folder w/ your .dll.

Personally, I think this is a great design pattern, if for no other reason than what I've mentioned here... and there are many many other reasons as well.​

You can see the thread here - thread855-362300

Your call
You know what I recomend.
That'l do donkey, that'l do
[bravo] Mark
 
Well... I afeared... I've never created a .DLL in my life... But as for changing the database name, one file, the global.asax holds that.
 
No reason to fear with .Net. Dll's are no longer the hell they used to be. No sure if inline changes makes one or not.

Dll's in dotnet don't need to be registered. You merely place them in the directory and your done. As for creating it when you build/compile your project one is built for you. That'l do donkey, that'l do
[bravo] Mark
 
Don't get hung on the changing the db name. That was merely an example of a change that you **MIGHT** have to make only on the coding side.

There are infinitely more, and maybe I chose a bad one to demonstrate with.

You will thank yourself for making the design pattern change.

It's ALWAYS best to use whatever tools a company puts out, no matter how against-the-grain it seems. You just have to believe at first there is a reason, and before you know it, you'll see the reasons first hand (if reading about them just isn't enough for you)... not to mention not knowing the latest techniques for development will seriously stunt your career.

Just my two cents. Worth exactly what you paid for them.

;-)
penny1.gif
penny1.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top