Click to See Complete Forum and Search --> : ASP Request.Form problem


Alex C
06-03-2004, 07:05 PM
Hi,

I'm trying to submit a form back to itself and then retain the item that was selected in a dropdown menu, The menu is created by grabbing entries from the database. This all works fine. But it's the line If KeepLocation2 = RsLocation("LocationID") Then that seems to never be true. But I've even tested it and KeepLocation2 certainly does equal one of the LocationID column's values. So each time the form is submited back to itself the value is lost and the dropdown menu is created as default again.

Note, I haven't included all the code, but this is basicly the only code that has anything to do with that option box. The db connection etc is all fine as other input boxes/text boxes etc in this form keep their value ok.

Here is my code:



KeepLocation2 = Request.Form("Location2")

<%

dim RsLocation ' Record set used to display database contents
dim SqlCommandLocation ' The SQL command to be sent to database

' Create command string
SqlCommandLocation = _
" SELECT" _
& " LocationID" _
& " , LocationDesc" _
& " FROM" _
& " Location"

' Execute command string
Set RsLocation = SQLQuery(cnDB, SqlCommandLocation)

%>

<select name="Location2" size="1">
<option value="0">

<%

Do Until RsLocation.EOF

If KeepLocation2 = RsLocation("LocationID") Then

Response.Write "<option selected value=""" & RsLocation("LocationID") & """>" & RsLocation("LocationDesc") & vbCrLf

Else

Response.Write "<option value=""" & RsLocation("LocationID") & """>" & RsLocation("LocationDesc") & vbCrLf

End If

RsLocation.MoveNext

Loop

' Close off the connections and clear the connection objects
RsLocation.Close
Set RsLocation = Nothing

%>

</select>


Thanks,
Alex

lmf232s
06-03-2004, 07:59 PM
I am not going to do it client side like you have it but try this and fix the syntax

<option value="<%=RsLocation("LocationID")" <%if KeepLocation2 = RsLocation("LocationID") then Response.write "Selected"%>><%=RsLocation("LocatioinID")%>

Good luck.

Doing it with the Response.write you will need to take out the <% and %> and replace it with the appropriate syntax of " and & where needed.

Later

CrazyC115
06-03-2004, 10:02 PM
ASP makes life fun when dealing with numbers. The answer is:

1 = 1 <- True
1 = "1" <- False
"1" = "1" <- True

SO when comparing numbers I always toss both variables into a CLNG().


IF CLNG(KeepLocation2) = CLNG(RsLocation("LocationID")) THEN
'Do work
END IF


That should solve your problem. Be aware though CLNG will crash if it is not fed a numeric value, ie a null value from your RecordSet. If you have null values for some of your LocationID's then you may want to pre-test with:


IF IsNumeric(RsLocation("LocationID")) THEN
'Do work
END IF

Alex C
06-03-2004, 10:13 PM
Originally posted by CrazyC115
ASP makes life fun when dealing with numbers. The answer is:

1 = 1 <- True
1 = "1" <- False
"1" = "1" <- True

SO when comparing numbers I always toss both variables into a CLNG().


IF CLNG(KeepLocation2) = CLNG(RsLocation("LocationID")) THEN
'Do work
END IF


That should solve your problem. Be aware though CLNG will crash if it is not fed a numeric value, ie a null value from your RecordSet. If you have null values for some of your LocationID's then you may want to pre-test with:


IF IsNumeric(RsLocation("LocationID")) THEN
'Do work
END IF

That worked thank you very much :)

Can you explain to me though if, KeepLocation2 isn't even created UNTIL the LocationID value creates the actual dropdown box. Since it was the LocationID (an AutoNumber in MS Access database) I thought that KeepLocation2 would have continued to keep the same format.

Why is this not so?

Also, LocationID is created from an AutoNumber so they will always be numeric but thanks for that tip, I shall use that for other times!


Regards,
Alex

CrazyC115
06-03-2004, 10:28 PM
Originally posted by Alex C
That worked thank you very much :)

Can you explain to me though if, KeepLocation2 isn't even created UNTIL the LocationID value creates the actual dropdown box. Since it was the LocationID (an AutoNumber in MS Access database) I thought that KeepLocation2 would have continued to keep the same format.

Why is this not so?

Also, LocationID is created from an AutoNumber so they will always be numeric but thanks for that tip, I shall use that for other times!


Regards,
Alex

I'm assuming your are talking about when say the page first loads. Then KeepLocation2 would contain nothing as there was no form input done. I would do something like:


KeepLocation2 = Request.Form('location2')
IF KeepLocation2 = "" THEN KeepLocation2 = 0


That way KeepLocation2 will always be a number.

Alex C
06-03-2004, 11:47 PM
Originally posted by CrazyC115
I'm assuming your are talking about when say the page first loads. Then KeepLocation2 would contain nothing as there was no form input done. I would do something like:


KeepLocation2 = Request.Form('location2')
IF KeepLocation2 = "" THEN KeepLocation2 = 0


That way KeepLocation2 will always be a number.
Sort of, but I was more meaning. The dropdown menu values are created from the database values. The KeepLocation2 value is created from the dropdown menu value. So why when you compare KeepLocation2 to the database value are the variable types different?


Thanks,
Alex

CrazyC115
06-04-2004, 01:48 PM
Originally posted by Alex C
Sort of, but I was more meaning. The dropdown menu values are created from the database values. The KeepLocation2 value is created from the dropdown menu value. So why when you compare KeepLocation2 to the database value are the variable types different?


Thanks,
Alex

Simply because of how the variables are being passed between your HTML Form and your Server Side Script. The variable type is not preserved properly hense why you use the Convert Functions (CBOOL, CINT, CLNG etc.) to make sure the data is in the proper format.