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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

vbscript type mismatch 1

Status
Not open for further replies.

ppetree

Programmer
Mar 3, 2007
60
US
Hey Gang!

Would someone kindly help me understand this little problem:

Code:
'See if this is a single system purchase
u1 = Trim(Request.Form("q1"))
If u1 = 0 Then
    'If we got here, we are looking for quantity purchases
	u2  = Trim(Request.Form("q2"))  'This would be the starter pak
    If u2 = 0 Then
        'If q2 is empty we failed our form validation or this is a bogus submit
        Response.Redirect("/purchase")
    Else
        u3  = Trim(Request.Form("q3"))
        u4  = Trim(Request.Form("q4"))
        u5  = Trim(Request.Form("q5"))
        u6  = Trim(Request.Form("q6"))
        u7  = Trim(Request.Form("q7"))
        u8  = Trim(Request.Form("q8"))
        u9  = Trim(Request.Form("q9"))
        u10 = Trim(Request.Form("q10"))
        u11 = Trim(Request.Form("q11"))
        u12 = Trim(Request.Form("q12"))
        u13 = Trim(Request.Form("q13"))
        u14 = Trim(Request.Form("q14"))
        u15 = Trim(Request.Form("q15"))
        u16 = Trim(Request.Form("q16"))
	End If
End If
More stuff like database query goes here
Code:
If u1 > 0 Then
    Response.Write("<tr><td>" &prodRows(P_num, 0) &"</td><td>" &prodRows(P_desc, 0) &"</td><td>" &prodRows(P_price, 0) &"</td><td>" &u1 &"</td></tr>")
    nTotal = prodRows(P_price, 0) * u1
Else
    Response.Write("<tr><td>" &prodRows(P_num, 1) &"</td><td>" &prodRows(P_desc, 1) &"</td><td>" &prodRows(P_price, 1) &"</td><td>" &u1 &"</td></tr>")
    nTotal = prodRows(P_price, 1) * u2
    Response.Write("Sub total: " &nTotal &"<br>")
End If    

If u3 > 0 Then
    Response.Write("<tr><td>" &prodRows(P_num, 2) &"</td><td>" &prodRows(P_desc, 2) &"</td><td>" &prodRows(P_price, 2) &"</td><td>" &u1 &"</td></tr>")
    nTotal = nTotal + prodRows(P_price, 2) * u3
    Response.Write("Sub total: " &nTotal)
End If

At the If u3 > 0 Then... statement just above I get a type mismatch '[string: ""]'

The rules enforced on the form side is that either the u1 or u2 fields must have a number greater than 0 and only if u2 is greater than 0 can u3 - u16 possibly have a numeric value and then only one of u3-u16 can have a numeric value.

The question is why am I getting a type mismatch when comparing a null (according to the error message) value against zero (of course I know that Null and Zero are different but how do you cast against this?)?
 
The thing you need to remember about ASP is that all variables are variants. This means that they can take on ANY data type. So, if you treat a variable as a string, then it will become a string. If you treat a variable as a integer, then it will become an integer. If you mix the 2, you're begging for trouble.

For example, this works:

Code:
<%

Dim Anything
	
If Anything > 0 Then
	Response.Write("Anything is greater than 0.")
End If

%>

But this doesn't:

Code:
<%

Dim Anything

Anything = ""
If Anything > 0 Then
	Response.Write("Anything is greater than 0.")
End If
%>

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
George,

That makes sense but flies in the face of expectation... it was my understanding that vb would evaluate a variable either way (like perl and java) but from what you are telling me once vb makes up its mind that a variable is of one type then it stays that type forever.?.?

So the solution, if I understand the problem correctly, is to cast u1...u16 using cInt(Trim(Request.Form("q13")))

Correct?

Phil
 
Well....

You can change the data type willy nilly, like this...

Code:
<%

	Dim Anything
	
	Response.Write(TypeName(Anything) & "<br />")

	Anything = ""
	Response.Write(TypeName(Anything) & "<br />")

	Anything = 3
	Response.Write(TypeName(Anything) & "<br />")

	Anything = 3.14159
	Response.Write(TypeName(Anything) & "<br />")

%>

The problem, as I see it, is that comparison operators will not type convert for the purposes of a comparison. So, you can change the data type by treating it as another data type, but you can't necessarily compare them.

To be honest, I'm a big VB6/SQL Server guy. I've been learning ASP for the past couple months. That being said, There may be a better way of doing this. I do know this, you cannot convert an empty string to an integer because you will get an error. Here's one way of solving your problem.

Code:
<%
Anything = ""
If IsNumeric(Anything) Then
    Anything = cInt(Anything)
Else
    Anything = 0
End If
%>

Basically, if the variable is numeric, the convert it to integer, otherwise set it to 0. This may not be the most appropriate way to accomodate this, but it may.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
George,

Here is what I ended up with:

Code:
'Get all quantities, convert NULLs to Zeros
For i=0 to 15
   quantities(i) = Trim(Request.Form("q" &i+1))
   If IsNumeric(quantities(i)) Then
       quantities(i) = cInt(quantities(i))
   Else
       quantities(i) = 0
   End If
Next

If quantities(0) = 0 Then
    'If we got here, we are looking for quantity purchases
    If quantities(1) = 0 Then
        'If q2 is empty we failed our form validation 
        'or this is a bogus submit
        Response.Redirect("/purchase")
	End If
End If

And of course the problem has gone away!

You are a gentleman and a scholar!

Thanks,

Phil
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top