Free Guides

Language Tutorials


           

ASP ( Active Server Pages )

                 Index              

Your Ad Here

 

Basic Control Structure                             

Relative and Virtual file Including in ASP

Efficient coding requires reusable common code which gets linked to more than one file. In ASP we will be using file include to add codes inside one external file to our required file. There can be many reasons to use file include inside our ASP scripts, some important requirements we will discuss here.

Menu or links area
Let us say we have a site with more pages ( say one hundred ) and we have added one more page to this site. The link of this section or page has to be connected to all hundred pages so we have to edit all the required pages were the link to this new page is to be added. A better solution is to use one menu.asp file and include the file inside every asp file like this
 

<!---#include file="menu.asp" --->


We only have to add our new page link inside the menu.asp file and it will appear all the pages which have included this menu.asp file.

Here the menu.asp file is located in the same directory as the main files calling this include file. If this is inside another directory say my_dir then it has to be included like this.
 

<!---#include file="my_dir/menu.asp" --->

Another requirement is to keep the database connecting string. Say your host has given you a set of login id , password and database detail ( known as connecting string ). We will be using database in many files so using the connecting string to connect to database. So rather than keeping this connecting string in each and every file we will keep in one connection.asp file and include this inside all required files. Now this will help us a great when we shift our site to a new host. As the new host give a new connection string we need to change this connection.asp file only. Another way it helps we are maintaining a copy of the site in our local computer with its own connecting string.

Including Virtual
We can include a file located from the root of the virtual directory. Say our connection string is kept at root of our virtual directory. Now from any level the same file can be included like this .

 

<!-- #include virtual = "/connection.asp" -->


Another point we have to keep in mind while keeping sensitive data like connecting string is file extension. If we keep the file name as connection.inc and any one open the file in browser then all the code with database login details will be exposed. So we have to keep these data inside a file with .asp extension.

Include files are always included first before all other codes of main file gets executed. So we can't keep any condition like if else to dynamically include any file.
 

If Else and nested If conditional script control

To control the structure and flow of script we use if then else conditional statements of VBScript used in ASP. By using different combinations of if else condition we can control the program execution flow as per requirement. Before going into more details on this let us try the basic syntax of if – condition – Then – Else – Endif syntax.
 

If condition Then
Script block 1 here if condition is true
Else
Script block 2 here if condition is false
EndIf

In the above script block we have first tested the condition and if it is TRUE then Script block 1 gets executed and if the condition is FALSE then script block 2 is executed.

Here is a simple sample code for printing greatest of two numbers.
 

Dim n1,n2
n1=25
n2=45
If n1 > n2 Then
Response.Write " Greatest number is " & n1
Else
Response.Write " Greatest number is " & n2
End If

 


By changing the assigned values to n1 and n2 we can check how the if else control structure works.

We can keep If conditions inside another If conditions but each If condition has to have matching End If condition. We call it nested If conditions. The above code is changed to add one more If condition inside main If condition.
 

Dim n1,n2
n1=-25
n2=-45
If n1 > n2 Then
Response.Write " Greatest number is " & n1
If n1 < 0 Then
Response.Write " First number is a negetive number "
End IF

Else
Response.Write " Greatest number is " & n2
End If
 

Some places we can use ElseIF condition than using more nested If Else condition. Here is an example of uses of ElseIf Condition
 

Dim my_num
my_num=40
If my_num < 100 Then
Response.Write " It is less than 100" & "<br>"
If my_num < 50 Then
Response.Write " It is less than 50 also" & "<br>"
End If



ElseIf my_num > 200 Then
Response.Write " It is greater than 200 " & "<br>"
Else
Response.Write " This is greater than 100 but less than 200" & "<br>"
End if
 

Note that ElseIf doest not require an End If to close but the original If condition should have its own.

ElseIf condition can't be used just after one Else condition. Where as Else condition can be used before ElseIF condition.
 

Matching options by using Select Case statement in ASP

In VBScript used in ASP, when we have to match a given variable with more number of possible values, it is better to use Select Case statement. Here the first possible matching value is checked and then the code gets executed.


This Select Case type statement is very common in other scripting languages and they are known differently in different scripts. In php the equivalent statement is SWITCH.


Let us start with the syntax of Select Case statement in ASP
 

Select Case expression
Case value1
Script part if value1 matches
Case value2
Script part if value2 matches
End Select
 

Here is the code part in a simple example where a number is stored in a variable and then it is matched with different Cases inside a Select Case statement.
 

Dim my_num
my_num=150

Select Case my_num
Case 100
Response.Write “It is less than 100" & "<br>"
Case 150 Response.Write “It is equal to 150 " & "<br>"
Case 200 Response.Write “It is equal to 200 " & "<br>"
End Select
 

The output of the above line is “It is equal to 150”

In the same line while checking for one value we can check for another value by separating it with a coma, like this .
 

Case 150,160
Response.Write " It is equal to 150 or 160 " & "<br>"

If none of the value matches with the variable we have set then we can keep one default value for this by using Case Else. So in the code below the Case Else statement will be executed.
 

Dim my_num
my_num=160
Select Case my_num

Case 100
Response.Write " It is greater than 150 equal to 200 " & "<br>"
Case 150
Response.Write " It is equal to 150 " & "<br>"
Case Else
Response.Write " It is Not equal to any set values " & "<br>"

End Select

Here the Case Else part will be executed as all other cases will fail to match.

ASP Do While Wend commands syntax

Looping is a common requirement in any scripting language, we will learn here how to use Do While, Until, Loops to manage execution of the code blocks more than once.

Let us start with do While Loop, here the condition is checked for the starting of the loop and the code is executed only if the condition is TRUE. Here is the syntax
 

Do While Condition
Script block here
Loop
 

As long as the condition is satisfied ( or True ) the script block will be executed. Always at the starting the Condition is checked. Here is a simple script using Do While loop.

 

Dim my_num
my_num=1
Do While my_num <=10
Response.Write my_num & "<br>"
my_num = my_num +1
Loop
 

The above code will print 1 to 10 and it will fail to print 11 as the condition checking will return False. We can modify the Do While loop and keep the condition checking at the end of the Loop.

Do
Script block here
Loop While Condition

 

Now let us change our old example and try this way.
 

Dim my_num
my_num=11

Do
Response.Write my_num & "<br>"
my_num = my_num +1
Loop While my_num <=10
 


Here we can see there is a output of 11 as the inside the loop code is executed once even before the condition is checked and found False. So this way at least once the code inside the Loop will be executed. Test the above code by adding the While condition at the starting of the loop.

Sometime we may try to execute the loop till the condition becomes True. For this we can use Do Until Loop statement. Here is the code.
 

Dim my_num
my_num=1
Do Until my_num > 10
Response.Write my_num & "<br>"
my_num = my_num +1
Loop
 

There While … Wend statement for looping also. This is same as Do While Loop.

ASP for Next Commands syntax

We can fix the number of looping across a code block to be done by using for next looping. This is one of the common scripting commands used in different languages. PHP for loop is here. Let us start with the basic syntax of for next loop.
 

For count_start to count_end step 1
Script block
Next
 

count_start is the variable stores the initial value of the count, count_end is the final value of the loop and the value of count_start increases by the value of step. Default value of Step is 1. Here is a simple code to display number 1 to 10 by For Next loop.
 

Dim i
For i=1 to 10
Response.Write i & "<br>"
Next
 

The code above will print number 1 to 10. We can modify the code by adding a step value of 3 like this.
 

Dim i
For i=1 to 10 step 2
Response.Write i & "<br>"
Next
 

The output of the above code is 1 3 5 7 9 with line breaks after end of every number.