Global.asax file

  • Thread starter Thread starter Prince
  • Start date Start date
P

Prince

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
 
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.
 
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
 
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".
 
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.
 
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
 
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
 
Back
Top