Вопрос

I need to load data from the DataBase to DDL..

I know how to do that with textBox but with ddl it just dosent work..

I tried use selected

set con = Server.CreateObject("ADODB.Connection")
con.open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("WebData/DB.mdb") & ";"
set rsUser= con.Execute("Select * FROM Users WHERE UserName = '"&username&"'")...
 ........
.......
<select name="ddlCountries" selected = "<%rsUser("country")%>">

It dosent work..Moreover I need to load all the options from db and after that to choose the selected value..

I load the data in this way

 <select name= "ddlCountries" id="ddlCountries">
                <option value="-1">  choose
                    </option>
                    <%set con = Server.CreateObject("ADODB.Connection")
                con.open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("WebData/DB.mdb") & ";"
                set rs = con.Execute("Select * FROM Countries where mode=true")
                while not rs.eof%>
                <option value=<%=rs("PkId")%>><%=rs("Name")%></option>
                    <%rs.movenext
                    wend
                rs.close
                set rs= nothing
                Con.close%>
                </select>

Now how i choose the value that i need?

Thanks..Hope you understand me

Это было полезно?

Решение

Assuming that your database connection is correct and opened and rs("Name") is the name of country this should work for you:

  <% 
 dim rsSelected
 set con = Server.CreateObject("ADODB.Connection")
 con.open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" &          Server.MapPath("WebData/DB.mdb") & ";"
 set rsUser  = Server.CreateObject("ADODB.RecordSet")
 set rsUser= con.Execute("Select top 1 country FROM Users WHERE UserName = '" & username & "'")

 rsSelected=rsUser("country")

 set rsUser=nothing
 %>
 <select name= "ddlCountries" id="ddlCountries">
            <option value="-1">  choose
                </option>
                <%

            set rs = Server.CreateObject("ADODB.RecordSet")
            set rs = con.Execute("Select * FROM Countries where mode=true")
            while not rs.eof
 if rs("Name")=rsSelected then%>
  <option selected value=<%=rs("PkId")%>><%=rs("Name")%></option>
 <% else 
 %>
            <option value=<%=rs("PkId")%>><%=rs("Name")%></option>
                <%
 end if
 rs.movenext
                wend
            rs.close
            set rs= nothing
            Con.close%>
            </select>

Just make sure that Select * FROM Users WHERE UserName = '" & username & "'" always return single record.I would put some safeguards in case if it does not return anything.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top