In this article we will be discussing a simple way to populate a dropdown list control in a Classic ASP web page. This tutorial is for beginners who have just started going through the legacy applications written in VBScript/Jscript for Classic ASP. This is also useful for people interested in COM Interop.
We will be using VBscript to code the Classic ASP page. The same objective can be achieved using JScript but with a different syntax.
Let's start with the steps.

Step 1

When the page is loaded for the first time we will be checking the already saved value in the database so that it can be displayed as the selected text in the dropdown.

Please refer to the code snippet below. I have used Product_id as a querystring parameter to search the database. Also I have declared a function to set the selected text of dropdown.
  1. <%
  2. 'Get the value of parameter from querystring
  3. Product_ID = Request.Querystring("ProductID")
  4. 'Declare a variable to store the connection string
  5. Dim connstr
  6. connstr = "Provide your connection string"
  7. 'Create a new ADODB recordset
  8. set rsSelectedProduct = Server.CreateObject("ADODB.recordset")
  9. 'Create a new ADODB connection
  10. Set conn = Server.CreateObject("ADODB.Connection")
  11. 'Open the connection using the connection string
  12. conn.open connstr
  13. 'Store the query in a variable , in real world scenario we should be using a stored procedure with parameters
  14. QuerySQL = "Select Product_name from tbl_Products where Product_ID = '" & Product_ID & "'"
  15. 'Execute the query
  16. set rsSelectedProduct = conn.Execute(QuerySQL)
  17. 'Check if recordset is empty , if not then store the value
  18. if not rsSelectedProduct.EOF then
  19. product = rsSelectedProduct("Product_name")
  20. else
  21. product = ""
  22. end if
  23. 'Close the Connection and recordset
  24. rsSelectedProduct.Close
  25. conn.Close
  26. Set rsSelectedProduct = Nothing
  27. Set conn = Nothing

  28. 'Declare a function to set the selected text
  29. Function selectProduct(vProduct)
  30. if vProduct = product then
  31. Response.Write("selected=""selected""")
  32. end if
  33. End Function

  34. %>
Step 2

Inside the server side vbscript code Create a new ADODB connection, ADODB Recordset and open the connection using the required connection string.

Please refer to the code snippet below.
  1. <%
  2. 'Create Connection object
  3. Set conn2 = Server.CreateObject("ADODB.Connection")
  4. 'Create Recordset object
  5. Set rsProductList = Server.CreateObject("ADODB.recordset")
  6. 'Open the connection using the previous connection string
  7. conn2.open connstr
  8. 'Declare a variable to store the query to be excuted. Here we are using a hardcoded query.We can also use a stored procedure
  9. QueryProduct = "select Product_Name,Product_ID from tbl_Products order by Product_Name asc"
  10. %>
Step 3

Execute the query and populate the Recordset from the result. Then convert the recordset to a 2 dimensional array in Vbscript.

Refer to the code below.
  1. <%
  2. 'Execute the query
  3. set rsProductList = conn2.Execute(QueryProduct)
  4. 'Declare the array
  5. Dim arrProducts
  6. if not rsProductList.EOF then
  7. arrProducts = rsProductList.GetRows() ' Convert recordset to 2D Array
  8. end if
  9. 'Close the Connection and recordset
  10. rsProductList.Close
  11. conn2.Close
  12. Set rsProductList = Nothing
  13. Set conn2 = Nothing
  14. %>
Step 4

Create the dropdown list tag inside the HTML <body> section of the page. Here I have used the 2D Array generated in step 3 above.
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>Populate Product dropdown</title>
  6. </head>
  7. <body>
  8. <br/>
  9. <label id="lblOne" for="ddlproduct" title="Select Product:" class="Example">Product:</label>
  10. <select name="productSelect" id="productSelect">
  11. <%
  12. 'Check whether it's a proper array or not
  13. if IsArray(arrProducts) then
  14. For i = 0 to ubound(arrProducts, 2) %>
  15. <option value="<%= arrProducts(1,i)%>" <%= selectProduct(arrProducts(0,i)) %>> <%= arrProducts(0,i) %> </option>
  16. <% next %>
  17. <% else %>
  18. <option value=""> Select </option>
  19. <% end if %>
  20. </select>
  21. </body>
  22. </html>
Here is the Complete code for the page test.asp. If we host this page in IIS then we will be able to see the dropdown generated.
  1. <%
  2. 'Get the value of parameter from querystring
  3. Product_ID = Request.Querystring("ProductID")
  4. 'Declare a variable to store the connection string
  5. Dim connstr
  6. connstr = "Provide your connection string"
  7. 'Create a new ADODB recordset
  8. set rsSelectedProduct = Server.CreateObject("ADODB.recordset")
  9. 'Create a new ADODB connection
  10. Set conn = Server.CreateObject("ADODB.Connection")
  11. 'Open the connection using the connection string
  12. conn.open connstr
  13. 'Store the query in a variable , in real world scenario we should be using a stored procedure with parameters
  14. QuerySQL = "Select Product_name from tbl_Products where Product_ID = '" & Product_ID & "'"
  15. 'Execute the query
  16. set rsSelectedProduct = conn.Execute(QuerySQL)
  17. 'Check if recordset is empty , if not then store the value
  18. if not rsSelectedProduct.EOF then
  19. product = rsSelectedProduct("Product_name")
  20. else
  21. product = ""
  22. end if
  23. 'Close the Connection and recordset
  24. rsSelectedProduct.Close
  25. conn.Close
  26. Set rsSelectedProduct = Nothing
  27. Set conn = Nothing
  28. 'Declare a function to set the selected text
  29. Function selectProduct(vProduct)
  30. if vProduct = product then
  31. Response.Write("selected=""selected""")
  32. end if
  33. End Function
  34. 'Create Connection object
  35. Set conn2 = Server.CreateObject("ADODB.Connection")
  36. 'Create Recordset object
  37. Set rsProductList = Server.CreateObject("ADODB.recordset")
  38. 'Open the connection using the previous connection string
  39. conn2.open connstr
  40. 'Declare a variable to store the query to be excuted. Here we are using a hardcoded query.We can also use a stored procedure
  41. QueryProduct = "select Product_Name,Product_ID from tbl_Products order by Product_Name asc"
  42. 'Execute the query
  43. set rsProductList = conn2.Execute(QueryProduct)
  44. 'Declare the array
  45. Dim arrProducts
  46. if not rsProductList.EOF then
  47. arrProducts = rsProductList.GetRows() ' Convert recordset to 2D Array
  48. end if
  49. 'Close the Connection and recordset
  50. rsProductList.Close
  51. conn2.Close
  52. Set rsProductList = Nothing
  53. Set conn2 = Nothing
  54. %>
  55. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  56. <html xmlns="http://www.w3.org/1999/xhtml">
  57. <head>
  58. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  59. <title>Populate Product dropdown</title>
  60. </head>
  61. <body>
  62. <br/>
  63. <label id="lblOne" for="ddlproduct" title="Select Product:" class="Example">Product:</label>
  64. <select name="productSelect" id="productSelect">
  65. <%
  66. 'Check whether it's a proper array or not
  67. if IsArray(arrProducts) then
  68. For i = 0 to ubound(arrProducts, 2) %>
  69. <option value="<%= arrProducts(1,i)%>" <%= selectProduct(arrProducts(0,i)) %>> <%= arrProducts(0,i) %> </option>
  70. <% next %>
  71. <% else %>
  72. <option value=""> Select </option>
  73. <% end if %>
  74. </select>
  75. </body>
  76. </html>
I hope this will be useful for beginners who have just started going through legacy ASP code. Comments and feedback are always welcome.Thank you.