Home
Link To Us
Components
Resources










Site Menu

An Overview in Creating an ActiveX DLL for Use with ASP Scripts

Setting up a DSN-Less Database Connection

Search the Web for other ASP Tutorials

A Note From the Author

cover
A Good ASP Database Resource
More Resources




















Top Ranked
Reference Books









One more acronym please!

Remember I said that there are three strengths to BASIC, and I only mentioned two. Well, here’s the last and a very important one. Within the last couple of years a language has beendeveloped known as Visual Basic Script, or VBScript that can run programs through web browsers such as Netscape, Opera, and Microsoft Internet Explorer. This makes it easy to transport some programs developed in Visual Basic to the web and vice-versa. Not all features are supported in VBScript. For instance the ability to read and write files through the web browser is extremely limited so that violations of security, viruses are more difficult to program and are not as frequent. Database programs, however, are easy to transport and benefit the most in conversions. This allows for data in a database is now accessible worldwide instead of companywide.

How do I do it?

VBScript is readable by web browsers as ASP (Active Server Page) files on a Microsoft Internet Information Server (version 3.0 and later of course). Instead of index.html we now have index.asp. The languages that we must be familiar with also now change. We must still be familiar with Visual Basic (ok, actually VBScript) and SQL, however, we just added HTML (Hyper Text Markup Language). Note: HTML is a fairly simple language to learn. If you would like more information, there are many good primers on the web.

In Visual Basic we have forms, now our forms in a small sense become web pages written in HTML. The way it works is that the scripts are executed and it sends the information to the web browser in HTML form. None of the scripting language source is viewable when it is sent, only the HTML. For instance, here is an extremely short ASP program that will demonstrate the HTML output.

<%@ LANGUAGE="VBScript" %>
<% 
Option Explicit Dim i For i= 1 to 5 %> <font size=<%=i%>>Hello</font>
<% Next %>
When run in your web browser this output will result:

Hello
Hello
Hello
Hello
Hello

If you choose to view the source the code looks like this:

<font face="arial" size=1>Hello<br></font>
<font face="arial" size=2>Hello<br></font>
<font face="arial" size=3>Hello<br></font>
<font face="arial" size=4>Hello<br></font>
<font face="arial" size=5>Hello<br></font>
The code at the beginning: <%@ LANGUAGE="VBScript" %> denotes that the language that will be used within this ASP is Visual Basic Script. This line must be the first line of the code. The script part of this page is contained within the <% %> brackets. The HTML is the rest of the surrounding code. The value of the variable i is printed in HTML by using the <%=i%> command. It is important to note that in Visual Basic Script all variables are variants. This means that no variable can be defined as a integer, string, etc.

Bring on the Databases

Like I said before one of the greatest functions of Visual Basic is its ability to work with databases. VBScript is also very easy to set up to access and perform database operations. Once again we begin by setting up the variables and connection to the database.
<%@ LANGUAGE="VBScript" %>

<%
Option Explicit

Dim Connection
Dim RS
Dim SQLStmt
Dim SSN, Firstname, Lastname

Set Connection = Server.CreateObject("ADODB.Connection")

Connection.Open "DSN=Students; UID=sa"
The Connection variable is set to the ADO (Access Data Object) Connection type object. Once that has been set we can open the connection through the same DSN that we set up earlier. There is no need to duplicate the ODBC data source. Now we can set up our SQL query exactly the same way it would appear in Visual Basic. This query will select all in the information contained in the social security number, first name, and last name fields in the database and then order it in alphabetical order by lastname.
	SQLStmt = "SELECT SSN, Firstname, Lastname "
	SQLStmt = SQLStmt & "FROM Students "
	SQLStmt = SQLStmt & "ORDER BY Lastname "
We then set the variable RS to execute this SQL Query which is similar to the OpenRecordset function of Visual Basic.
	Set RS = Connection.Execute(SQLStmt)<br>
%>
Following this we have the written the page in normal HTML. This simply sets up the look of the beginning of the page before the data is sent from the database.
<html>
<title>Administration Student Update</title>
<BODY BGCOLOR="FFFFFF">

<hr>
<font face="arial" size=2>
<h1>Student Information</h1>
<h2>Here are the students in the Database.

Click a name to update the student information:</h2>

We then set up a Do/While loop to go through all the records in the database until EOF or the last record has been reached. This is accomplished by using the RS variable with the .Fields reference. The records are moved through by utilizing the .MoveNext function.
  <%Do While

Not RS.EOF%> Name:<ahref="details1.asp?SSN=<%= RS.Fields("SSN") %>">
<b><%= RS.Fields("Lastname")%>, <%= RS.Fields("Firstname") %></b></a>

<%   RS.MoveNext      Loop %>

We then finish the page with normal HTML and we close the connection.
<hr>
</body>
</html>

<%	Connection.Close %>
In a few lines of code we have written a program to pull data from a SQL Server database. One thing you may have noticed is the ? after the hypertext link to the file details1.asp. This sets up a query string within the link. A query string sends data through the URL location (basically the address of the page) to the next page that can interpret this information. In this case the string that is sent becomes SSN=A Social Security Number in the database. Example: SSN=123456789. By taking a look at details1.asp we can see how the information is used.
<%@ LANGUAGE="VBScript" %>

<%
Option Explicit
Dim UID, SQLStmt, Connection, FirstName, LastName
Dim RS, SSN, Major, GradDate

	SSN = Request.QueryString("SSN")
	Set Connection = Server.CreateObject("ADODB.Connection")

	Connection.Open "DSN=Students; UID=sa"

	SQLStmt = "SELECT * "
	SQLStmt = SQLStmt & "FROM Students "
	SQLStmt = SQLStmt & "WHERE SSN = '" & SSN & "' "
	Set RS = Connection.Execute(SQLStmt)

%>

<html>
<title>Administration Student Update</title>
<BODY BGCOLOR="FFFFFF">

<hr>
<font face="arial" size=2>
<h1>Student Information</h1>

<%	Do While Not RS.EOF%>
<h2>Here is the Student Information for 
<%= RS.Fields("Firstname") %> <%= RS.Fields("Lastname") %>:</h2> <b>First Name:</b> <%= RS.Fields("Firstname") %><p> <b>Last Name:</b> <%= RS.Fields("Lastname") %><p> <b>Social Security Number:</b> <%= RS.Fields("SSN") %><p> <b>Major:</b> <%= RS.Fields("Major") %><p> <b>Graduation Year:</b> <%= RS.Fields("GradDate") %><p> <% RS.MoveNext Loop %> <hr> </body> </html> <% Connection.Close %>
The value of the variable of SSN in the query string was passed onto this page and then placed in the new Visual Basic script variable SSN by the function Request.QueryString. It was then processed in a SQL Query and more information on the record was extracted from the database.

By using query strings information can continually be passed between web pages on a site so that information can be retained.

One of the many beauties of ASP is that the web pages will never increase in size as the database grows as they are dynamic. Data can change and the outputted web pages will change, but the original ASP files will stay the same. Another plus about ASP is that if the script is run on the server as all the scripts demonstrated do, the programs can be used on various platforms. As long as the browser can read the HTML, there is no limit on operating system or machine. Also, since all the ASP files are generally in one location all the files can be updated easily, so there are no patches or version updates that need to be released to customers. ASP is currently being used on E-Commerce sites and other database sites and it is quickly growing.

For a good reference book check out:

cover

If you found this information useful please contact me at webmaster@tutorial-web.com

© 1999, 2002 Derrald Farnsworth-Livingston Web Development


Sections

Section 1: Setting it all Up
Section 2: SQL and Stuff
Section 3: VBPower!
Section 4: ASP Results
Printable Version

The Bookworm
Search the Tutorial-Web library for books on any topic!
Sponsors

Search Now:
In Association with Amazon.com

Enter your email address to join our mailing list!
We will NOT sell your name to advertisers