Quickie question: Declaring a global-level variable with Global.asax

My instructor told me it might be best to create an SQL connection in global.asax instead of making new connections for every form I create.

However, when I declare the variable in Session_Start, it still doesn't see the variable on the other form I have. Obviously there is something I'm not getting here.
So my question might sound trite, but I simply need to create an SQL connection variable that can be accessed from any of my .aspx pages.
Any takers?
0
generaltao
1/20/2005 4:57:56 PM
📁 asp.net.state-management
📃 8807 articles.
⭐ 0 followers.

💬 5 Replies
👁️‍🗨️ 1060 Views


Your professor does not understand ASP.NET and connection pooling.  Creating a single connection in Global.asax and using it on all pages is a terrible idea.  ASP.NET has connection pooling built in, so to leverage that, open the connection as late as possible, and close it as early as possible (returning it to the pool).  In most cases, rather than a totally new connection, your call to .Open will instead efficiently retrieve one from the pool.

If you really want to save something for use in the session, save it in the Session collection. Saving the open connection in the Session collection is a terrible idea.

Starting with ASP.NET 2.0?  Look at:
Programming Microsoft Web Forms
My Blog
0
douglas
1/21/2005 1:33:56 AM

Why are direct answers almost NEVER given on this forum?  Partial indirect solutions are always posted.
As to declaring global variable that are accessable to all page, here is a simple solution I had to figure out myself.
Imports Microsoft.VisualBasic
Public Class CGlobal
    Inherits System.Web.UI.Page
    Public Shared USRID As String

    Public Sub SetUSRID(ByVal id As String)
        USRID = id
    End Sub

    Public Function GetUSRID() As String
        GetUSRID = USRID
    End Function
End Class
0
nacoda
9/9/2005 4:05:54 PM

            
<table width="85%"> <tr> <td class="txt4">  nacoda wrote: </td> </tr> <tr> <td class="quoteTable"> <table width="100%"> <tr> <td width="100%" valign="top" class="txt4">Why are direct answers almost NEVER given on this forum?  Partial indirect solutions are always posted.</td> </tr> </table> </td> </tr> </table>


Hmmmm.  I've been on these Forums for over 2 years now and I have not found that to be the case.  People who haven't gotten the help they've needed come back and ask more questions to the point where they are satisfied.  Often people just need a push in the right direction, not every single line of code.  After all, Google is an excellent resource for articles and code, and once you know what you are looking for, have the right terminology and an idea of what you need, it is much easier to help yourself.

Not to mention that most people answering questions here are developers who are using their own time to try to help other developers, and posting complete solutions to things is time-consuming, especially when other resources are available.

I'm sorry you were not able to find what you were looking for.  Had you tried posting a question on the Forums?  Did you try the Search feature?

Terri Morton
Engagement Manager, Neudesic

How to ask a question

0
tmorton
9/9/2005 5:26:33 PM
I agree with the moderator that you
do not want to go the global.asax route. My preferred way of handling
data string and connections is the following. First I put the conn
string in the machine.config or web.config file as a configuration
value. The main reason for putting it here is for the ease of changing
it if needed w/o having to recompile the app. When it is time for my
app to get data, it usually calls a data class that create the
connection in its constructor with the connection string stored in the
config file. This way as long as the data class is in use I have the
connection.

0
aspwhiz
9/9/2005 5:59:21 PM
Nacoda,

The original message was as follows:
"My instructor told me it might be best to create an SQL connection in global.asax instead of making new connections for every form I create.
However, when I declare the variable in Session_Start, it still doesn't see the variable on the other form I have. Obviously there is something I'm not getting here.
So my question might sound trite, but I simply need to create an SQL connection variable that can be accessed from any of my .aspx pages.
Any takers?"

My answer to this question, I feel, was a direct response to the question.  Creating a session global connection in global.asax is a terrible idea.  That the questioner could not get it working was almost a bit beside the point.
More importantly, your solution is not what the questioner was asking for either.  Note that the original question referred to a session specific variable, your variable will be application global.
It is unfortunate that sometimes the answer to questions is not as direct as folks would like.  The sad fact is that the world is messy, and sometimes there is not the sort of direct answer you would like.
I appologize if my answer was not satisfactory for you.

Starting with ASP.NET 2.0?  Look at:
Programming Microsoft Web Forms
My Blog
0
douglas
9/9/2005 6:02:05 PM
Reply: