Microsoft & .NETVisual BasicUsing ASP with VB Components

Using ASP with VB Components

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Active Server Pages (ASP) has been around
for sometime now. The main sticking point for users is that it only
runs on Windows NT (well, you can run it on Unix using ChilliASP, but
that’s not the point I am trying to make) Unix/Linux make up the bulk
of internet servers, and so most users are stuck with CGI and PHP.
That means one thing – not much information on ASP. This article will
show you how to use ADO in ASP, and how to use your own VB components
in ASP.

ADO (ActiveX Data Objects) is relatively
new to the database world, and I have to say, not many people like
it. But aside from that, a lot of people use it, mainly because it
is flexible around using different datasources. Thus, Microsoft
included ADO with the ASP server installation which means you need
to know the basics if you are to really get the most out of using
ASP.

If you know HTML, or at least have
looked at any HTML code, you will know that you can embed scripts in
it; VB Script and Java Script are prime examples. Well, ASP script
can be embedded within an HTML document. This makes life a lot
easier, because we don’t have to rewrite all of our web sites in
another language. ASP is really like using VB Script to interact
with databases (well, mainly with databases).

OK. ASP script is always found inside
these tags: <% ASP CODE %> Your ASP code can span multiple
lines, or can just be a few characters long. Anyway, getting back to
using ASP and ADO. If you have ever programmed with the DAO (Data
Access Objects) object model then you shouldn’t find it too
difficult to use ADO (well, for the basic features anyway).

The main objects that you will be using
in your ASP code are the Connection and Recordset objects. You use
the CreateObject method to create a connection object. You will need
a local Data Source Name (DSN) connection on the web server to your
database, because you need to pass the DSN when you connect to the
database. Other than that, the Recordset object is quite simple. The
BOF and EOF (Beginning Of File and End Of File) properties can be
used for loops, and that is about it.

I will now take you through a sample
ASP script that uses ADO to write the contents of a recordset to
an HTML page. The first line is to create a connection object:

<%
Set Conn = Server.CreateObject("ADODB.Connection")

Now open a DSN, in this case People,
and execute an SQL (Structured Query Language) query to create a
recordset object:

Conn.Open("People")
Set RS = Conn.Execute("SELECT * FROM Persons")

Simple enough. We now need to iterate
through all the records in the Persons table, and write then, in a
very simple way, to the HTML page:

If Not RS.EOF Then
Do
Response.Write RS("PersonFName") & " " & RS("PersonLName")
RS.Movenext
Loop Until RS.EOF
End If

In the first line, we check that the
table is not empty, and we then proceed to loop through the
Persons table writing each PersonFName and PersonLName field to
the HTML file. The Response.Write statement will write the
information at the point where the ASP script is located. E.g. If
above your ASP script you have header table, then the names
will be written beneath that table.

Simple. But how can this help me in my
Visual Basic development? Well, lets see.

Just think about it. When you use
ADO in VB, you are referencing the ADO DLL. When you are using
ADO in ASP, aren’t you doing the same thing? So what you can do
is to write your Visual Basic code in a VB DLL, compile,
register it on the web server and use it in your ASP code.

Let’s do an example. Say you have
written a component in VB that allows you to send email with a
randomly generated number. You could use that component (in the
form of an ActiveX DLL) from within your ASP/HTML code to send
an automated response to a user who had entered their email
address. Sounds great doesn’t it? Well, it’s quite easy to do.

Firstly, make sure that the
functions used to send the email are in a class module, and
that its Instancing property is set to MultiUse. Now compile
your code into a DLL, and register it on the web server. You
now need to write some ASP code that will create an instance
of the class, and pass some values.

As with ADO, you can use the
CreateObject to create an instance of your class:

Set AutoMail = Server.CreateObject("MyMail.CEmail")

Notice that I have referenced the
DLL name as MyMail and the class as CEmail. You will need to
change these as appropriate.

Anyway, now that you have the
instance of your class, how do you get the values the user
entered from another web page? Well, ASP comes to the rescue again.
On your first HTML form where the user enters the values, set
the <form> tag to point towards an ASP page:

<form method="post" action="confirm.asp">

Now, make a new HTML page and save
it as confirm.asp. Start by adding the code above to create an
instance of the class, and then add some code to get the
values. The ASP object model gives up a useful method to get
values that were passed to the page. The method is Request.
Pass a parameter of the name of the form item, in our case,
the text box where the user put their email address in. Here
is the HTML code for the text box:

<input type="text" name="email" size="20">

We will use the Request method to
get the value of that text box. For this example, we also want
to get the users name, so we will request the ‘name’ value
from the text box on the previous HTML page.

If you wanted to request a value
passed in the url, for example, you might want to retrieve the
ID value from the following url:

http://www.myserver.com/mypage.asp?id=6 

You would use the Request.QueryString method passing the value name as an
arguement:

lngID = Request.QueryString("id")

So lets round up what we’ve done
so far: We create an instance of the class and request
the email and name values from the form page. The next thing
we need to do is to pass the values to the component (in the
form of the class module) to allow it to send the email. This
brings us to a point that I want to discuss; should we pass
the values as arguements or properties? My natural move would
be to use properties, but when we are creating a web based
application with mainly server side processing over some kind
of a network, we are going to pass them as arguements. This
scenario is known as ‘maintaining state’ or in our case, not
maintaining state.

I will explain why it is bad to
maintain state. Firstly, when making any program/component
that uses some kind of a network, whether it be a Local Area
Network (LAN) or the Internet, we need to think about traffic.
This means we need to consider how many users will be using
the component, and how well your server(s) can cope with the
demand. If you only have one or two users operating over the
network then maintaining state could be done. But if you are
using the Internet or an Intranet with many users frequently
sending and receiving data over the network, you want to
minimize the amount of data you send.

OK. But don’t I still send the
same amount of data whether I pass the values as arguements or
not? Well, yes and no. Take a look at the following example:

MyApp.Name = "Sam"
MyApp.Email = "sam@vbsquare.com"
MyApp.Update

or

MyApp.Update "Sam", _
  "<a href="mailto:sam@vbsquare.com">sam@vbsquare.com</a>"

The first example sends three
‘packets’ of data across the network. The second example only
sends one packet across the network. Although the packet maybe
larger, because it goes only once, it decreases network
traffic. Using this information, we should try to minimize the
amount of network usage and therefore avoid maintaining state.

We can now assemble our VB class
to take the values as arguements and then pass then from the
ASP script all in one call. All we need to do now is to tell
the user that we have successfully sent them the email. This
can be done using the Response.Write statement that we came
across earlier. Below is the code that I have described in the
last few paragraphs. In short it: creates an instance of the
class module, retrieves the form values, inputs the values in
one call to the class and finally writes some HTML to tell the
user that the email has been sent:

<%
Set AutoMail = Server.CreateObject("MyMail.CEmail")
strName = Request("Name")
strEmail = Request("Email")
AutoMail.SendMail strName, strEmail
Response.Write "You have been sent an email at " & strEmail
Set AutoMail = Nothing
%>

Easy. Note at the end that we set
the AutoMail variable to Nothing, just to release any memory.

Well, that’s pretty much about
it for this article, but I have build a small sample project
for you to download. It includes a full set of ASP pages and
HTML forms that demonstrate what I have shown here, along
with a VB DLL (with source) that will use the Microsoft
Active Messaging 1.1 (available from ftp://ftp.microsoft.com/services/technet/samples/boes/bo/mailexch/exchange/appfarm/actmsg.exe)

Please, tell me what you thought
of this how to. Was it useful, useless? Too detailed, not
enough code? Are you using ASP and ActiveMessaging? Tell me:
sam@vbsquare.com or
use our online form at http://www.vbsquare.com/feedback.htm


Download the files

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories