Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Global Variables (Global.asax)

Status
Not open for further replies.

nevets2001uk

IS-IT--Management
Jun 26, 2002
609
GB
Hi.

I want to create some global variables for my ASP.NET application. I believe I can set them in the global.asax file. I am currently using this code for the file...

<Script Runat=&quot;Server&quot;>

Sub Application_Start

Dim conParts as OleDbConnection
Dim cmdSelectPart as OleDbCommand
Dim dtrPart as OleDbDataReader

End Sub

</Script>

When I run the pages the variables are creating un-defined errors. Is there something extra I should do? Or am I completely on the wrong track?

Thanks
 
Use Friend or Public rather than dim
And you don't need to put them in Global.asax - any class or module will do.



Mark [openup]
 
ASP.NET do not support the PUBLIC variable decalarion in a module just like what we're used to in VB. intead use the Application State.

Sub Application_Start
application(&quot;var1&quot;) = &quot;value1&quot;
application(&quot;var2&quot;) = 2
application(&quot;var3&quot;) = false
application(&quot;IsUserAuthenticated&quot;) = false 'very handy
End Sub

just like a session object, but this one fires whenever a user attempts to view any page of your virtual directory.

i noticed on your example that you want sql connections be globally, im not sure if that's possible but i think that it's not appropriate since you could hang the connection to sql server forever or for the time of your application running. which is of course, not good.

hope this helps.
 
Cheers! I forgot all about this post! I did eventually do it by just declaring the variables before the subs at the top of my asp.net code. Think I was explaining the initial problem in a bit of a round about way.

Thanks for everyones helps tho.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top