Using ASP with VB Components
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:
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 = "firstname.lastname@example.org" MyApp.Update
MyApp.Update "Sam", _ "<a href="mailto:email@example.com">firstname.lastname@example.org</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: email@example.com or use our online form at http://www.vbsquare.com/feedback.htm
Page 4 of 4