Jump to content

dynamically populate drop down lists


raghup

Recommended Posts

How do I populate a drop down list?I have a table named tblChecklist in my database. One of the fields is named txtStatusOfJob and has a dropdown list. This list is populated by another table called tblDDLStatus. The field name in this table is txtStatus.I am using asp with vbscript. I tried using the following code in an edit record page. It is not working. The pages is not loading. Any help would be appreciated.Cheers Raghu

<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%><%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%><!--#include file="Connections/cnDbase2.asp" --><%Dim rsCheckListsSet rsCheckLists = Server.CreateObject("ADODB.Recordset")rsCheckLists.ActiveConnection = MM_cnDbase_STRINGrsCheckLists.Source = "SELECT * FROM ChecklistaipabChad WHERE anID=" & Request.QueryString("id")rsCheckLists.CursorType = 0rsCheckLists.CursorLocation = 2rsCheckLists.LockType = 1rsCheckLists.Open()%><% Dim DataConnDim cmdPopulateStatusDim SQL     Set DataConn = Server.CreateObject("ADODB.Connection")Set cmdPopulateStatus = Server.CreateObject("ADODB.Recordset")DataConn.Open "dsn=webdbase"SQL = "SELECT STATUS, DESCRIPTION FROM ddlStatus ORDER BY STATUS ASC"cmdPopulateStatus.Open SQL, DataConn%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><body><form><Select Name="statusOfJob" id = "statusOfJob"><option value="" selected="<%=(rsCheckLists.Fields.Item("statusOfJob").Value)%>"><%=(rsCheckLists.Fields.Item("statusOfJob").Value)%></option> <%While Not cmdPopulateStatus.EOF%><option value="<%= cmdPopulateStatus("STATUS") %>"><%= cmdPopulateStatus("DESCRIPTION") %></option><%cmdPopulateStatus.MoveNextWendcmdPopulateStatus.CloseSet cmdPopulateStatus = NothingDataConn.CloseSet DataConn = Nothing%></Select></form></body></html>[code]
Link to comment
Share on other sites

rsStatus.Source = "SELECT STATUS FROM tblDDLStatus ORDER BY txtStatus ASC"You are only getting a field called "STATUS" from the table, but you try to order by txtStatus (which you are not getting). You're also trying to use another field later called DESCRIPTION that you are also not getting.

Link to comment
Share on other sites

rsStatus.Source = "SELECT STATUS FROM tblDDLStatus ORDER BY txtStatus ASC"You are only getting a field called "STATUS" from the table, but you try to order by txtStatus (which you are not getting). You're also trying to use another field later called DESCRIPTION that you are also not getting.
I have changed to the following
rsStatus.Source = "SELECT STATUS, DESCRIPTION FROM ddlStatus ORDER BY STATUS ASC"

still not working.cheersraghu

Link to comment
Share on other sites

I have changed the code above. The out put is as followsIN PROGRESS <<APPROVEDARCHIVEDCOMPLETEDIN PROGRESS <<PUBLISHEDREJECTEDThe above code is giving me the dropdown list as shown "IN PROGRESS" is visible two times! How do I fix this?CheersRaghu

Link to comment
Share on other sites

There is a static option here:<option value="" selected="<%=(rsCheckLists.Fields.Item("statusOfJob").Value)%>"><%=(rsCheckLists.Fields.Item("statusOfJob").Value)%></option>So one option will be printed there, and the rest in the loop. If you want then you can check in the loop to skip the one printed first, or also just leave out the first one being printed and instead check in the loop if the current item is the selected item, and if so to write the selected attribute for it.

Link to comment
Share on other sites

There is a static option here:<option value="" selected="<%=(rsCheckLists.Fields.Item("statusOfJob").Value)%>"><%=(rsCheckLists.Fields.Item("statusOfJob").Value)%></option>So one option will be printed there, and the rest in the loop. If you want then you can check in the loop to skip the one printed first, or also just leave out the first one being printed and instead check in the loop if the current item is the selected item, and if so to write the selected attribute for it.
Thanks a lot for the tip. I used the following. It is working fine now!cheersRaghu
<Select Name="statusOfJob" id = "statusOfJob"><%While Not cmdPopulateStatus.EOF%><option value="<%= cmdPopulateStatus("STATUS") %>" <%If rsCheckLists.Fields.Item("statusOfJob") = cmdPopulateStatus("STATUS") Then Response.Write("selected=""selected""") : Response.Write("")%>><%= cmdPopulateStatus("DESCRIPTION") %></option><%cmdPopulateStatus.MoveNextWendcmdPopulateStatus.CloseSet cmdPopulateStatus = NothingDataConn.CloseSet DataConn = Nothing%></Select>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...