Tuesday, August 26, 2008

Dynamic Array for unknown number of data

In one situation, I want to display a drop down selection of different dealers for administrator to assign dealer to various products/users. I don't want to connect to database to retrieve dealers for 100 times so I used a in-memory array to hold the dealers and build drop down selection on the ASP page.

Here is the code for dynamic array and assigning values. With this array in memory. It will be fast and easy to create as many dropdown as you want with only one connect to database.


'create an array to save the retailers.
dim aryRetailers() ' dynamic array
ReDim aryRetailers(1,0)

aryRetailers(0,0) = "0"
aryRetailers(1,0) = "select a retailer"
dim cnt
cnt = 1
do while not rst.eof
ReDim Preserve aryRetailers(1, cnt)
aryRetailers(0,cnt) = rst("intRetailer")
aryRetailers(1,cnt) = rst("txtRetailer")
cnt = cnt + 1
rst.movenext
loop
set rst = nothing

' build the dealer drop down list
sub retailerdropdown
<select name=drpRetailer size=1>
for i = 0 to ubound(aryRetailers,2)
<option value=" & aryRetailers(0,i) & ">" & aryRetailers(1,i) & "</option>"
next
</select>
end sub

No comments: