How to access session variable?

Discussion in 'ASP General' started by Eric, May 26, 2005.

  1. Eric

    Eric Guest

    Hi,

    I've a problem with trying to retrieve a session variable in an include file.

    Basically, the main asp creates a session variable:

    <% Session("var1") = "Hello" %>

    And then when I click on a button it refers to the include file, which I
    believe is all client-side code as there are no server <% %> tags.

    I want to be able to use this session variable in the include file, how do I
    do this?

    If I try retrieving this session variable with Session("var"), it will come
    up with an error saying that it has a type mismatch 'Session'.

    The session state is enabled in IIS manager and appears to be working with
    the other session variables in asp's not written with the server tags <% %>.

    The include file has the .asp extension and has two language sections and I
    want to use the session variable within the vbscript language tag like this:

    <Script Language="JavaScript">
    ...
    ...
    ...
    </Script>

    <Script Language="VBScript">

    If Not Session("Var") Then
    ...
    ...
    End If

    </Script>

    I'm getting the type mismatch: "session" error on the IF block in the above
    sample.

    Thanks for your help in advance,
    Eric.
    Eric, May 26, 2005
    #1
    1. Advertising

  2. Eric

    Roland Hall Guest

    "Eric" <> wrote in message
    news:...
    : Hi,
    :
    : I've a problem with trying to retrieve a session variable in an include
    file.
    :
    : Basically, the main asp creates a session variable:
    :
    : <% Session("var1") = "Hello" %>
    :
    : And then when I click on a button it refers to the include file, which I
    : believe is all client-side code as there are no server <% %> tags.
    :
    : I want to be able to use this session variable in the include file, how do
    I
    : do this?
    :
    : If I try retrieving this session variable with Session("var"), it will
    come
    : up with an error saying that it has a type mismatch 'Session'.
    :
    : The session state is enabled in IIS manager and appears to be working with
    : the other session variables in asp's not written with the server tags <%
    %>.
    :
    : The include file has the .asp extension and has two language sections and
    I
    : want to use the session variable within the vbscript language tag like
    this:
    :
    : <Script Language="JavaScript">
    : ...
    : ...
    : ...
    : </Script>
    :
    : <Script Language="VBScript">
    :
    : If Not Session("Var") Then
    : ...
    : ...
    : End If
    :
    : </Script>
    :
    : I'm getting the type mismatch: "session" error on the IF block in the
    above
    : sample.

    You may be able to pass the session variable value to client-side code but
    to access it directly you probably need to run it server-side.

    <%

    dim myvar
    myvar = session("Var")
    %>
    <script type="text/javascript">
    var myvar = <%=myvar%>;
    alert(myvar);
    </script>
    <script type="text/vbscript">
    dim myvar : myvar = <%=myvar%>
    msgbox myvar
    </script>

    or

    <script type="text/vbscript" runat="server">
    dim myvar
    myvar = session("Var")
    </script>

    --
    Roland Hall
    /* This information is distributed in the hope that it will be useful, but
    without any warranty; without even the implied warranty of merchantability
    or fitness for a particular purpose. */
    Technet Script Center - http://www.microsoft.com/technet/scriptcenter/
    WSH 5.6 Documentation - http://msdn.microsoft.com/downloads/list/webdev.asp
    MSDN Library - http://msdn.microsoft.com/library/default.asp
    Roland Hall, May 26, 2005
    #2
    1. Advertising

  3. there may be a way to put your server side session variable into something
    so client side code can read it but I have never done it.
    generallly you write it server side and read it server side

    http://www.powerasp.com/content/code-snippets/session-variables.asp


    "Eric" <> wrote in message
    news:...
    > Hi,
    >
    > I've a problem with trying to retrieve a session variable in an include
    > file.
    >
    > Basically, the main asp creates a session variable:
    >
    > <% Session("var1") = "Hello" %>
    >
    > And then when I click on a button it refers to the include file, which I
    > believe is all client-side code as there are no server <% %> tags.
    >
    > I want to be able to use this session variable in the include file, how do
    > I
    > do this?
    >
    > If I try retrieving this session variable with Session("var"), it will
    > come
    > up with an error saying that it has a type mismatch 'Session'.
    >
    > The session state is enabled in IIS manager and appears to be working with
    > the other session variables in asp's not written with the server tags <%
    > %>.
    >
    > The include file has the .asp extension and has two language sections and
    > I
    > want to use the session variable within the vbscript language tag like
    > this:
    >
    > <Script Language="JavaScript">
    > ...
    > ...
    > ...
    > </Script>
    >
    > <Script Language="VBScript">
    >
    > If Not Session("Var") Then
    > ...
    > ...
    > End If
    >
    > </Script>
    >
    > I'm getting the type mismatch: "session" error on the IF block in the
    > above
    > sample.
    >
    > Thanks for your help in advance,
    > Eric.
    Kyle Peterson, May 26, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeff Smythe
    Replies:
    3
    Views:
    1,230
    Jeff Smythe
    Jan 2, 2004
  2. Replies:
    1
    Views:
    1,071
    Bruce Barker
    Nov 14, 2005
  3. =?Utf-8?B?Um9iSEs=?=
    Replies:
    4
    Views:
    5,254
    =?Utf-8?B?Um9iSEs=?=
    Apr 11, 2007
  4. chowchho
    Replies:
    7
    Views:
    619
    Patrice
    Mar 28, 2008
  5. Jazzis
    Replies:
    2
    Views:
    235
    Jazzis
    Sep 23, 2003
Loading...

Share This Page