SunQuest
 
       ASP
  Home arrow ASP arrow Page 4 - The Real Basics of Functions in ASP
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Actuate Whitepapers 
Moblin 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP

The Real Basics of Functions in ASP
By: John Puddifoot
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 257
    2004-12-21

    Table of Contents:
  • The Real Basics of Functions in ASP
  • Simplemaths
  • Customer Spec Part 2
  • A Common ASP Task
  • Calling the Function

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    The Real Basics of Functions in ASP - A Common ASP Task


    (Page 4 of 5 )

     

    The following function provides a real life example of a common ASP task. Customer Spec: "The job is to create a drop-down box on my webpage using values from my database table. The table is called tbl_fruit and lists different types of fruit. Take a look at the table below as an example. Oh yes, I might need the drop down to sit inside a form with other input fields."

    fruit_idfruit_name
    1Apple
    2Pear
    3Orange

    The solution is to develop a function named "db_dropdown" which we’ll call in our script just as we did with the simplemaths function earlier.

    The function will carry out the following tasks (which could be further broken down into more functions if we choose to do so):

    1. Open a new HTML dropdown box definition

    2. Query the database table using an open connection. (This opens a recordset object).

    3. Create an option for each record in the recordset result in a single drop-down box

    4. Close the recordset connection

    5. Close the HTML dropdown box definition

    <%
    function db_dropdown(dbconnection, tablename, bound_field, display_field)
      ' output the HTML code to initialise a drop-down box
      response.write "<select name='"&bound_field&"' id='"&bound_field&"'>"

      ' open recordset connection
      strSQL="SELECT " & bound_field & "," & display_field & " FROM " & tablename
      set xrs=server.createobject("adodb.recordset")
      xrs.open strSQL, dbconnection
     
      do while not xrs.eof
        ' for each line record, write an option to the drop-down box
        response.write "<option value='"&xrs(0)& "'>"& xrs(1)& "</option>"
        xrs.movenext
      loop
     
      'clear up the connections
      xrs.close
      set xrs=Nothing

      ' output the HTML code to close the drop-down box
      response.write "</select>"
    end function
    %>

    That’s the function, ready to go. It might not be perfect yet, but because we only need to write it once and we’ll maintain it in one place we can perfect it later. It’s a good idea to keep all of your functions in one place. If you’re familiar with using "Include Files" that’s a great option – if not, then you have some more reading to do! Again it comes down to maintainability. If you have one copy of each of the functions kept in one central file it’s easy to update if necessary.

    More ASP Articles
    More By John Puddifoot


       · The code works great to add new records to a database. However, if you need to...
       · Here is the most valuable function for asp. Place this into global.asas or a...
       · function db_dropdown(sTable, sId, sDropdownName, sBoundfield,...
       · What does the value of "sID" represent? Is it the value of the field from Table1 in...
       · ' Create a suitable connection object for a given connection string. Function...
       · ' Create a suitable connection object for a given connection string. Function...
       · This is very good but i don't why this doesn't work???
       · simple... but VERY cool.
       · I have problem to seek them, because I don't have this software...?
       · function db_dropdown(tablename, bound_field, display_field,order_field,...
     

    ASP ARTICLES

    - ADO for the Beginner
    - ADO.NET 101: Data Rendering with a DataGrid ...
    - Introducing SoftArtisans OfficeWriter 3.0 En...
    - Getting Remote Files With ASP
    - The Real Basics of Functions in ASP
    - Enhancing Readability with ASP
    - Mimicking PHP's String Formatting Functions
    - Windows Server Hacks 12, 77, and 98
    - How to Sort a Multi-Dimensional Array
    - Developing an Information Management Tool wi...
    - What are Active Server Pages?
    - Getting Remote Pages with ASP
    - FTP’ing Files with ASP
    - Apply Single-Sign-On to Your Application
    - Easy Error Management





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway