473,395 Members | 1,443 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Global.asax file

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
6 3645
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: hansiman | last post by:
I use Application_Start in global.asax to set some physical folder paths ie.: Application("pdf") = "c:\www\<site>\pdf\" global.asax uses code behind. When I move the project from the dev to...
22
by: fd123456 | last post by:
Hi Tom ! Sorry about the messy quoting, Google is playing tricks on me at the moment. > Global.asax is where you normally have the Global Application > and Session variables and code to...
5
by: ad | last post by:
The Global.asax is code-inside with default. How to change Global.asax to code-behind?
2
by: Steve | last post by:
I am new to this newsgroup & to .NET in general. I have been playing around with Visual Studio .NET, building and rendering web pages using VB "code behind" files. My problem / question is; How...
2
by: Michael Tissington | last post by:
How do I specify the CodeFile for my Global.asax file ? According to the documentation I can use the CodeFile attribute with Application, however when I try to use this I get an error saying that...
11
by: Ron | last post by:
I have a web project compiled with the new "Web Deployment Projects" plugin for VS2005. I'm deploying the web project to one assembly and with updateable option set to ON. When I'm running the...
4
by: Al Santino | last post by:
Hello, I've created a simple C# web services project using Visual Studio 2005. My service compiles and runs correctly when called by remote clients. I'm able to step through the service in the...
16
by: thefritz_j | last post by:
We just converted our VS2003 1.1 VB web project (which was working fine) to VS2005 2.0 and now I get: Parser Error Message: Could not load type '<Namespace>.'. Source Error: Line 1: <%@...
8
by: Rob T | last post by:
When I was using VS2003, I was able to compile my asp.net project locally on my machine and copy it to the production server and it would run just fine. I've now converted to VS2005. The project...
15
by: =?Utf-8?B?UGF0Qg==?= | last post by:
Just starting to move to ASP.NET 2.0 and having trouble with the Global.asax code file. In 1.1 I could have a code behind file for the global.asax file. This allow for shared variables of the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.