Search code examples
htmlsql-serverasp-classic

How to fill a Select with data from SQL Server? Using ASP Classic


I'm a newbie programming on ASP Classic and I'm trying to fill a Select with information from SQL Server tables, but I got trouble trying to separate two columns, I need put a column on Value side and the other one outside option just for display the name of the option.

I'm trying to get this:

<option value="Field_1">Field_2</option>

I'm trying with different loop ways, but I still am getting 2 columns combined inside my value.

Like this:

<option value="Field_1Field_2"> </option>

This is my code - I appreciate any ideas.

Thanks

set rs = Server.CreateObject("ADODB.Recordset")
SQL = "SELECT id_geren, Acron FROM tbl_geren"
rs.open SQL, conect
<html>
<body>
<select id="" style="width:65px"> 

<% do until rs.EOF %>
<option value=
<% for each x in rs.fields %>
<% Response.Write(x) %>
<% next
rs.MoveNext %>
></option>
<%  loop
rs.close

%>
</select>
</body>
</html>

I expect the output like this:

<option value="id_geren">Acron</option>

but the actual output is:

<option value="id_gerenAcron"></option>

Solution

  • You don't need to iterate over the individual fields as they can be accessed independently;

    <% do until rs.EOF %>
    <option value=“<%= rs(“id_geren”) %>”><%= rs(“Acron”) %>”></option>
    <% rs.MoveNext %>
    <% loop
    rs.close
    %>