Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 5 of 5
  1. #1
    Unpaid WDF Intern TheGAME1264's Avatar
    Join Date
    Dec 2002
    Location
    Not from USA
    Posts
    14,485
    Member #
    425
    Liked
    2783 times
    In my years as an ASP developer, I've gotten into the habit of using a querystring called "action" on my various pages to determine the appropriate action a page is to take.

    For example, I may have an ASP page that looks something like this:

    Code:
    sub Procedure1
    
    ... 
    
    end sub
    
    sub Procedure2
    
    ...
    
    end sub
    
    sub PageHeader
    
    (page header code)
    
    end sub
    
    sub PageFooter
    
    (page footer code)
    
    end sub
    
    Dim Action
    Action = Request.QueryString ("action")
    if Action = "" then
         Action = "procedure1"
    end if
    PageHeader
    if Action = "procedure1" then
         Procedure1
    elseif Action = "procedure2" then
         Procedure2
    end if
    PageFooter
    In this very simple example, the problem isn't immediately apparent. But let's say we added Procedure3, Procedure4, Procedure5, Procedure6, etc. to this page. The series of if statements associated with the Action statement would quickly add up and become rather cumbersome.

    Enter the "Execute" function. Execute is a built-in ASP function that will allow the programmer to execute a line of ASP code that is passed to it.

    The syntax is as follows:
    Code:
    Execute (line of ASP code)
    For example:
    Code:
    Execute (Response.Write "Hello")
    Would execute the line Response.Write "Hello", and as a result, "Hello" would be outputted to the screen.

    Going back to our example above, let's replace all of those simple if statements with one execute line. You will notice that I have used values for the Action querystring that correspond with their associated procedures. Besides being a good programming practice (since it tells another programmer what's going to be called), it can be used in conjunction with the Execute command to reduce code significantly.

    How is this done? Let's take our example above and make one modification:

    Code:
    sub Procedure1
    
    ... 
    
    end sub
    
    sub Procedure2
    
    ...
    
    end sub
    
    sub PageHeader
    
    (page header code)
    
    end sub
    
    sub PageFooter
    
    (page footer code)
    
    end sub
    
    Dim Action
    Action = Request.QueryString ("action")
    if Action = "" then
         Action = "procedure1"
    end if
    PageHeader
    Execute (Action)
    PageFooter
    As you can see, we've replaced five lines of if statements with a single line that will execute whatever the value of Action may be.

    Those of you who are more perceptive will have noticed that I assigned a default value to Action. This is done to ensure that something gets executed; if Action remained null, then an error would be generated.

    In the next post, I will provide another huge timesaver when it comes to retrieving information for a specific record.
    If I've helped you out in any way, please pay it forward. My wife and I are walking for Autism Speaks. Please donate, and thanks.

    If someone helped you out, be sure to "Like" their post and/or help them in kind. The "Like" link is on the bottom right of each post, beside the "Share" link.

    My stuff (well, some of it): My bowling alley site | Canadian Postal Code Info (beta)

  2.  

  3. #2
    Unpaid WDF Intern TheGAME1264's Avatar
    Join Date
    Dec 2002
    Location
    Not from USA
    Posts
    14,485
    Member #
    425
    Liked
    2783 times
    Retrieving Information from a Record

    In this example, we will retrieve the Name and Email information from our database when we have a specific ID match (in other words, when one and only one record can match.)

    Code:
    sub FieldRetriever (Field_Count)
    
    	Dim q, TheString
                    ' Loop must be from 0 to field count since fields in a
                    ' database are counted starting at 0.  Remember to pass a 
                    ' value to Field_Count that is equal to the number of fields 
                    ' being retrieved - 1.
    	for q = 0 to Field_Count
    		TheString = RS (q).Name & " = RS (" & q & ")"
    		Execute (TheString)
    	Next
    
    end sub
    
    sub RetrieveNameandEmail
    
         ' Alternatively, the "1" in this query can be a variable 
         ' representing the customer ID.
         SQL_Query = "Select Name, Email from Customers where ID = 1" 
         RS.Open SQL_Query
         ' make sure there is at least one match
         if RS.RecordCount > 0 then
                 ' Retrieve Name and Email.  Make sure that the value passed 
                 ' to FieldRetriever is one less than the number of fields being 
                 ' retrieved.
                 call FieldRetriever (1)
         end if
         RS.Close
         ' check to make sure that you got what you wanted.  This doesn't 
         ' really need to be there, but for the sake of this exercise, I
         ' provided it.
         Response.Write Name & " " & Email 
       
    end sub
    
    sub PageHeader
    
    (page header code)
    
    end sub
    
    sub PageFooter
    
    (page footer code)
    
    end sub
    
    Dim Name, Email
    ' Name and Email MUST be global variables since we are going to 
    ' retrieve values for them from one sub procedure and use them in another.  
    ' This is EXTREMELY important.  The code will still execute, but no
    ' values will be returned, if these variables are declared locally or not
    ' at all.
    ' They must also have the same names as the fields of information
    ' being retrieved.
    PageHeader
    RetrieveNameandEmail
    PageFooter
    In the example above, FieldRetriever will execute a line that will assign a value retrieved from a database to a variable which is presumed to exist and have a name matching RS (q).Name (or the name of the field of information being retrieved).

    This is a huge timesaver when attempting to retrieve information from 5 or more fields. I have cases where I need 15, 16, 17 fields of information for one record, and calling one line as opposed to typing 17 is a big advantage.

    There are many other uses for Execute, but these two alone should give the aspiring ASP developer a building block for future ease of development.

    Be an Executioner!
    If I've helped you out in any way, please pay it forward. My wife and I are walking for Autism Speaks. Please donate, and thanks.

    If someone helped you out, be sure to "Like" their post and/or help them in kind. The "Like" link is on the bottom right of each post, beside the "Share" link.

    My stuff (well, some of it): My bowling alley site | Canadian Postal Code Info (beta)

  4. #3
    WDF Staff smoseley's Avatar
    Join Date
    Mar 2003
    Location
    Boston, MA
    Posts
    9,729
    Member #
    819
    Liked
    205 times
    Back in my ASP days, I used to use a QueryString or Form Collection-based methodology like this for administration interfaces, but I used two parameters: table & action

    Then, I would supply the table name and action for the two params. For instance: "user, delete".

    What would happen next is interesting. I created a subroutine that would concatenate the table name and action and create a Command object to Execute the stored procedure with that name. For instance, "user_delete".

    Now, as you know, ASP has the Command.Refresh() method, which will automatically retrieve the required Params collection for a stored procedure. So what I did then was I had the function loop through the collection of Params and perform a Request.Form(Param.Name.Value) - which would then search for the required parameter. Based upon the Param.Type, I would then also apply whatever necessary formatting to it.

    It was a really nice functionality, because I could add admin interfaces simply by creating the form and the stored procedure. I was in the process of automating the form creation when I decided to dump ASP altogether and devote myself to Java, JSP and PHP.

  5. #4
    WDF Staff smoseley's Avatar
    Join Date
    Mar 2003
    Location
    Boston, MA
    Posts
    9,729
    Member #
    819
    Liked
    205 times
    PS... WELCOME BACK !!

  6. #5
    Unpaid WDF Intern TheGAME1264's Avatar
    Join Date
    Dec 2002
    Location
    Not from USA
    Posts
    14,485
    Member #
    425
    Liked
    2783 times
    Thanks. And thanks for wreckin' a perfectly good tutorial with something even I barely understood. heh
    If I've helped you out in any way, please pay it forward. My wife and I are walking for Autism Speaks. Please donate, and thanks.

    If someone helped you out, be sure to "Like" their post and/or help them in kind. The "Like" link is on the bottom right of each post, beside the "Share" link.

    My stuff (well, some of it): My bowling alley site | Canadian Postal Code Info (beta)


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Search tags for this page

asp code like link

,

asp page run under functional account

,

execute function asp

,

function used to execute in asp

Click on a term to search for related topics.
All times are GMT -6. The time now is 09:56 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com