Global.asax file

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
 
S

Steve C. Orr [MVP, MCSD]

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.
 
N

news.airmail.net

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
 
S

SF

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".
 
A

Alvin Bruney

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.
 
P

Prince

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
 
S

Steve C. Orr [MVP, MCSD]

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top