SunQuest
 
       ASP
  Home arrow ASP arrow Page 5 - 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 
VeriSign Whitepapers 
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
     
    Iron Speed
     
    ADVERTISEMENT

    Ajax Application Generator Generate database and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!

    The Real Basics of Functions in ASP - Calling the Function


    (Page 5 of 5 )

    So now we need to call the function. There is a wealth of information on database connectivity techniques dotted all over the Web, but for the sake of simplicity we’ll assume we’ve got an MS Access database called "shop" stored in the same folder as the script. This is sufficient for initial learning but it is worth taking look at the alternatives available to you for your live site and the security implications of the options.

    Here is the complete script. Simply paste this into an ASP file – and to test you’ll need that copy of the database as described above.

    <%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    </head>

    <body>
    <form>
    <%
    ' define the database connection settings
    strconnect="DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & server.mappath("shop.mdb")

    ' open a connection object and actually connect to the database
    set dbconn=server.createobject("adodb.connection")
    dbconn.open strconnect

    call db_dropdown(dbconn, "tbl_fruit", "fruit_id", "fruit_name")

    ' close the db connection
    dbconn.close
    set dbconn=Nothing
    %>
    </form>
    </body>
    </html>
    <%
    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><br />"
    end function
    %>

    Now every time that you need a drop-down box you simply have to call this function "db_dropdown". You may choose to place the Database Connection object inside this function or even another function. There are many ways this function can be developed further and that is the great benefit of the concept. The code may do exactly what you need it to do now, but in six months time you may decide that the results need to be sorted in alphabetical order, or that you want to add a default value to the function. With a bit of thinking you’ll be able to achieve this with an absolute minimum of work.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · 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

    Iron Speed




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