Name

CreateObject

Synopsis

objvar = Server.CreateObject(ProgID)
objvar = Server.CreateObject(Type)

Returns a reference to a COM object created based on the supplied ProgID.

Parameters

objvar

A variable of type Object to receive the reference to the newly created object.

ProgID

A String variable or literal containing the COM programmatic ID of the desired object.

Type

The type name of a runtime callable wrapper (RCW) class that the tlbimp.exe utility generated to expose a COM object to managed code.

Example

The code example declares an object variable, uses Server.CreateObject to assign a reference to a newly created ADO Recordset object to the variable, and then opens the recordset and returns the RecordCount property to the browser by using the Message label control:

Sub Page_Load(  )
   Dim rs1
   Dim ConnString As String
   ConnString = "driver={SQL Server};server=(local)\NetSDK;"
   ConnString =  ConnString & "database=Pubs;Trusted_Connection=yes"
   rs1 = Server.CreateObject("ADODB.Recordset")
   ' 1 and 3 are the values for adOpenKeyset and adLockOptimistic
   rs1.Open("SELECT * FROM Authors", ConnString, 1, 3)
   Message.Text = "There are " & rs1.RecordCount & _
      " records in the Authors table.<br/>"
End Sub

Figure 18-1 shows the output of a page that combines the example for the CreateObject method with the example for the CreateObjectFromClsid method.

Output of CreateObject and CreateObjectFromClsid examples

Figure 18-1. Output of CreateObject and CreateObjectFromClsid ...

Get ASP.NET in a Nutshell now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.