Search code examples
asp-classicquery-stringconstantsmismatch

Inconsistent results from ASP classic


I have a somewhat unnerving problem when using constant values. I have a simple page called "test.asp" which sets a variable to a constant and then to a querystring value (if it exists). Then a select case checks if the variable matches the constant and outputs a message "matched". The problem is that asp does not recognise when the querystring value is the same as the constant. I have run 2 attempts, "test.asp" and "test.asp?SortField=1". The first attempt runs as expected but the second attempt reports that 1 is not the same as 1. The code for this page is as follows.

<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<head></head>
<body>

<%
const cSortFielda = 1
dim vSortField

vSortField = cSortFielda
if not isempty(Request("SortField")) then
    vSortField = Request("SortField")
    end if

select case vSortField
    case cSortFielda
        response.write "matched</br>"

    case else
        response.write "failed</br>"
        response.write "vSortField = " & vSortField & "(" & asc(vSortField) & ") </br>"
        response.write "cSortFielda = " & cSortFielda & "(" & asc(cSortFielda) & ") </br>"
        response.write "vSortField = cSortFielda is " & (vSortField = cSortFielda) & "</br>"

        end select
%>

</body>
</html>

Am I missing something blatantly obvious or is asp classic at fault?

Thank you for any assistance.


Solution

  • You're comparing a string with a number. You need to make sure they are consistent. So either change this line:

    const cSortFielda = 1
    

    To:

    const cSortFielda = "1"
    

    Or change this line:

    vSortField = Request("SortField")
    

    To:

    vSortField = CLng(Request("SortField"))
    

    The first example compares string to string, and the second example compares number to number.