I'm getting the error message Conversion from string "" to type 'Decimal' is not valid.
when trying to run this code
Call purchase.total(TextBox5.Text)
But textbox5.Text has the 1.99. Please help?
Printable View
I'm getting the error message Conversion from string "" to type 'Decimal' is not valid.
when trying to run this code
Call purchase.total(TextBox5.Text)
But textbox5.Text has the 1.99. Please help?
TextBoxes use Strings for the text property.
If you want to convert a 'number' in a textbox (which is a string) you have to use something like:
Dim item1 As Decimal = CDec(Me.TextBox1.Text)
Or:vb Code:
Dim item1 As Decimal = Decimal.Parse(Me.TextBox1.Text)
(also see TryParse)
Could have something to do with blank spaces too.
Ues TryParse
vb Code:
Dim total As Decimal Dim myStrTotal As String = "" 'If the myStrTotal is "" or it is some thing that 'can't be convertet to decimal than the "total" will be 0 Decimal.TryParse(myStrTotal, total)
You should use TryParse as VBDT suggests but it provides more than that. If you had to rely on testing the value afterwards you couldn't tell the difference between a value of zero and a failure. Normally if the operation failes you won't want to use zero, but rather notify the user of the failure and ask them to provide a valid value:vb Code:
Dim item1 As Decimal If Decimal.TryParse(Me.TextBox1.Text, item1) Then 'Use item1 here. Else MessageBox.Show("Please enter a valid numerical value.") End If