When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles [1.x] [2.0]
ASPFAQs.com
Message Board
Related Web Technologies
User Tips!
Coding Tips
Search

Sections:
Book Reviews
Sample Chapters
Commonly Asked Message Board Questions
Headlines from ASPWire.com
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
Web Hosts
XML Info
Information:
Advertise
Feedback
Author an Article
Technology Jobs

















internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers
ASP ASP.NET ASP FAQs Message Board Feedback ASP Jobs
Print this page.

Windows Systems Administrator
Jupitermedia
US-CT-Darien

Justtechjobs.com Post A Job | Post A Resume

Published: Monday, December 06, 1999

Building List Boxes Using the Results from a Database Query
By Scott Mitchell


Any ASP developer who has been around the block a few times has written code that creates a list box containing the elements from a database query. A list box is comprised of a number of options. Each option has a displayed title, and a hidden value. It is this value of the selected list box option that is passed onto the ASP page that the form is submitted to.

- continued -

The most common way to build a list box from a database query is to use code similar to:

Response.Write "<SELECT NAME=selListBox>" While Not objRS.EOF Response.Write "<OPTION VALUE=" & objRS("UniqueID") Response.Write ">" & objRS("DisplayTitle") & "</OPTION>" objRS.MoveNext Wend Response.Write "</SELECT>"

(As a side note, this is not the most efficient way to construct a list box from the results of a database query. The fastest way is to use GetString. For an article that presents code on how to build a list box from a database query using GetString, check out Charles Carroll's great GetString article.)

Why retype this code each time you need to create a list box? Why not use a nifty function? This function, provided by alert reader Jason Huber, demonstrates how to create a list box on the fly with the help of a reusable function. Here is the function!

<SCRIPT LANGUAGE=VBSCRIPT RUNAT=SERVER> Public Sub BuildComboBox(rs, dispname, val, name, selected) 'rs = the recordset 'val = fieldname to place in the val of the option 'dispname = fieldname to display in the user control 'name = name of the select (cboBox) 'selected is the item in the lst that should be selected rs.movefirst response.write("<SELECT NAME=" & name & ">") while not rs.eof if rs.fields(val) = selected then response.write("<OPTION VALUE=" & rs.fields(val) & " selected>") response.write(rs.fields(dispname) & "</OPTION>") else response.write("<OPTION VALUE=" & rs.fields(val) & ">") response.write(rs.fields(dispname) & "</OPTION>") end if rs.MoveNext wend response.write("</SELECT>") End Sub </SCRIPT>

That's it! Here is some example code on how to call this function. The table, Products, contains several columns, including the name of the product (ProductName) and a unique identifier for each product (ProductID). The following code will generate a list box named selProduct that will contain an option for each product in the Products table. The name of the product will be displayed, and the value of each option will be the unique indentifier for the product. The product with ProductID equal to 17 will be selected by default. where the

Dim objConn, objRS Set objConn = Server.CreateObject("ADODB.Connection") Set objRS = Server.CreateObject("ADODB.Recordset") objConn.Open "DSN=ProductInfo" Set objRS = objConn.Execute("SELECT * FROM Products") BuildComboBox objRS,"ProdutName","ProductID","selProduct",17

Happy Programming!

  • By Scott Mitchell


    Attachments:

  • Download the source code for BuildComboBox in text format


    Windows Internet Technology | ASP.NET [1.x] [2.0] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article



  • JupiterOnlineMedia

    internet.comearthweb.comDevx.commediabistro.comGraphics.com

    Search:

    Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

    Jupitermedia Corporate Info


    Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

    Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

    Solutions
    Whitepapers and eBooks
    Microsoft Article: HyperV-The Killer Feature in WinServer ‘08
    Avaya Article: How to Feed Data into the Avaya Event Processor
    Microsoft Article: Install What You Need with Win Server ‘08
    HP eBook: Putting the Green into IT
    Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
    Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
    Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
    Avaya Article: Setting Up a SIP A/S Development Environment
    IBM Article: How Cool Is Your Data Center?
    Microsoft Article: Managing Virtual Machines with Microsoft System Center
    HP eBook: Storage Networking , Part 1
    Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
    MORE WHITEPAPERS, EBOOKS, AND ARTICLES
    Webcasts
    Intel Video: Are Multi-core Processors Here to Stay?
    On-Demand Webcast: Five Virtualization Trends to Watch
    HP Video: Page Cost Calculator
    Intel Video: APIs for Parallel Programming
    HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
    Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
    MORE WEBCASTS, PODCASTS, AND VIDEOS
    Downloads and eKits
    Sun Download: Solaris 8 Migration Assistant
    Sybase Download: SQL Anywhere Developer Edition
    Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
    Red Gate Download: SQL Compare Pro 6
    Iron Speed Designer Application Generator
    MORE DOWNLOADS, EKITS, AND FREE TRIALS
    Tutorials and Demos
    How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
    eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
    IBM Article: Collaborating in the High-Performance Workplace
    HP Demo: StorageWorks EVA4400
    Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
    Microsoft How-to Article: Get Going with Silverlight and Windows Live
    MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES