HOWTO: Populating a Combobox from Active Server Pages

Last reviewed: December 11, 1997
Article ID: Q175426
The information in this article applies to:
  • Active Server Pages
  • Microsoft Visual InterDev, version 1.0

SUMMARY

This article demonstrates how to load a ComboBox with records retrieved from a database. In this example we will create an Active Server Pages (ASPP page that connects to the Adventure Works database.

MORE INFORMATION

The code below demonstrates how to load values into an HTML list box:

   <HTML>
   <BODY>
   <BR>This is an HTML ListBox<BR>
   <SELECT NAME="ListBox" SIZE=1>
   <% Set conn = Server.CreateObject("ADODB.Connection") %>
   <% conn.Open "DSN=AdvWorks"  ' connect to the database %>
   <% Set rs = conn.Execute("SELECT City FROM Customers") %>
   <% Do While Not rs.EOF  ' define the ListBox OPTIONs %>
      <OPTION VALUE="<%= rs("City") %>"> <%= rs("City") %>
      <% rs.MoveNext %>
   <% Loop %>
   <% rs.Close %>
   <% conn.Close %>
   </SELECT>
   </BODY>
   </HTML>

The code below demonstrates how to load values into an ActiveX ComboBox control. Be aware that this control must be installed and registered on your system in order for this sample to work correctly:

   <%@ LANGUAGE="VBSCRIPT" %>
   <HTML>
   <BODY>
   <BR>This is an ActiveX ComboBox control<BR>
   <!-- insert the ActiveX control into the HTML page -->
   <OBJECT ID="ComboBox" WIDTH=96 HEIGHT=24
      CLASSID="CLSID:8BD21D30-EC42-11CE-9E0D-00AA006002F3">
   </OBJECT>
   <SCRIPT LANGUAGE="VBScript">
   <!--
   ' load the ActiveX control after the window has been loaded
   Sub Window_OnLoad()
      <% Set conn = Server.CreateObject("ADODB.Connection") %>
      <% conn.Open "DSN=AdvWorks"  ' connect to the database %>
      <% Set rs = conn.Execute("SELECT City FROM Customers") %>
      <% Do While Not rs.EOF %>
         ComboBox.AddItem("<%= rs("City") %>") ' Do an AddItem for
                                               ' each record
         <% rs.MoveNext %>
      <% Loop %>
      <% rs.Close %>
      <% conn.Close %>
   End Sub
   -->
   </SCRIPT>
   </BODY>
   </HTML>

NOTE: Use your browser to view these pages and the view the HTML source. This will give you a better understanding of what code was produced by ASP.

REFERENCES

For the latest Knowledge Base artices and other support information on Visual InterDev and Active Server Pages, see the following page on the Microsoft Technical Support site:

   http://support.microsoft.com/support/vinterdev/

Keywords          : AXSFHTML AXSFMisc kbcode
Technology        : kbinetdev
Version           : WINDOWS:1.0; WINNT:
Platform          : WINDOWS winnt
Issue type        : kbhowto


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: December 11, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.