Global.asax file

Discussion in 'ASP .Net' started by Prince, Dec 1, 2003.

  1. Prince

    Prince Guest

    I have a question about the global.asax.cs file. I'm
    reading info from a database to populate a DataGrid. I
    read somewhere that the opening of the database should
    occur in the global.asax.cs file that way every request
    doesn't open the database again.

    How is this done? It doesn't seem that the global file
    can access the DataGrid webcontrol, located on the
    index.aspx, file. Only the index.aspx.cs file can access
    this control.

    Also, I tried putting the connection object inside the
    global.asax.cs file but could not reference it in the
    index.aspx.cs file.

    What is the best practice for populating a DataGrid from
    a database table? I would think I could open the
    database, populate the DataGrid and close the connection
    all within the global.asax.cs file's Application_Start()
    event handler. Then within the index.aspx.cs file, just
    re-bind the control.

    Does someone please give me an example of how this is
    done?

    thanks,
    Prince
     
    Prince, Dec 1, 2003
    #1
    1. Advertising

  2. You read wrong.
    Do not use the same connection throughout your site. This would be very bad
    and would limit your scalability severely.
    ADO.NET has built in connection pooling.
    Therefore you should open a database connection just before you need it on a
    page, and close the database connection as soon as possible. The connection
    pooling makes this very efficient.

    --
    I hope this helps,
    Steve C. Orr, MCSD, MVP
    http://Steve.Orr.net
    Hire top-notch developers at http://www.able-consulting.com



    "Prince" <> wrote in message
    news:037901c3b847$647f29b0$...
    > I have a question about the global.asax.cs file. I'm
    > reading info from a database to populate a DataGrid. I
    > read somewhere that the opening of the database should
    > occur in the global.asax.cs file that way every request
    > doesn't open the database again.
    >
    > How is this done? It doesn't seem that the global file
    > can access the DataGrid webcontrol, located on the
    > index.aspx, file. Only the index.aspx.cs file can access
    > this control.
    >
    > Also, I tried putting the connection object inside the
    > global.asax.cs file but could not reference it in the
    > index.aspx.cs file.
    >
    > What is the best practice for populating a DataGrid from
    > a database table? I would think I could open the
    > database, populate the DataGrid and close the connection
    > all within the global.asax.cs file's Application_Start()
    > event handler. Then within the index.aspx.cs file, just
    > re-bind the control.
    >
    > Does someone please give me an example of how this is
    > done?
    >
    > thanks,
    > Prince
     
    Steve C. Orr [MVP, MCSD], Dec 1, 2003
    #2
    1. Advertising

  3. Here is how I do it...

    In Global.asax I drag the DbConnections that I need. I add DbCommands as
    needed. I add DbDataAdapters as needed.

    So, for each database I have a connection
    For each connection I have 4 commands
    For each connection I have an Adapter

    That is all that the Global.asax holds, an instance of the objects that I
    need to use on any page of the website

    On the page I want to use these objects I declare a Variable like so

    ---------------------
    Private wrMain as New Global
    ---------------------

    Now I can use the variable wrMain to define the objects like so

    -------------------------
    Dim ds as New DataSet
    Try
    Me.wrMain.DbConn.Open()
    Me.wrMain.DbSelect.CommandText = "SQL TEXT"
    Me.wrMain.DbAdapter.Fill(ds, "tablename")
    Me.DataGrid.DataSource = ds
    Me.DataGrid.DataMember = ds.Tables(0).TableName
    Me.DataGrid.DataBind()
    Catch ex as Exception
    Me.lblError.Text = "Error... " & ex.Message
    Finally
    Me.wrMain.DbConn.Close()
    End Try
    ------------------------------




    "Prince" <> wrote in message
    news:037901c3b847$647f29b0$...
    > I have a question about the global.asax.cs file. I'm
    > reading info from a database to populate a DataGrid. I
    > read somewhere that the opening of the database should
    > occur in the global.asax.cs file that way every request
    > doesn't open the database again.
    >
    > How is this done? It doesn't seem that the global file
    > can access the DataGrid webcontrol, located on the
    > index.aspx, file. Only the index.aspx.cs file can access
    > this control.
    >
    > Also, I tried putting the connection object inside the
    > global.asax.cs file but could not reference it in the
    > index.aspx.cs file.
    >
    > What is the best practice for populating a DataGrid from
    > a database table? I would think I could open the
    > database, populate the DataGrid and close the connection
    > all within the global.asax.cs file's Application_Start()
    > event handler. Then within the index.aspx.cs file, just
    > re-bind the control.
    >
    > Does someone please give me an example of how this is
    > done?
    >
    > thanks,
    > Prince
     
    news.airmail.net, Dec 1, 2003
    #3
  4. Prince

    SF Guest

    I've been using threadstatics to access the Global object:

    public class Global : System.Web.HttpApplication
    {
    ...
    [ThreadStatic] private static Global myGlobal;
    public static Global MyGlobal
    {
    get { return myGlobal; }
    set { myGlobal= value; }
    }
    ...
    protected void Application_BeginRequest(Object sender, EventArgs e)
    {
    MyGlobal =this;
    }
    }

    Like this you can access your global object (like a singleton) anywhere in
    you application, e.g. in index.aspx.cs
    by referring to "Global g = Global.MyGlobal".





    "Prince" <> wrote in message
    news:037901c3b847$647f29b0$...
    > I have a question about the global.asax.cs file. I'm
    > reading info from a database to populate a DataGrid. I
    > read somewhere that the opening of the database should
    > occur in the global.asax.cs file that way every request
    > doesn't open the database again.
    >
    > How is this done? It doesn't seem that the global file
    > can access the DataGrid webcontrol, located on the
    > index.aspx, file. Only the index.aspx.cs file can access
    > this control.
    >
    > Also, I tried putting the connection object inside the
    > global.asax.cs file but could not reference it in the
    > index.aspx.cs file.
    >
    > What is the best practice for populating a DataGrid from
    > a database table? I would think I could open the
    > database, populate the DataGrid and close the connection
    > all within the global.asax.cs file's Application_Start()
    > event handler. Then within the index.aspx.cs file, just
    > re-bind the control.
    >
    > Does someone please give me an example of how this is
    > done?
    >
    > thanks,
    > Prince
     
    SF, Dec 2, 2003
    #4
  5. Prince

    Alvin Bruney Guest

    Did you read Steve's response. He said that what you are doing is bad. You
    should stop doing it and not try to explain why you are doing it this way.
    You still have global handles holding connection objects leaking resources.

    --
    Regards,
    Alvin Bruney
    Got Tidbits? Get it here
    www.networkip.net/tidbits
    "news.airmail.net" <> wrote in message
    news:bqggk1$...
    > Here is how I do it...
    >
    > In Global.asax I drag the DbConnections that I need. I add DbCommands as
    > needed. I add DbDataAdapters as needed.
    >
    > So, for each database I have a connection
    > For each connection I have 4 commands
    > For each connection I have an Adapter
    >
    > That is all that the Global.asax holds, an instance of the objects that I
    > need to use on any page of the website
    >
    > On the page I want to use these objects I declare a Variable like so
    >
    > ---------------------
    > Private wrMain as New Global
    > ---------------------
    >
    > Now I can use the variable wrMain to define the objects like so
    >
    > -------------------------
    > Dim ds as New DataSet
    > Try
    > Me.wrMain.DbConn.Open()
    > Me.wrMain.DbSelect.CommandText = "SQL TEXT"
    > Me.wrMain.DbAdapter.Fill(ds, "tablename")
    > Me.DataGrid.DataSource = ds
    > Me.DataGrid.DataMember = ds.Tables(0).TableName
    > Me.DataGrid.DataBind()
    > Catch ex as Exception
    > Me.lblError.Text = "Error... " & ex.Message
    > Finally
    > Me.wrMain.DbConn.Close()
    > End Try
    > ------------------------------
    >
    >
    >
    >
    > "Prince" <> wrote in message
    > news:037901c3b847$647f29b0$...
    > > I have a question about the global.asax.cs file. I'm
    > > reading info from a database to populate a DataGrid. I
    > > read somewhere that the opening of the database should
    > > occur in the global.asax.cs file that way every request
    > > doesn't open the database again.
    > >
    > > How is this done? It doesn't seem that the global file
    > > can access the DataGrid webcontrol, located on the
    > > index.aspx, file. Only the index.aspx.cs file can access
    > > this control.
    > >
    > > Also, I tried putting the connection object inside the
    > > global.asax.cs file but could not reference it in the
    > > index.aspx.cs file.
    > >
    > > What is the best practice for populating a DataGrid from
    > > a database table? I would think I could open the
    > > database, populate the DataGrid and close the connection
    > > all within the global.asax.cs file's Application_Start()
    > > event handler. Then within the index.aspx.cs file, just
    > > re-bind the control.
    > >
    > > Does someone please give me an example of how this is
    > > done?
    > >
    > > thanks,
    > > Prince

    >
    >
     
    Alvin Bruney, Dec 2, 2003
    #5
  6. Prince

    Prince Guest

    thanks for the response. But what if I only wanted to
    read from the database only one time and populate the
    DataGrid control since every user will be viewing the
    same data.

    Will leaving the connection in the particular page still
    the best method?

    -- Prince


    >-----Original Message-----
    >You read wrong.
    >Do not use the same connection throughout your site.

    This would be very bad
    >and would limit your scalability severely.
    >ADO.NET has built in connection pooling.
    >Therefore you should open a database connection just

    before you need it on a
    >page, and close the database connection as soon as

    possible. The connection
    >pooling makes this very efficient.
    >
    >--
    >I hope this helps,
    >Steve C. Orr, MCSD, MVP
    >http://Steve.Orr.net
    >Hire top-notch developers at http://www.able-

    consulting.com
    >
    >
    >
    >"Prince" <> wrote in message
    >news:037901c3b847$647f29b0$...
    >> I have a question about the global.asax.cs file. I'm
    >> reading info from a database to populate a DataGrid. I
    >> read somewhere that the opening of the database should
    >> occur in the global.asax.cs file that way every request
    >> doesn't open the database again.
    >>
    >> How is this done? It doesn't seem that the global file
    >> can access the DataGrid webcontrol, located on the
    >> index.aspx, file. Only the index.aspx.cs file can

    access
    >> this control.
    >>
    >> Also, I tried putting the connection object inside the
    >> global.asax.cs file but could not reference it in the
    >> index.aspx.cs file.
    >>
    >> What is the best practice for populating a DataGrid

    from
    >> a database table? I would think I could open the
    >> database, populate the DataGrid and close the

    connection
    >> all within the global.asax.cs file's Application_Start

    ()
    >> event handler. Then within the index.aspx.cs file,

    just
    >> re-bind the control.
    >>
    >> Does someone please give me an example of how this is
    >> done?
    >>
    >> thanks,
    >> Prince

    >
    >
    >.
    >
     
    Prince, Dec 2, 2003
    #6
  7. In this case you should fill a DataTable from within your Global.asax.
    Put it into Application state with a line like this:
    Application("MyData") = MyDataTable

    Then you can pull the DataTable from any page you need and bind it to
    DataGrids or whatever.
    Since DataTables are disconnected from the database, this is quite
    efficient.

    --
    I hope this helps,
    Steve C. Orr, MCSD, MVP
    http://Steve.Orr.net
    Hire top-notch developers at http://www.able-consulting.com



    "Prince" <> wrote in message
    news:07a601c3b89b$81db9940$...
    > thanks for the response. But what if I only wanted to
    > read from the database only one time and populate the
    > DataGrid control since every user will be viewing the
    > same data.
    >
    > Will leaving the connection in the particular page still
    > the best method?
    >
    > -- Prince
    >
    >
    > >-----Original Message-----
    > >You read wrong.
    > >Do not use the same connection throughout your site.

    > This would be very bad
    > >and would limit your scalability severely.
    > >ADO.NET has built in connection pooling.
    > >Therefore you should open a database connection just

    > before you need it on a
    > >page, and close the database connection as soon as

    > possible. The connection
    > >pooling makes this very efficient.
    > >
    > >--
    > >I hope this helps,
    > >Steve C. Orr, MCSD, MVP
    > >http://Steve.Orr.net
    > >Hire top-notch developers at http://www.able-

    > consulting.com
    > >
    > >
    > >
    > >"Prince" <> wrote in message
    > >news:037901c3b847$647f29b0$...
    > >> I have a question about the global.asax.cs file. I'm
    > >> reading info from a database to populate a DataGrid. I
    > >> read somewhere that the opening of the database should
    > >> occur in the global.asax.cs file that way every request
    > >> doesn't open the database again.
    > >>
    > >> How is this done? It doesn't seem that the global file
    > >> can access the DataGrid webcontrol, located on the
    > >> index.aspx, file. Only the index.aspx.cs file can

    > access
    > >> this control.
    > >>
    > >> Also, I tried putting the connection object inside the
    > >> global.asax.cs file but could not reference it in the
    > >> index.aspx.cs file.
    > >>
    > >> What is the best practice for populating a DataGrid

    > from
    > >> a database table? I would think I could open the
    > >> database, populate the DataGrid and close the

    > connection
    > >> all within the global.asax.cs file's Application_Start

    > ()
    > >> event handler. Then within the index.aspx.cs file,

    > just
    > >> re-bind the control.
    > >>
    > >> Does someone please give me an example of how this is
    > >> done?
    > >>
    > >> thanks,
    > >> Prince

    > >
    > >
    > >.
    > >
     
    Steve C. Orr [MVP, MCSD], Dec 2, 2003
    #7
    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:
    792
    Alvin Bruney
    Jul 29, 2003
  2. Daniel Bass
    Replies:
    2
    Views:
    6,885
    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,832
  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:
    843
    =?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,169
    Mark Rae
    May 23, 2006
Loading...

Share This Page