By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,087 Members | 1,520 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,087 IT Pros & Developers. It's quick & easy.

Global.asax file

P: n/a
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
Nov 18 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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" <pr******@cox.net> wrote in message
news:03****************************@phx.gbl...
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

Nov 18 '05 #2

P: n/a
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" <pr******@cox.net> wrote in message
news:03****************************@phx.gbl...
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

Nov 18 '05 #3

P: n/a
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" <sa*******@airmail.net> wrote in message
news:bq********@library2.airnews.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
------------------------------


"Prince" <pr******@cox.net> wrote in message
news:03****************************@phx.gbl...
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


Nov 18 '05 #4

P: n/a
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".

"Prince" <pr******@cox.net> wrote in message
news:03****************************@phx.gbl...
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

Nov 18 '05 #5

P: n/a
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 badand 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 apage, and close the database connection as soon as possible. The connectionpooling 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" <pr******@cox.net> wrote in message
news:03****************************@phx.gbl...
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

.

Nov 18 '05 #6

P: n/a
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" <pr******@cox.net> wrote in message
news:07****************************@phx.gbl...
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" <pr******@cox.net> wrote in message
news:03****************************@phx.gbl...
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

.

Nov 18 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.