How To Access Session and Application Variables from Within a Visual Basic Component (230149)



The information in this article applies to:

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

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

  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:

175167 How To Persisting Values Without Sessions

231282 INFO: Stress Tools to Test Your Web Server

244457 How To Debug an MTS Visual Basic Component Running Under ASP


Modification Type:MinorLast Reviewed:6/29/2004
Keywords:kbASPObj kbCodeSnippet kbhowto KB230149