An unhandled exception of type 'System.InvalidCastException'
Hello all. I’m trying to learn vb.net at home. I have a book with exercises and now I’m stuck because of this error:
An unhandled exception of type 'System.InvalidCastException' occurred in microsoft.visualbasic.dll
Additional information: Cast from string "233.3" to type 'Decimal' is not valid.
So there is code from a book:
PrivateSub btnCalculate_Click(ByVal senderAs System.Object, _
ByVal eAs System.EventArgs)Handles btnCalculate.Click
Dim dOrderTotalAsDecimal
Dim dDiscountPctAsDecimal
Dim dDiscountAmountAsDecimal
Dim dInvoiceTotalAsDecimal
dOrderTotal = txtOrderTotal.Text
If dOrderTotal >= 100Then
dDiscountPct = 0.2
Else
dDiscountPct = 0
EndIf
dDiscountAmount = dOrderTotal * dDiscountPct
dInvoiceTotal = dOrderTotal - dDiscountAmount
lblDiscountAmount.Text = dDiscountAmount
lblInvoiceTotal.Text = dInvoiceTotal
txtOrderTotal.Focus()
EndSub
If I put let say “233” in Text Box everything is fine, but if I try “233.3” this error occur. I’m using Visual Studio .NET 2003 Service Pack 1. If anybody can explain to me what is wrong and how I can fix it I would really appreciate, because as I mentioned this code is from a book and it supposed to work.
[6419 byte] By [
Ovod] at [2007-12-28]
you are trying to use a value that cannot be used on the decimal object, you should always validate your inputs and catch errors...but this is a talk on a different day.
the problem is, you need to convert the input string to a decimal type! :-) so....
dOrderTotal = Convert.ToDecimal(Me.txtOrderTotal.Text)
I tried your solution and I get this error:
An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll
Additional information: Input string was not in a correct format.
On another forum somebody suggest this:
dOrderTotal = Val(txtOrderTotal.Text)
It works, but as I mentioned I’m trying to learn VB at home and I’m really curios why code from a book doesn’t work. There nothing in the book about problem with . character.
I hardly think
dOrderTotal = Convert.ToDouble(Me.txtOrderTotal.Text)
is an answer when dOrderTotal was declared as decimal.
I suspect the problem is because of location. Are you in a country where the decimal separator is normally , (comma) rather than . (point). Using a . instead of , in such a locale will give the errors described.
Ok I changed variable to the Double type and tried this:
dOrderTotal = Convert.ToDouble(Me.txtOrderTotal.Text)
I got this error:
An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll
Additional information: Input string was not in a correct format.
Dave299 thanks for your post, I tried “,” instead of “.” end everything works without any conversion. But can anybody explain why Convert.ToDouble didn’t work?
Hi.
I'm glad you have got it to work finally.
I don't have VS 2003 so I cannot be certain of the following but in VB Express (which is what I use) your original code would have tried to do an implicit conversion from the string contained in the textbox to the decimal type defined for dOrderTotal. It would be able to do this provided the string was in a format which was recognised as a number. From your response it appears that in your locale you use "," as the decimal separator so trying to use "." instead would cause the conversion to fail because that would not be recognised as a number.
Doing an explicit conversion, e.g. Convert.ToDecimal would also fail for the same reason. It would make no difference which conversion you tried to do - double, single, decimal - they would all fail because the string being converted is in the wrong format. I cannot explain why ahmedilyas thought it would make a difference.
If the book you are using is of American origin your easiest option might be to go into Control Panel, Regional and Language Options and change to English (United States) whenever you wish to use code from the book.