Populate Dropdown from SQL in Classic ASP

2019-06-07 12:13发布

问题:

I am trying to force users to only select certain values when adding a record. So naturally I am using a dropdown, but I'd like the options to be populated by a specific field in the database. I figured I'd do a Do/Loop but I am apparently doing something wrong.

Dim dstrSQL
Dim drs
    dstrSQL = "SELECT EventID FROM Events"
    set conn2 = CreateObject("ADODB.Connection")
    conn2.open CONN_STRING
    set drs = conn2.execute(dstrSQL)
    conn2.close: set conn2 = nothing
Do
        Response.Write "<option value=" & drs.Fields(0) & " >" & drs.Fields(0) & "</option>"
        drs.MoveNext
Loop

回答1:

It's been a long time. Something like this:

conn2.open CONN_STRING
set drs = conn2.execute(dstrSQL)

do while not drs.eof %>

    <option value="<%= drs.Fields(0) %>" ><%= drs.Fields(0) %></option>
    <% drs.MoveNext
Loop
 conn2.close
 set conn2 = nothing %>