Connie
Connie

Reputation: 15

Classic ASP: Empty Select When Using If Statement

I have a simple bit of code that is filling a select object from a db table. I want to check a value to set an entry as selected, but when I check a db value, I get an empty select box. This code produces the empty box:

response.write "<td><select name='FromDept'>"
Do While not rs.eof
   If rs("DeptID")  = 61 Then 
      response.write "<option value=" & rs("DeptID") & " selected>" & rs("DeptName")     & "</option>" 
   Else    
      response.write "<option value=" & rs("DeptID") & ">" & rs("DeptName") & "</option>"
   End If 
   rs.MoveNext     
Loop
rs.close
response.write "</select></td>"

However, this code produces a select box with values:

response.write "<td><select name='FromDept'>"
LpCnt = 0 
Do While not rs.eof

    If LpCnt = 9 Then 
       response.write "<option value=" & rs("DeptID") & " selected>" & rs("DeptName") & "</option>" 
    Else    
       response.write "<option value=" & rs("DeptID") & ">" & rs("DeptName") & "</option>"
    End If 
    rs.MoveNext     
    LpCnt = LpCnt + 1 
Loop
rs.close
response.write "</select></td>"  

Thanks for any help!

Upvotes: 0

Views: 1406

Answers (2)

Shadow Wizard
Shadow Wizard

Reputation: 66388

Empty drop down means you get error in that statement and it's ignored most likely due to On Error Resume Next line you have somewhere.

First of all, get rid of that On Error Resume Next line.

Second, error in such code means type conversion problem. Try this code instead:

Dim curDeptID
Do While not rs.eof
   curDeptID = 0
   If Not IsNull(rs("DeptID")) Then
       curDeptID = CLng(CStr(rs("DeptID")))
   End If
   If curDeptID=61 Then 
      response.write "<option value=" & rs("DeptID") & " selected>" & rs("DeptName")     & "</option>" 
   Else    
      response.write "<option value=" & rs("DeptID") & ">" & rs("DeptName") & "</option>"
   End If 
   rs.MoveNext     
Loop

Upvotes: 0

Diodeus - James MacFarlane
Diodeus - James MacFarlane

Reputation: 114377

Assign the value to a temporary variable:

response.write "<td><select name='FromDept'>"
Do While not rs.eof
   dept = rs("DeptID")
   If dept  = 61 Then 
      response.write "<option value=" & dept & " selected>" & rs("DeptName")     & "</option>" 
   ...

Upvotes: 1

Related Questions