Beginners Guide to COM in ASP.NET
As in all of the tutorials here, once you see what needs to occur, in order to use COM objects in your ASP.Net code, you will see that it’s not all that complicated. There are, however, a few things that do need to be put in place before it will happen.
The first item to be addressed is a property that needs to be added to the Page Directive:
ASPCompat=”True”
In case you don’t know about the page directive, you really ought to look into it because it’s very useful in many ways. It’s added at the beginning of your page, like this:
<%@ Page Language=”VB” Debug=”True” AspCompat=”True” Trace=”True” %>
The next thing that’s needed is quite important. To do this, the identity needs to be set to the ASPNet user account on the web server. In Win2K, go to Component Services, click on the main entry in the tree, where your COM objects are located, and click ‘Properties‘. Next, click the ‘Identity Tab‘. Here, under the line, ‘This application will run under the following account’, you will see two Account choices:
- Interactive user – the current logged on user
- This user
Since the ASPNet user is the one who needs access to the COM objects, here, you can choose ‘This user‘, and Browse to the ASPNet user account – then click ‘OK’.
Keep in mind, if you upgrade the Framework to a newer version, AFTER you follow these steps, you might need to go back into the Windows User accounts and reset the password (usually blank) for the ASPNet user account. The System password and the password entered in the ‘Identity Tab‘ must be the same. You will know whether or not you need to do this fairly easily. You will start getting identity errors, asking you to check the username/password.
There is also a slight difference in declaring and instantiating COM objects between Classic ASP and ASP.Net. In Classic ASP, you would merely add this to your code:
Dim MyObj
In ASP.Net, you must include the type, so you use ‘Object’:
Dim MyObj as Object
For instantiation, in Classic ASP, you would use:
Set MyObj=Server.CreateObject (“ObjectName.Class”)
In ASP.Net, you ‘Set’ is no longer available, so you do it like this:
MyObj=Server.CreateObject (“ObjectName.Class”)
Let’s say, that in the MyObj dll, there’s a method called GetRecords, for this example, which returns a recordset. In order to call this method in ASP.Net, you would do it like this:
Dim objRS as Object objRS = MyObj.GetRecords(arguments go here)
In order to display any recordset results from methods in your COM object, for someone who’s worked with SQL Server and MS Access, this next part may seem a little strange. It doesn’t matter whether or not your COM object uses SQL Server or not, you will need to use an OleDbDataAdapter. It’s the only one that has ‘ADODB.RecordSet’ available as an argument in the Fill method. Keep in mind, at this point, your code is not directly accessing SQL Server – it’s accessing the COM object. Here’s an example of how you could display your results in a DataGrid, and have the Record count available for use also:
Dim myDataSet as DataSet = new DataSet() Dim adapter as OleDbDataAdapter = new OleDbDataAdapter() Dim count as Integer = adapter.Fill(myDataSet, objRS, "ADODB.RecordSet") MyDataGrid.DataSource = myDataSet.Tables(0).DefaultView DataBind() if count>0 then lblRows.text="Records Found = " & count & "" End If
Of course, just like in Classic ASP, you must dispose of your objects, but remember, there is no ‘Set’ in ASP.Net:
MyObj=Nothing objRS=Nothing
That’s all there is to it. As with all the tutorials on ASPNet101.com, you can see that this was fairly painless, and definitely not as hard is it may have seemed to begin with.




14. Jul, 2007 








No comments yet... Be the first to leave a reply!