HOWTO: Access Session and Application Variables from Within a Visual Basic Component

ID: Q230149


The information in this article applies to:
  • Active Server Pages, included with:
    • Microsoft Internet Information Server 4.0
  • Microsoft Visual Basic Learning, Professional, and Enterprise Editions for Windows, versions 5.0, 6.0


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

  1. Open a new ActiveX DLL project.


  2. Set a reference to the Microsoft Transaction Server (MTS) Type Library (Mtxas.dll).


  3. Set a reference to the Microsoft Active Server Pages Object library (Asp.dll).


  4. Rename the project as prjMTS and class as clsMTS.


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


  6. 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
    
    %> 


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



REFERENCES

For additional information, click the article numbers below to view the articles in the Microsoft Knowledge Base:

Q175167 HOWTO: Persisting Values Without Sessions
Q231282 INFO: Stress Tools to Test Your Web Server

Additional query words:

Keywords : kbASP kbASPObj kbCOMt kbVBp kbGrpASP kbDSupport
Version : WINDOWS:5.0,6.0; winnt:4.0
Platform : WINDOWS winnt
Issue type : kbhowto


Last Reviewed: January 7, 2000
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.