Microsoft KB Archive/230149

= How To Access Session and Application Variables from Within a Visual Basic Component =

Article ID: 230149

Article Last Modified on 6/29/2004

-

APPLIES TO


 * Microsoft Active Server Pages 4.0, when used with:
 * Microsoft Internet Information Server 4.0
 * Microsoft Visual Basic 5.0 Learning Edition
 * Microsoft Visual Basic 6.0 Learning Edition
 * Microsoft Visual Basic 5.0 Professional Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 5.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition

-



This article was previously published under Q230149



SUMMARY
This article demonstrates how to access the session and application variables declared in your Active Server Pages (ASP) page from within a Visual Basic component.

A common scenario is reading user ID and passwords stored in session variables from within a component to avoid passing them as parameters. Note that your application becomes more scalable by avoiding state in Session (see the REFERENCES section in this article for more information). With this in mind, stress test your application before you move you application from development to production.



MORE INFORMATION
 Open a new ActiveX DLL project. Set a reference to the Microsoft Transaction Server (MTS) Type Library (Mtxas.dll). Set a reference to the Microsoft Active Server Pages Object library (Asp.dll). Rename the project as prjMTS and class as clsMTS.  Copy the following code to the clsMTS: Dim objApplication As Object Dim objSession As Object

Public Function GetVar As String

Dim objCtx As ObjectContext Set objCtx = GetObjectContext

Set objApplication = objCtx.Item("Application") Set objSession = objCtx.Item("Session")

GetVar = objApplication("Var1") & objSession("Var2") & "..."

End Function   Create a blank new ASP page under one of virtual directories and add this code to it: <%

Application("Var1") = "Where do you want" Session("Var2") = "to go today ?"

Dim obj Set obj = Server.CreateObject("prjMTS.clsMTS") response.write obj.GetVar

Set obj = Nothing

%>                        When you run this ASP page, the variables set in the page are accessed inside the Visual Basic component and the following appears in the browser:

Where do you want to go today ?

</li></ol>

<div class="references_section">