Selecting a drop down option

I am trying to read from a database and create 8 dropdowns. I am only getting one dropdown and struggling to get 8. Any help would greatly be appreciated.

<select name="customer">

<% 
For i=1 To 8
WHILE NOT objRS.EOF 

firstname = objrs("FirstName")
lastname = objrs("LastName")
customer = FirstName & " " & LastName

Response.Write "<option value=" & customer & ">" & customer & "</option>"

objRS.MoveNext

WEND
next
%>
</select>

Hi,

This isn’t a JavaScript question, so I moved it to the .NET forum.

I don’t really do .NET, but this seems weird:

firstname = objrs("FirstName")
lastname = objrs("LastName")
customer = FirstName & " " & LastName

Shouldn’t it be:

firstname = objrs("FirstName")
lastname = objrs("LastName")
customer = firstname & " " & lastname

The code you posted only seems to create one dropdown. Do you have this code 8 times?

1 Like

Hi James,

Thank you for moving the post; my mistake. Yes, it is not JS - it’s ASP Classic, not NET.

I was hoping that one of the .NET experts would be able to help me. The for I-1 to 8, I believe should be able to repeat the option select 8 times. But I am struggling with getting it right though!

Hopefully, someone can help. Thank you again.

Ah ok, I had thought you wanted 8 selects, not 8 options.

In that case I would imagine that Response.Write sends a response to the client then terminates the loop (hence only one option).

What you should try now is to build up the string you want to return within the loop, then only send it once the loop is done.

I have tried every combination under the sun and can’t seem to get it right; hence looking to the forum for a solution. Spent hours researching and can’t figure it out. I think it’s something simple, but I am missing it!

Ok, well good luck.

If I was you, I would consider either learning .NET, or having your backend rewritten in something you can find developers for. Looping over a dataset shouldn’t be something you need to spend hours on :slight_smile:

I am able to loop through a database just fine, not difficult to do. Have many sites that do that. I am just have difficulty accomplishing a loop that gives me, in this case of I=1 to 8, 8 selects with the same database information that is currently in just the one. I think it’s comes down to handling the for I=1 to 8 and the positioning that the I for the repeat. Oh well !! I appreciate your thoughts and I know that your are the expert with JS (and others) etc. I am just an old dog trying to learn new tricks and at times I struggle. Just doesn’t come easy to me!

1 Like

Wow. It’s been a few since I’ve seen classic asp…

This method will not get you eight instances. It will get you as many rows as are returned in your recordset object. If you’re only seeing one, that’s all the recordset is returning.

You can find that out by checking the record count

Response.Write objRS.RecordCount

But if you’re going to continue down the path of learning classic asp (though I would suggest learning something more current - MVC or Core), then you’ll want to get used to using GetRows(). That will return the recordset as a two dimensional array, which is far more efficient.

You’ll also want to get out of the habit of switching back and forth from html and server side. I think this is right but once again, it’s been years…

<%
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open(Server.Mappath("/db/northwind.mdb"))
set rs = Server.CreateObject("ADODB.recordset")
rs.Open "SELECT FirstName, LastName FROM Customers", conn

arrRS = rs.GetRows()
dim maxRows = 7
response.write "<select name=""customer"">"
for i = 0 to uBound(arrRS, 2)
     customerName = arrRS(0, i) & " " & arrRS(1, i)
     response.write "<option value=""" & customerName & """>" & customer & "</option>"
     if i > maxRows then Exit For
next
response.write "</select>"
%>
1 Like

I had tried previously using getRows and ubound as you have but getting errors so I went the other route. I will work on your code and get back to you.

Thank you for your time.

That code gives me the same results as my code except it limits the records. Obviously that can be changed, but I would want all.

What I am looking for is to have (in this case) 8 dropdown boxes with all the fields as it’s looped through the database.

To me this smells. Could you tell us why you need 8 duplicate drop downs?

REALLY!

Because I am doing a form that requires it.

Yes really. If that’s the best response you can come up with, then good luck.

Thank you for getting me started on the right track. This is what I ended up with and needed; 8 individual dropdowns. Thanks again!

arrobjRS = objrs.GetRows()

for j = 1 to 8
response.write "<select name=""customerName "">"

for i = 0 to uBound(arrobjRS, 2)

     customerName  = arrobjRS(0, i) & " " & arrobjRS(1, i)

     response.write "<option value=""" & customerName & """>" & customerName  & "</option>"

next

response.write "</select>"
next
1 Like

But that gives you eight copies of the exact same record. Why in the world would you need that?

1 Like

A selection of teams.

That still makes no sense. What value does this add? The only thing the person can select is…John Smith.

<select name="customerName">
     <option value="John Smith">John Smith</option>
     <option value="John Smith">John Smith</option>
     <option value="John Smith">John Smith</option>
     <option value="John Smith">John Smith</option>
     <option value="John Smith">John Smith</option>
     <option value="John Smith">John Smith</option>
     <option value="John Smith">John Smith</option>
     <option value="John Smith">John Smith</option>
</select>

So if that’s the ONLY option, then why not just automatically set the value? Why give them a non-existent choice (or one that LOOKS like an error and would make me question what I was getting into trying to use the form…)?

No, John Smith is not the only option. It loops through the database and ends up like:

<select name="customerName">
     <option value="John Smith">John Smith</option>
     <option value="Ted Sullivan">Ted Sullivan</option>
     <option value="Mary Martin">Mary Martin</option>
     <option value="Bob Jones">Bob Jones</option>
     <option value="Harry Johnson">Harry Johnson</option>
     <option value="Bruce Evans">Bruce Evans</option>
     <option value="Ken White">Ken White</option>
     <option value="Tom Jones">Tom Jones</option>
</select>

Ahhhhh… Now I get it. You changed the code behavior from the first post to the last. You wanted eight exact copies of the same dropdown.

Other than the select having the same name as all the rest which can cause issues on the backend that you would need to account for. But there is an easier way to get what you want.

arrobjRS = objrs.GetRows()

' Create a list of the dropdown options
customerDropDown = ""
for i = 0 to uBound(arrobjRS, 2)
     customerName  = arrobjRS(0, i) & " " & arrobjRS(1, i)
     customerDropDown = customerDropDown & "<option value=""" & customerName & """>" & customerName  & "</option>"
next

' Now create the eight dropdowns.
for j = 1 to 8
    response.write "<select name=""customerName "">" & customerDropDown & "</select>"
next

So glad you understand what I am doing! Thank you for the alternative code. I will give it a try and I truly appreciate having an expert such as yourself to give me that.