|
 |
Even if you are on the right track, you'll get run over if you just sit there.
Will Rogers
|
|
|
|
ASP Programming Fundamentals
©2005
|
|
Kevin's web site, , has carried a number
of tutorials, articles, and Discussion Groups for learning about ASP.Net, ASP and ADO for 6 years now.
He has written for a number of online publications, including Wrox Publications' ASP Today, as well as co-authoring Wrox Publications'
,
with John Kauffman and Thearon Willis, and Wrox's , with John Kauffman and Brian Matsik.
Most recently, Kevin has written several articles for the web site.
|
|
|
Well, campers, it has become painfully obvious to me that my efforts to educate people regarding the use of ASP/ADO has been somewhat unsuccessful, and that is apparently due largely to a lack of understanding concerning basic programming fundamentals. So, the purpose of this article is to give some programming background to those of you who wish to employ ASP/ADO in your webs, but have little or no experience with programming.
What Is Programming?
Simply put, programming is the process of creating a set of instructions for the computer to follow. These instructions are written in a language or languages which people can understand, and then "compiled" (literally, translated) into machine language, or interpreted by the computer from reading a script. HTML is a programming language, as are Javascript and VBScript. The "syntax" of a programming language consists of the various language elements, conventions, and operators that are used to write the instructions.
Procedural and Object-Oriented Programming
The first item of business is to identify the 2 kinds of programming that exist, and how each one fits into the overall picture:
Procedural Programming was the first kind of programming to develop, and involves a relatively simple way of doing things. In procedural programming, the computer is given a set of instructions which it executes, and then waits for input, which it reacts to by executing another set of instructions, and so on.
There are 3 basic elements of procedural programming:
- Sequence - The order in which instructions are executed is the sequence of the programming. This is far more important than it might seem, but having things in the proper sequence is essential.
- Selection - If/else conditional statements and other forms of selection form the second element. This is how a program makes and reacts to choices.
- Iteration - The use of "loops" and other forms of repetitive sets of instructions forms the last building block of procedural programming.
Object Oriented Programming came along later, with the advent of multi-tasking operating systems such as Windows. In point of fact, procedural programming is at the heart of all programming, including obect-oriented, but because certain kinds of objects have many characteristics in common (such as windows, for example), it is more convenient to treat them as objects, rather than as sets of instructions. An object has properties, methods, and event handlers.
- Properties - Properties of an object are the characteristics which define how the object behaves. In a web page, for example, the page itself has certain properties, which are defined in the tag, such as the background color, style source page, etc.
- Methods - Methods are actually blocks of instructions that can be executed by an object itself, and each object has its' own set of methods. A simple example of this would be the submit() method of a form. When You click on the Submit button of a form, or invoke the submit() method for a form with a Javascript command, the form is submitted using the form's submit() method.
- Event Handlers - An "event" is when something happens (duh!), either something that the user has done, or something the program itself has done, or another program has done. The simplest example I can think of for an event is using the form example above. When you click the "Submit" button, you have generated an event, and the event handler is the set of instructions which is programmed to execute when that event occurs. Because of the nature of multi-tasking systems like Windows, one is never sure where the next event is going to come from, so event handlers are designed to react to events in the appropriate manner. Each object has its' own event handlers, built into the object itself.
One last thing that is important to remember regarding object-oriented programming: It employs procedural programming. In other words, an object-oriented program is going to contain both object-oriented and procedural code. An event handler, for instance, may simply be a procedure that executes when the event occurs. This procedure may or may not include object-oriented code.
The program uses an interface for communicating with the user. The interface is a method of the computer talking to the user in a way that the user can understand, and the user talking back to the program in a way that the computer can understand. In a web application, the web pages themselves comprise the interface. The pages contain information which the user reads, and contain active elements, such as hyperlinks and forms, that enable the user to input instructions to the computer. Think of the interface as something like the old Star Trek universal translator: It helps the user and the computer to communicate, even though they think and communicate quite differently.
Putting It All Together
So, how do all of these principles work together in an ASP application? Well, remember that I described HTML, Javascript, and VBScript as programming languages? An HTML page (or an ASP page containing HTML) is a set of instructions to the browser. For example, see this bold word? The HTML for it reads bold. The HTML tags "" and "" are instructions to the browser to make the block of text between them bold. The block of text is actually an instruction for the browser to print that block of text.
A web browser is actually a device for sending and receiving messages to and from a web server on the internet. When the browser receives a file from the server, it reads that file sequentially, and executes the instructions contained in that file, to create the web page that it displays in its' window. This means that it reads all of the HTML as well as any client-side scripting that exists on the page, in the order in which it appears on the page. Javascript commands are executed when they are read by the browser. Javascript function definitions are interpreted and stored, ready to be used when invoked by a command on the page, or an event.
Server-side (ASP) scripting is executed on the server, before the page is sent to the browser, and in fact, does not even get sent to the browser at all. Again, the scripting is executed sequentially, as the page is being read. Since the page may contain a mixture of server-side and client-side scripting, as well as HTML, the scripting is executed when it is read, and the HTML is sent to the browser in the order in which it appears and is interspersed with the ASP scripting.
Consider the following table. The left column is the actual code in an ASP page. The right column explains how the code is interpreted:
|
Set rs = Server.CreateObject("ADODB.RecordSet")
q = "SELECT * FROM mytable"
rs.Open q, "DSN=example;"
%>
|
The first scripting on the page is server-side. It is executed before any HTML is sent to the page. This example opens a query.
|
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
<meta name="GENERATOR" content="Microsoft FrontPage 3.0">
<title>ASP Programming Fundamentals</title>
</head>
<body stylesrc="Default.asp">
<p align="center"><img src="images/taklogo.gif"
WIDTH="600" HEIGHT="96"></p>
<hr>
|
Now comes some HTML. After running the script, the server reads this block of HTML and sends it to the browser.
|
<table border="0"
width="100%" style="border: 2px solid rgb(0,0,255)">
<tr>
<td width="40%" valign="middle" align="left"><font
color="#0000FF"
face="Arial"><strong>Name</strong></font></td>
<td width="40%" valign="middle" align="left"><font
color="#0000FF"
face="Arial"><strong>Subject</strong></font></td>
<td width="20%" valign="middle" align="left"><font
color="#0000FF" face="Arial"><strong>Date
Posted</strong></font></td>
</tr>
|
Here, the HTML for the start of a table appears in the body of the page. This code is simply sent to the browser, where it is interpreted and displayed by the browser.
|
<%y = 0
while NOT rs.EOF%>
|
Now, a loop is about to be executed. Note that the beginning of the loop is all that appears.
|
<tr>
<td width="40%"><font face="Arial"><small>
<a
href="showmessage.asp?recno=<%=rs("recno")%>">
<%=rs("name")%></a>
</small></font></td>
<td width="40%"><font face="Arial"><small>
<a
href="showmessage.asp?recno=<%
=rs("recno")%>">
<%=rs("subject")%></a>
</small></font></td>
<td width="20%"><font face="Arial"><small>
<a href="showmessage.asp?recno=<%
=rs("recno")%>">
<%=rs("date")%></a>
</small></font></td>
</tr>
|
Here, we have a mixture of HTML
and server-side scripting. As each bit of HTML is reached, it is streamed to the browser.
As each ASP instruction is read, it is executed, all in the order in which they appear.
However, all code in this section is executed each time through the loop.
|
<%rs.MoveNext
y = y + 1
if y = 10 then
while NOT rs.EOF
rs.MoveNext
wend
end if
wend%>
|
Here is the ASP code that
comprises the end of the loop. Note that there is a second loop inside the first. This is
called "nesting." The second loop is nested inside the first.
|
|
</table>
|
Finally, the end of the table is output to the browser.
|
As you can see, the code is read sequentially by the server. As each block of server-side code is reached, it is executed immediately by the server. As each block of client-side code (HTML and any client-side scripting), it is sent to the client (browser), where it is executed, in the order in which it is received.
So, you can see that the code is both procedural and object-oriented. The procedural part is the execution of the scripting sequentially, along with any loops which occur. the object-oriented part is the invocation of the various server- and client-side objects that appear in the instructions, such as the creation of the RecordSet object in the very beginning.
Error-handling and Debugging
Believe it or not, the biggest investment of a programmer's time is involved in error-handling and debugging. Why? Because half of the participants in an application, that is, the user, is human, and prone to error, unlike our trusty electronic companions. A relatively simple application may contain scads of code to prevent humans from doing something stupid, or to react appropriately when they do, which they do. Humans do the darndest things. Don't ever put anything past them.
For example, what if you're working with a database, and you want some input into a currency field? Well, it's one thing to format your SQL Statement so that the proper delimiters appear around the appropriate data types. But what if the user adds a dollar sign, or some other unexpected text, rather than simply numbers and the period. What if they type in some ridiculous number, thinking that they are filling in one field when in fact they are filling in another? What if they leave it blank? You have to get creative, and imagine every stupid or inappropriate thing that a user can do, and account for it with appropriate code. This may consist of client-side form validation, server-side code in the form handler page, or both.
You also have to account for things happening on the server, which don't fit well into your plan. There are any number of server-side events which may have an impact upon the execution of your application. What happens, for instance, if the server is rebooted?
Debugging generally consists of testing your code in action. It is best to do this in bits and pieces, testing each section of code as you build it. Debugging consists mostly of trying to do everything you can think of to screw up the application, identifying the causes, and accounting for them. But the final acid test is the running of the completed application, since all of the pieces may affect each other when they are combined into a single web application.
Building a Program
Building a web application is a lot like building a house. You begin with a problem. A client has a need. You identify the main problem, and then begin to identify the component parts of the problem. That is, you break it down into component parts. Each of these parts is an individual problem to be solved. The solution to each problem may contain a number of composite elements. In fact, a finished program is a complex combination of very simple building blocks that all interact with each other.
Once you've come up with your overall plan, you begin to put the simple building blocks together, one at a time, and test each piece as you go. Why? Because it's easier to find a bug in a single page or block of code than it is to find it in a vast expanse of code. Once you begin combining the elements of your application there will be enough digging for needles in haystacks.
You must plan the development of your application logically. Just as you can't build the walls of a house until you have laid the foundation, identify which elements of your application are essential to the workings of others, and plan the development along these lines.
As you progress, the application will develop from something very simple to something very complex, made up of many simple parts. Don't ever let the complexity of the overall plan overwhelm you. Take each simple problem, one at a time, and let the overall plan take shape automatically, as you simply concentrate on the single element you're designing.
Conclusion
If you can keep these fundamentals in mind, you can write a program! Just as the program itself is made up of simple building blocks (how complex is a single brick, or 2X4?), the fundamentals of programming are simple building blocks as well. Keep them all in mind as you work. When the forest gets too complex, take a look at the trees for awhile. Take it one tree at a time, then one grove of trees, and work your way up to the forest. Logic is the key, organizing your thoughts, and your plans.
Hope this helps!
|
|
|
|
|