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

5 Replies
1060 Views

Similar Articles

[PageSpeed] 0
Get it on Google Play
Get it on Apple App Store

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
 nacoda wrote:
Why are direct answers almost NEVER given on this forum?  Partial indirect solutions are always posted.


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:

Similar Artilces:

Questions about global variables in Global.asax.cs
When creating global variables in the Global.asax.cs file for my application so that every other webform page can access them, when should I choose to 1) use the Application object as supposed to just 2) creating public properties for the Global class?For example:1) Declaration & Initialization (Global.asax.cs):   Application["File Name"] = "myFile.txt";                                   &nb...

global variable in global.asax
Hi!! Can any one point out the syntax and positioning of declaring a global variable within the global.asax file and the syntax for using within the c# code. Thanx!!...

Global Variable not global?
PB 8 Evaluation (build 7039). I have a global variable that is set in a response window. The response window is closed, I call a child mdi window through a menu and the value of the global variable 0 (not what I set it to). Am I doing something wrong or is this a bug? Have you ensured another variable with the same name has not been declared elsewhere ? If so, the variable in the local scope was probably set with your value. That's the main reason I can think of why you may not see your updated value. Sanjiv. "Anthony" <anthony@forumsonly.com> wrote i...

Global Variables vs. instance variables declared at application object level
There is only one application object instance during the application runtime that is instantiated at the start along with the global variables, so I am looking for what are the major differences, advantages , and disadvantages between Global Variables and instance variables declared for the application object. Thanks I've wondered also about the application instance variables. I think the difference with global vars is very subtle. Maybe too subtle to be of any use. Anyway, you shouldn't be using either one of them :-) Geert 'Darling' Van Damme Moh...

Declaring Global Variables and Global External Function By PowerScript
Does anyone can tell me, can I declare the global external function / global Variables by PowerScript ( such as, within the application's open event ), not in 'declare painter' ??? Thanks Samkola No. They need to be declared within the Global Variables painter. Ron Gallagher, CPDP Atlanta, GA rongallagher@mindspring.com samkola wrote in message <01beee19$8715d2a0$2d01a8c0@testing>... >Does anyone can tell me, can I declare the global external function / >global Variables by PowerScript ( such as, within the application's open >event ), not i...

Global.asax bug in VS2008 WebSite 3.5 project
I've just installed VS2008 and created a new WebSite project based on .NET 3.5. I've added Global.asax via (right click) context menu, but I get only a Global.asax file and not Global.asax.cs. Also, Global.asax I can't edit, delete... anything. It's completely untouchable while it containst basic method skeletons and languge Page directive in the header. Help! Hi toncij, As far as I know, the .net do't generate the Global.asax.cs file by default. And it take the inline way, you can write the code in Global.asax. If you want to use Global.asax.cs, you can ...

How do you declare a GLOBAL variable in VB.net
Does anyone know how to do this?  Thanks, NewKid Global variables in .net 2.0 dont really exist the way they used to in C++ for example, because you dont have a single instance class running.  If you are in the ASP.NET model, you can use the global.asax file to declare a Public or Public Shared variable, or you can use Sessions as your global variable holder. If you are in a VB model for win forms, Your best bet is to declare a class, and put a public member in it that will hold your value, and instanciate it when the application starts.Bryan SampicaAuction Site StarterKit |M...

Questions: Global Connection String, Global Mail Server Variables
Hello everyone, I Recently had the misfortune of having my server crash. I am currently transferring all the websites off the hard drive onto another server. My Question: Where can I store global variables for websites such as a connection string or mail server? I will be going through all the websites and updating all the connection strings in my aspx pages (the sql server has moved).  Also I will be updating all forms that send SMTP mail messages. (the mail server has moved, and it is not localhost).  I probably have to update 100+ pages so since I am goi...

Very basic Global.asax variable question.
Hello I have a Very basic Global.asax variable question. If we create a variable in Global.asax file. Will it available to dirrerent users or only one user. Like if we create a session var it is only available for one user. but if we create a variable in the Global.asax file...all users can access that variable? A Session variable is only accessible by the current user while the variable set within your Global class will be accessible to all users.Thanks, EdMicrosoft MVP - ASP/ASP.NET sahajMarg: Hello I have a Very basic Global.asax variable question. ...

Missing Global.asax and Global.asax.vb
Hello all, I am following this example for form authentication (http://support.microsoft.com/kb/326340) and It mentions Global.asax and Global.asax.vb files. I did not have these so I found a post about adding the Global.asax as it is not in by default in VS 2008. The issue is that there was not an accompianing Global.asax.vb file. I tried adding a class and naming it Global.asax.vb but somehow I do not think that it is going to work. Any thoughts?   Thanks,   Ty There is no code behind for Global.asax the code goes within the markup Thanks for the information. That helps...

Can I create global session and app variables in Global.asax
Just wondering how I can use the Global.asax file. I wondered if I could put all my session and app variables in one place and would this be the way to do it. Code sample appreciated. There are some details on working with the Global class in this article, as well as some additional links and resources. Scotthttp://www.OdeToCode.com/blogs/scott/...

Global variable declaration in vb.net winforms
Hi all Can anyone please tell how to declare global variable in winform. I want to declare one variable at start of a application and use in different form. eg. i want to store userid/usertype and check in each form this variable across the application. Also please tell me how to use in each form Thanks in advance Rashid Windows Forms forums at http://forums.microsoft.com is more suitable place to ask WinForm related questionsThanks,Teemu KeiskiFinland, EU With Visual Basic, it's fairly easy to create global variables in winforms.  Modules are the usual method.  As long as it...

declaring session variables in the global.asax file
hi everybody, was wondering if anyone could help me, i have been trying to declare a bunch of sessiion variables in the global.asax file, but whenever i try to call them, it wil not work... code for the asax file is below... (it's written in c++ but is so simple that a VB guy could help me) <script language="C#" runat="server"> void Application_Start(Object sender, EventArgs e) { //nothing } void Session_Start(Object sender, EventArgs e) { Session["sessionid"] = Session.SessionID; Session[&qu...

Advice: not global variables but directory-level variables
I'm working on a .net 2.0 web site where basically each directory represents a separate implementation of a basic template. I have a few bits of information I want to be available to every page in that directory (and different for each directory) - String or Boolean variables. The values of these need to be easily human-editable.  What is the best way to set this up? I'm considering adding a few keys to a web.config file for each directory. Does this sound like a good idea to anyone here or is there a better way you've seen or done?  Thanks! Soun...

Web resources about - Quickie question: Declaring a global-level variable with Global.asax - asp.net.state-management

Facebook Begins Rolling Out Country-Level Analytics For Global Pages Via Graph API, FQL
When Facebook announced its global pages initiative in October, it promised page administrators that it would provide country-level analytics, ...

Global CO2 Levels Tick Past 400 Parts Per Million - YouTube
Reversing concentrations of carbon dioxide in the atmosphere would take an unprecedented worldwide effort, say NOAA researchers. Follow Sebastian ...

Level Global Co-Founder Gets Prison
Hedge Fund Level Global Investors co-founder Anthony Chiasson is also ordered to pay a $5 million fine for insider trading.

Level Global's Anthony Chiasson Gets 78 Months Prison
Anthony Chiasson left the federal courthouse in lower Manhattan looking like he was late for a meeting rather than receiving 78 months in federal ...

Tom Murphy: Understanding Community Development on a Global Level
I had the chance to speak with Lara Crampe, Development Officer for IIRR. She has proposed to give the talk titled "Sex, Slacktivism, and How... ...

For Level Global, F.B.I. Raid Is a Final Blow
No one at Level Global has been accused of any wrongdoing, but its reputation has been damaged by the F.B.I. raid and it has closed.

How mPOS Morphs At A Global Level
mPOS isn’t so much focused on micro-merchants anymore, according to Benoit Boudier, SVP of International Sales for ROAM. Rather, it’s become ...

Alarming global levels of antibiotic resistance
In the most detailed and comprehensive look at antibiotic resistance so far, a Washington-based research group last week reported alarming antibiotic ...

In 2014 unbelievingly oncology spending at global level reached $100 billion
Reports have proved that therapeutic activities with respect to cancer have improved a lot but side by side spending has also reached a height ...

Ex-Level Global Analyst Testifies on Dell Secrets
... witness testified Tuesday that he passed on corporate secrets about Dell's financial outlook to his boss at hedge-fund firm Level Global.

Resources last updated: 1/18/2016 10:20:13 PM