Brasciole
Brasciole

Reputation: 377

Dynamic Select Option in Classic ASP

I'm trying to write this code in Classic ASP but I can't get it to work for some reasons. Maybe I'm missing something:

        <select name="reseller" />
        <option value="">Select a Reseller:</option>
        <%
        sql = "SELECT resellerid, company FROM resellers WHERE insidesales <> 'True' ORDER BY company ASC"
        set rsResellers=conn.execute(sql)
            do until rsResellers.eof
                thisReseller = rsResellers("company")
                if strReseller = thisReseller then thisSEL = " SELECTED='SELECTED'" else thisSEL = "" end if
                response.write "<option value="""& rsResellers("company") &""""& thisSEL &">"& rsResellers("company") &"</option>"
            rsResellers.movenext
            loop
        rsResellers.close
        set rsResellers=nothing
        %>
        </select>

strReseller comes a query: strReseller = rsRMA("reseller"). If I do a response.write strReseller, I get a value, so I know the string isn't empty. But for some reasons, the option isn't "SELECTED".

I know I should be using "resellerid" but for this particular project, I can't since I'm taking over an old database with data in it, and the data doesn't use the ID.

Upvotes: 0

Views: 3398

Answers (1)

KHeaney
KHeaney

Reputation: 785

I would use StrComp over equals and also not write every option with Response.Write only what you need to. Something like this

<select name="reseller" />
<option value="">Select a Reseller:</option>
<% sql = "SELECT resellerid, company" &_
          "FROM resellers WHERE insidesales <> 'True'" &_ 
          "ORDER BY company ASC"
    set rsResellers=conn.execute(sql)
    do until rsResellers.eof
        thisReseller = rsResellers("company")
        if StrComp(strReseller, thisReseller, 1) = 0 then 
            thisSEL="selected='selected'" 
        else thisSEL="" 
        end if %>
        <option value="<%=thisReseller %>" <%=thisSEL%> ><%=thisReseller %></option>
        <% rsResellers.movenext
    loop
    rsResellers.close
    set rsResellers=nothing %>
</select>

This will allow your check to be case insensitive which might be the reason for your error.

Upvotes: 1

Related Questions