Question Conversion from string "" to type 'Decimal' is not valid

nicole200718

Active member
Joined
Feb 2, 2011
Messages
35
Programming Experience
Beginner
I get this error: Conversion from string "" to type 'Decimal' is not valid. Here is where the error is happening please help?

decLarborCharge = CDec(txtLaborCharge.Text)

and here is the whole function that i have it in:

Function OtherCharges() AsDecimal
' This function returns the cost of labor and parts.
Dim decLarborCharge AsDecimal
decLarborCharge = CDec(txtLaborCharge.Text)
Return decLarborCharge
EndFunction
 
Hello,

The reason you are getting this error is because the txtLaborCharge textbox is blank, and VB can't convert an empty string to a number. You will likely want to check that the box is not blank. One way to do this is:

VB.NET:
    Function OtherCharges() As Decimal
        ' This function returns the cost of labor and parts.
        Dim decLarborCharge As Decimal

        If txtLaborCharge.Text = String.Empty Then
            txtLaborCharge.Text = "0"
        End If

        decLarborCharge = CDec(txtLaborCharge.Text)
        Return decLarborCharge
    End Function
You could, of course, do anything you wanted; this example just sets the labour charges to zero if nothing is entered.

You may also want consider using the IsNumeric function to check if txtLaborCharge.Text is actually a number before trying to convert it.
 
Back
Top