Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

IF - THEN statement not working! 1

Status
Not open for further replies.

Rexolio

Technical User
Aug 29, 2001
230
Hi guys,

I'm trying to run the following script, which fills in values for a drop down menu:

Code:
<%
formid = request.querystring(&quot;formid&quot;)

Set rs = conn.execute(&quot;SELECT * FROM table&quot;)
do while not rs.eof
   if rs(&quot;formid&quot;) = formid then
      formselected = &quot; selected&quot;
   else
      formselected = &quot;&quot;
   end if
%>
<option value=&quot;<%=rs(&quot;value&quot;)%>&quot;<%=formselected%>><%=rs(&quot;value&quot;)%></option>
<%
rs.movenext
loop
rs.close
%>

I've checked formid and it's being passed correctly and there's only one value that matches it in the database. I've used a similar script tons of time and it will make the appropriate entry &quot;selected&quot;. But this isn't working and I swear to God, I can't for the life of me figure out why. Seriously, there is NOTHING different in the querystring value and the value I'm trying to match in the database. I've checked over and over again.

I'm baffled! Any suggestions?


[bugeyed]
rexolio@bellsouth.net
&quot;I'm not dumb. I just have a command of thoroughly useless information.&quot; - Calvin, of Calvin and Hobbes
 
Is it creating the list of options correctly, including the formid that matches the one in your query string? Perhaps you could place a response.write of the rs(&quot;formid&quot;) in your do loop (outside of your if then statement) to verify the values that are being passed.

The other possibility is that they may be different types. If these are supposed to be numeric values, you may want to convert them to the same type and then do your comparison.

Hope this helps.

--------------------------------------------------------------------------------------------------------------------------
Human beings, who are almost unique in having the ability to learn from the experience of others, are also remarkable for their apparent disinclination to do so.
--Douglas Adams
 
3 suggestions:

1 - Like chopstick said, response.write your rs field values to see the value.

2 - Try converting your values to integers, just to be safe. Like this:

if CInt(rs(&quot;formid&quot;)) = CInt(formid) then

Just make sure they are integer values.

3 - I would also change your line that writes out the option tags. Just write out the entire HTML string in your loop:

<%

formid = request.querystring(&quot;formid&quot;)

'Make sure we did get a value.
if len(trim(formid)) > 0 then
'We did. Continue.
'Get our records.
Set rs = conn.execute(&quot;SELECT * FROM table&quot;)

'Loop thru the rs, displaying the data in option tags.
do while not rs.eof
if cint(rs(&quot;formid&quot;)) = cint(formid) then
Response.Write (&quot;<option selected value=&quot;&quot;&quot; & rs(&quot;value&quot;) & &quot;&quot;&quot;>&quot; & rs(&quot;value&quot;) & &quot;</option>&quot;)
else
Response.Write (&quot;<option value=&quot;&quot;&quot; & rs(&quot;value&quot;) & &quot;&quot;&quot;>&quot; & rs(&quot;value&quot;) & &quot;</option>&quot;)
end if
loop

'Close & destroy our recordset.
rs.Close
set rs = nothing
end if
%>


Mark
 
as meckeard suggested, the problem may be with the variable types, i.e., values coming in from the Request object are strings, while the values in your database may be integers? my preference when doing comparisions like this (comparing request object values to database values) is to treat them all as strings...

so, in this case, I might suggest using the trim() function to make this a little easier, as in:

Code:
if trim(rs(&quot;formid&quot;).value) = trim(formid) then
  formselected = &quot; selected&quot;
else
  formselected = &quot;&quot;
end if

good luck!
 
Hi guys...

Thanks!!! I had already did the response.write to verify that the values were correct, which is how I knew in the first place. BUT, Mark, if CInt(rs(&quot;formid&quot;)) = CInt(formid) then worked!!!!

Thanks soooo much!


[bugeyed]
rexolio@bellsouth.net
&quot;I'm not dumb. I just have a command of thoroughly useless information.&quot; - Calvin, of Calvin and Hobbes
 
i would just like to point out that the statement:
Code:
  if CInt(rs(&quot;formid&quot;)) = CInt(formid)
will produce an error if formid is ever a non-integer or empty-string. you can use trim() or CStr() for more robustness.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top