Tutorial :Exception from VB.NET Comparison Statement



Question:

Here is the exception:

"Conversion from string "" to type 'Double' is not valid."

Here is the line of code that that throws the exception (confirmed from stepping through in the debugger):

If LoanData.Item("ApplicationId") <> "" AndAlso LoanData.Item("ApplicationId") IsNot DBNull.Value Then  

Any ideas?


Solution:1

did you try LoanData.Item("ApplicationId").toString()?


Solution:2

LoanData.Item("ApplicationId") is returning a double probably.

You're trying to compare it with a String.

If this is the problem you could just do this:

LoanData.Item("ApplicationId").ToString() <> ""


Solution:3

The value stored in LoanData.Item("ApplicationId") must be of type Double.

The VB.NET compiler is letting you use the <> operator only because you must have Option Strict Off. It is assuming the value of LoanData.Item("ApplicationId") must be a String and is attempting to cast accordingly.

Since the value is not a String, you're getting an InvalidCastException.

Instead of using the <> operator, you can use Equals instead, which will result in a call to the Equals method of whatever type the value of LoanData.Item("ApplicationId") happens to have (or the Object.Equals method, if that type has not overridden it):

If Not LoanData.Item("ApplicationId").Equals("") AndAlso LoanData.Item("ApplicationId") IsNot DBNull.Value Then  

This should exhibit the closest possible behavior to what you currently have without resulting in an exception.


Solution:4

It's worth noting, btw, that VB6 would allow one to compare a double to a string directly, but this broke the transitive property of comparisons since "9" would be greater than "8Q", and "8Q" would be greater than the double 10.2 (since the double would be converted to a string in that case), but the double 10.2 would be greater than "9" (since the string would be converted to a double in that case). Eeks.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »