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>
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!
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!
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
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!
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>"
%>
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.
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…)?
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.