global.asax in v2

Discussion in 'ASP .Net' started by GaryDean, Jun 25, 2006.

  1. GaryDean

    GaryDean Guest

    I notice that the global.asax in v2 is inline. because of this I can't say
    Session["mysessionvar"] = conn;

    The compiler will accept it but at runtime the exception says "Session State
    is not available in this context".

    Is this a downgrade feature from 1.1?

    --
    Regards,
    Gary Blakely
    GaryDean, Jun 25, 2006
    #1
    1. Advertising

  2. Are you writing that in Session_OnStart ( or Session_Start ) ?
    If so, and if you define "conn" before you define Session["mysessionvar"], it should work.

    public void Session_OnStart()
    {
    string conn = "someconnection string";
    Session["mysessionvar"] = conn;
    }

    Do you have session state enabled ? If you do...that should work.

    btw, you can always continue to use codebehind in global.asax.

    There's nothing stopping you.

    Just create the global.asax.cs file in the App_Code directory
    ....and code away...in both global.asax and global.asax.cs.

    Make sure that Global inherits from HttpApplication in global.asax.cs :

    public class Global : HttpApplication
    {
    public Global()
    {
    }
    void Application_Start(object sender, EventArgs e)
    {
    // Code that runs on application startup
    //
    // TODO: Add constructor logic for other Application events here
    //
    }
    }

    Make sure you Import all the classes you need in global.asax.cs
    and... in global.asax...you'll need this directive :

    <%@ Application Language="C#" Inherits="Global" %>

    That's it!
    You can continue to blissfully code the way you're used to.

    Quite frankly, I don't know why you'd want to do that,
    since in global.asax you *can* do anything you can do in global.asax.cs
    without needing to double-compile your global.code but, hey, we aim to please!

    :)



    Juan T. Llibre, asp.net MVP
    aspnetfaq.com : http://www.aspnetfaq.com/
    asp.net faq : http://asp.net.do/faq/
    foros de asp.net, en espaƱol : http://asp.net.do/foros/
    ===================================
    "GaryDean" <> wrote in message
    news:...
    >I notice that the global.asax in v2 is inline. because of this I can't say Session["mysessionvar"]
    >= conn;
    >
    > The compiler will accept it but at runtime the exception says "Session State is not available in
    > this context".
    >
    > Is this a downgrade feature from 1.1?
    >
    > --
    > Regards,
    > Gary Blakely
    >
    >
    Juan T. Llibre, Jun 25, 2006
    #2
    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. VSK
    Replies:
    1
    Views:
    767
    Alvin Bruney
    Jul 29, 2003
  2. Daniel Bass
    Replies:
    2
    Views:
    6,857
    Daniel Bass
    Dec 31, 2003
  3. 2obvious

    no global constants in global.asax?

    2obvious, Sep 4, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    1,798
  4. =?Utf-8?B?YmNoYXJsZXM=?=

    global.asax and global.asax.cs

    =?Utf-8?B?YmNoYXJsZXM=?=, Oct 4, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    801
    =?Utf-8?B?YmNoYXJsZXM=?=
    Oct 5, 2004
  5. Mark Rae

    Global.asax / Global.asax.cs in v2

    Mark Rae, May 23, 2006, in forum: ASP .Net
    Replies:
    6
    Views:
    3,138
    Mark Rae
    May 23, 2006
Loading...

Share This Page