...

View Full Version : Dependent Drop Down List from DB



novicecoder
03-15-2006, 05:53 PM
I have an .asp page and want to display two drop down lists, please note the data comes from db values in a table:

DropDown1: Application
DropDown2: Server Location

In the table, I may have multiple instances of the same application name, but the only difference between all instances of say "Office" will be the server name/physical host. How can I remove these duplicate entries in DropDown1, and have it populate DropDown2 with the server location if there are duplicates?

For example

DropDown1:
Office <-- selected (would like to remove duplicates here also)
Office
Word
PPT
PPT
PPT

DropDown2:
New York
DC


Here is my code -> currently with only 1 drop down - not sure how to create a 2nd dependent one.




<SELECT NAME="Sites" style="font-family: Verdana; font-size: 9pt">
<OPTION> Please select a Application</OPTION><%

If WebSiteName <> "" Then
While Not objRS.EOF
If objRS("Application_Name") <> "" Then
If objRS("Application_Name") = WebSiteName Then
%>
<OPTION VALUE="<%=objRS("Application_Name")%>" SELECTED><%=objRS("Application_Name")%></OPTION><%
Else
%>
<OPTION VALUE="<%=objRS("Application_Name")%>"><%=objRS("Application_Name")%></OPTION><%
End If
End If
objRS.MoveNext
WEnd
Else
While Not objRS.EOF
If objRS("Application_Name") <> "" Then
%>
<OPTION VALUE="<%=objRS("Application_Name")%>"><%=objRS("Application_Name")%></OPTION><%
End If
objRS.MoveNext
WEnd
End If

objRS.Close
Set objRS = Nothing

%>
</SELECT><INPUT TYPE="submit" VALUE="Go" class="buttons" style="font-family: Verdana; font-size: 8pt">
</FORM>
<%

If WebSiteName <> "" Then
strSQL2 = "SELECT * FROM Application$ WHERE Application_Name = '" & WebSiteName & "'"

Set objRS2 = objConn.Execute(strSQL2)

If Err.Number > 0 Then
Response.Write "<B>Sorry, the database cannot be accessed.</B>"
Response.End
End If
If objRS2.EOF Then
Response.Write "<B>I'm sorry but your query returned no records.</B>"
Response.End
End If
%>

<form method="POST" action="thankyou.asp">

miranda
03-16-2006, 03:58 AM
Use SQL's SELECT DISTINCT keyword to eliminate duplicate entries from the first dropdown. Now, as to filling the 2nd drop down you would be better off assigning the values to a javascript array and then building the 2nd dropdown from the array. this way you prevent a postback to the serverafter the 1st dropdown is selected.

degsy
03-16-2006, 03:42 PM
If you want to populate the dropdown in pure ASP then here is an example

http://computer-helpforum.com/asp/aspfree/dependent_dropdown/ASP_dependent_dropdown_2.asp?manufacturer_id=2&Submit=Submit+Manufacturer
See attached for code



You can add javascript and/or Ajax to make it more user friendly
http://computer-helpforum.com/asp/ajax/select_via_ajax.asp



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum