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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Accessing Global.asax Vars

Status
Not open for further replies.

DotNetBlocks

Programmer
Apr 29, 2004
161
0
0
US
Hello All,
Does anyone know how i can access a varible in the global. i have a object called "CDatabase" that i would like to call on my aspx pages. i am trying to find some way that i can delare it in one place and have all of my pages be able to access it.


Thanks,
Babloome


Code:
Imports DBConnection
Imports System.Web
Imports System.Web.SessionState

Public Class Global
    Inherits System.Web.HttpApplication

#Region " Component Designer Generated Code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Component Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Required by the Component Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Component Designer
    'It can be modified using the Component Designer.
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        components = New System.ComponentModel.Container()
    End Sub

#End Region
    [b]Public CDatabase As DBConn.Sql = New DBConn.Sql ("Admin", "Password")
[/b]
    Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)

    End Sub

    Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires when the session is started
    End Sub

    Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires at the beginning of each request
    End Sub

    Sub Application_AuthenticateRequest(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires upon attempting to authenticate the use
    End Sub

    Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires when an error occurs
    End Sub

    Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires when the session ends
    End Sub

    Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires when the application ends
    End Sub

End Class
 
If you declare CDatabase as an Application-level variable then it can be available to everyone in the application.
 
Thank you MDTekUser for your quick response.

Can you please take a look at this. I am trying to create the application-level var but when i try to use it, the intellisense only lets me select from .gettype. What am i doing wrong?

Babloome

Create var
Code:
   Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)

        Application.Lock()
        Application("CDatabase") = New DBConn.Sql ("Admin", "Password")
        Application.UnLock()

    End Sub

Trying to read var
Code:
Application("CDatabase")
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top