how to use global.asax?

Discussion in 'ASP .Net' started by Chris, May 10, 2006.

  1. Chris

    Chris Guest

    Hi,

    I want to put the namespaces and the data connection into global.asax like
    this:
    <%@ import namespace="System.Data"%>
    <%@ import namespace="System.Data.OleDb"%>
    <script runat="server">
    Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
    Dim oConnection As System.Data.OleDb.OleDbConnection
    oConnection = New System.Data.OleDb.OleDbConnection()
    Dim comd As System.Data.OleDb.OleDbCommand
    Dim dtreader As System.Data.OleDb.OleDbDataReader
    Dim sConnectionString As String
    sConnectionString = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source
    = c:\mydb.mdb"
    oConnection.ConnectionString = sConnectionString
    oConnection.Open()
    End Sub
    </script>

    But how to link the global.asax file to the file (fetch.aspx) which must
    fetch the data?
    I did this in fetch.aspx file which generates an error "Name 'comd' is not
    declared"

    comd = New System.Data.OleDb.OleDbCommand("select count(*) from pc",
    oConnection)

    Thanks for helping.
    Chris
     
    Chris, May 10, 2006
    #1
    1. Advertising

  2. Chris

    Mark Rae Guest

    "Chris" <> wrote in message
    news:%...

    This is one of the worst thing you can do in ASP.NET - the absolute LAST
    thing you should do is to keep a connection to your RDBMS open for any
    longer than is necessary, let alone for the entire lifetime of your web app.
    Consider using a DAL (Data Access Layer) instead:
    http://www.15seconds.com/issue/030317.htm

    Also, be aware that using MS Access will severely restrict the number of
    concurrent users that your web app can support - you should seriously
    consider using a server RDBMS instead, preferably one with a native .NET
    data provider e.g. SQL Server, MSDE / SQL Server Express, MySql etc...
     
    Mark Rae, May 10, 2006
    #2
    1. Advertising

  3. Chris

    Patrice Guest

    global.asax works by itself (assuming the application root is defined as a
    web application in the IIS console). You don't have to link it with a
    particular page. Its purpose is to define events that will be triggered when
    appropriate (ie. your code will be automatically triggered when the
    application starts).

    As a side note :
    - you are declaring a local variable. It will be seen only in this event.
    - keeping a connection around is considered as a bad practice.

    The preferred way is to create the connection when needed (behing the scene
    the connection is returned from a pool). This way each request use its own
    connection. You can serve a number of users with fewer connections (if you
    have 100 users, you'll still have currently 10 open connections if 10 users
    are actually running something and others are just reading the web page they
    just get).

    --
    Patrice

    "Chris" <> a écrit dans le message de news:
    %...
    > Hi,
    >
    > I want to put the namespaces and the data connection into global.asax like
    > this:
    > <%@ import namespace="System.Data"%>
    > <%@ import namespace="System.Data.OleDb"%>
    > <script runat="server">
    > Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
    > Dim oConnection As System.Data.OleDb.OleDbConnection
    > oConnection = New System.Data.OleDb.OleDbConnection()
    > Dim comd As System.Data.OleDb.OleDbCommand
    > Dim dtreader As System.Data.OleDb.OleDbDataReader
    > Dim sConnectionString As String
    > sConnectionString = "Provider = Microsoft.Jet.OLEDB.4.0; Data
    > Source
    > = c:\mydb.mdb"
    > oConnection.ConnectionString = sConnectionString
    > oConnection.Open()
    > End Sub
    > </script>
    >
    > But how to link the global.asax file to the file (fetch.aspx) which must
    > fetch the data?
    > I did this in fetch.aspx file which generates an error "Name 'comd' is not
    > declared"
    >
    > comd = New System.Data.OleDb.OleDbCommand("select count(*) from pc",
    > oConnection)
    >
    > Thanks for helping.
    > Chris
    >
    >
     
    Patrice, May 10, 2006
    #3
  4. You have declared the reference for the command in the method. That
    means that it's a local variable, and when it goes out of scope when the
    methods ends, it's lost forever.

    Creating a database connection in Application_Start makes no sense
    anyway, unless it's only used inside the method and properly closed. If
    you would try to use the connection in the web pages, you would quickly
    run into problems, as every user that requests a page will be sharing
    the same connection. That means that only one user at a time can request
    a page, or you will get an error message because the connection is busy.
    Every page that uses the database needs it's own connection.

    Chris wrote:
    > Hi,
    >
    > I want to put the namespaces and the data connection into global.asax like
    > this:
    > <%@ import namespace="System.Data"%>
    > <%@ import namespace="System.Data.OleDb"%>
    > <script runat="server">
    > Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
    > Dim oConnection As System.Data.OleDb.OleDbConnection
    > oConnection = New System.Data.OleDb.OleDbConnection()
    > Dim comd As System.Data.OleDb.OleDbCommand
    > Dim dtreader As System.Data.OleDb.OleDbDataReader
    > Dim sConnectionString As String
    > sConnectionString = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source
    > = c:\mydb.mdb"
    > oConnection.ConnectionString = sConnectionString
    > oConnection.Open()
    > End Sub
    > </script>
    >
    > But how to link the global.asax file to the file (fetch.aspx) which must
    > fetch the data?
    > I did this in fetch.aspx file which generates an error "Name 'comd' is not
    > declared"
    >
    > comd = New System.Data.OleDb.OleDbCommand("select count(*) from pc",
    > oConnection)
    >
    > Thanks for helping.
    > Chris
    >
    >
     
    =?ISO-8859-1?Q?G=F6ran_Andersson?=, May 10, 2006
    #4
  5. Chris

    Chris Guest

    Ok, thanks for your advices ..;


    "Göran Andersson" <> wrote in message
    news:...
    > You have declared the reference for the command in the method. That
    > means that it's a local variable, and when it goes out of scope when the
    > methods ends, it's lost forever.
    >
    > Creating a database connection in Application_Start makes no sense
    > anyway, unless it's only used inside the method and properly closed. If
    > you would try to use the connection in the web pages, you would quickly
    > run into problems, as every user that requests a page will be sharing
    > the same connection. That means that only one user at a time can request
    > a page, or you will get an error message because the connection is busy.
    > Every page that uses the database needs it's own connection.
    >
    > Chris wrote:
    > > Hi,
    > >
    > > I want to put the namespaces and the data connection into global.asax

    like
    > > this:
    > > <%@ import namespace="System.Data"%>
    > > <%@ import namespace="System.Data.OleDb"%>
    > > <script runat="server">
    > > Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
    > > Dim oConnection As System.Data.OleDb.OleDbConnection
    > > oConnection = New System.Data.OleDb.OleDbConnection()
    > > Dim comd As System.Data.OleDb.OleDbCommand
    > > Dim dtreader As System.Data.OleDb.OleDbDataReader
    > > Dim sConnectionString As String
    > > sConnectionString = "Provider = Microsoft.Jet.OLEDB.4.0; Data

    Source
    > > = c:\mydb.mdb"
    > > oConnection.ConnectionString = sConnectionString
    > > oConnection.Open()
    > > End Sub
    > > </script>
    > >
    > > But how to link the global.asax file to the file (fetch.aspx) which must
    > > fetch the data?
    > > I did this in fetch.aspx file which generates an error "Name 'comd' is

    not
    > > declared"
    > >
    > > comd = New System.Data.OleDb.OleDbCommand("select count(*) from pc",
    > > oConnection)
    > >
    > > Thanks for helping.
    > > Chris
    > >
    > >
     
    Chris, May 10, 2006
    #5
    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:
    805
    Alvin Bruney
    Jul 29, 2003
  2. Daniel Bass
    Replies:
    2
    Views:
    6,906
    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,850
  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:
    864
    =?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,187
    Mark Rae
    May 23, 2006
Loading...

Share This Page