Dropdowns or combo boxes are an excellent way to get input from a user. One of the biggest problems with user input is spelling and formatting. Here is how to use and populate dropdowns from a database.
Nuts Own Code

Asp Directory

Nuts Web Search
Web Directory
Web Search

Nuts ASP Code
Functions
Scripts
Tutorials
Wizards
Fonts
Download Fonts
Download Dll Files
Dll Files
Relaxing Games
Online Games
Take Our Poll
Do you use .Net?
Yes
No
Not Yet
Dot What?
Really, Take It!
Get The Source
About Nuts4Asp
Privacy
Contact Us
Arts
Movies, Television, Music
Business
Jobs, Industries, Investing
Computers
Internet, Software, Hardware
Games
Video Games, RPGs, Gambling
Health
Fitness, Medicine, Alternative
Home
Family, Consumers, Cooking
Kids and Teens
Arts, School Time, Teen Life
News
Media, Newspapers, Weather
Reference
Maps, Education, Libraries
Regional
US, Canada, UK, Europe
Science
Biology, Psychology, Physics
Sports
Baseball, Soccer, Basketball
World
Deutsch, Español, Français, Italiano, Japanese, Nederlands, Polska, Svenska
Help build the largest human-edited directory of the web
For this example, I will use a list of cities for the client to choose from.

I won't get into the connection string. If you need to see how to connect to the database, several different examples are explained here. You can also find out how to enter it into a database here. Now that you are connected let's get the dropdown box populated.

This example will make a few assumptions. I will outline them now.

  1. The page that will process the form is named next_page.asp.
  2. The database has a table named myTable with a field named city.
  3. The connection string is named Conn.

assumption 1 
<form method="POST" action="next_page.asp">
<p><select size="1" name="D1">
<option selected>
City</option>
<%
'assumption 2
SQL = "Select city FROM myTable ORDER BY city ASC"
'assumption 3
SET RS = Conn.Execute(SQL)
While NOT RS.EOF
%>

<option value="<%=RS("city")%>"><%=RS("city")%></option>
<%
RS.MoveNext
WEND
Set RS = NOTHING
Conn.Close
%>

</select><input type="submit" value="Submit" name="B1"></p>
</form>

Now let's look at the code to get an understanding of what it is saying.

SQL = "Select city FROM myTable ORDER BY city ASC"
This says "open the table named myTable and look at if from the field named city in alphabetical order.
 

SET RS = Conn.Execute(SQL)
Now it is creating a recordset based on what the SQL statement said to do.

While NOT RS.EOF
In plane English this says "Since we aren't at the end of the field named city in the table, write this to the page:"

<option value="
<%=RS("city")%>"><%=RS("city")%></option>

<%=RS("city")%> 
This will write the value of one record (alphabetically mind you).

RS.MoveNext 
Since we still aren't to the end of the field write the next record.

WEND
Since we ran out of things to write, STOP what you're doing.

Set RS = NOTHING
Kill the recordset to free up the server resources you were using. ALWAYS DO THIS!

Conn.Close
Close the database connection.

Bad or missing query parameters in request.

Copyright © 2001-2009 Nuts4Asp.com
All other names used are property or copyright of their respective owners.