Solved

InvalidCastException was unhandled VB.NET visual studio 2008

Posted on 2014-03-06
7
1,573 Views
Last Modified: 2014-03-06
Hi all.

I'm getting the following error in my windows form created in visual studio using vb.net:

InvalidCastException was unhandled: Conversion from string "" to type 'Double' is not valid.

And it points to the line of code: ElseIf CDbl(txtQty4.Text) <= CDbl(txtQty2.Text) Then

Here is the entire Sub:

 If txtQty4.Text = "999999" Then
            txtQty5.Text = Nothing
            txtQty6.Text = Nothing
            txtQty7.Text = Nothing
            txtQty8.Text = Nothing
            txtQty9.Text = Nothing
            txtQty10.Text = Nothing

ElseIf CDbl(txtQty4.Text) <= CDbl(txtQty2.Text) Then
txtQty3.Text = Nothing
            txtQty4.Text = Nothing
            txtQty5.Text = Nothing
            txtQty6.Text = Nothing
            txtQty7.Text = Nothing
            txtQty8.Text = Nothing
            txtQty9.Text = Nothing
            txtQty10.Text = Nothing
Else
            txtQty5.Text = txtQty4.Text + 1
            txtQty6.Text = 999999
 End If

Open in new window


These are two textboxes where the end user enters a quantity amount. By default, txtQty2 is populated with 999999. If the number in txtQty2 is changed to another number (say 500) then txtQty4 is populated with 999999, but if the user changes the content in txtQty4 to a number less than txtQty2 then that's what the else if is there for.
Why is this causing an error?
0
Comment
Question by:printmedia
  • 5
  • 2
7 Comments
 
LVL 21

Expert Comment

by:Craig Wagner
ID: 39910180
You're getting the error because the textbox is empty and CDbl cannot convert the empty string (i.e. "") into a number (just like the error message is telling you). You can get the exact same error message by writing:

Dim bar = CDbl("")

You're going to need to check if the value is an empty string before converting it to a number. You might want to write a utility function to avoid cluttering your code with checking for empty value.

    Function ToDouble(valueToConvert As Object, defaultValue As Double) As Double
        Dim result As Double

        If (Not Double.TryParse(valueToConvert, result)) Then
            result = defaultValue
        End If

        Return defaultValue
    End Function

Open in new window

0
 

Author Comment

by:printmedia
ID: 39910183
But it's not empty, the else if fires because they entered a number in txtQty4 that is less than txtQty2, the field is not blank.
0
 

Author Comment

by:printmedia
ID: 39910200
By the way txtQty2 and txtQty4 are initially loaded like this:

txtQty2.Text = 999999
txtQty4.Text = 999999

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 21

Accepted Solution

by:
Craig Wagner earned 500 total points
ID: 39910228
I don't care what they're initially set to. When that line of code executes one of them must be an empty string. That could happen due to a bug elsewhere in the code that is blanking out that property or it could be due to the user deleting the contents of the textbox.

Have you used the debugger to step through the code and examine the contents of both at the time the code executes?
0
 

Author Comment

by:printmedia
ID: 39910229
It should be noted this code is in the txtQty4 LEAVE event.
0
 

Author Comment

by:printmedia
ID: 39910233
I'm new to Visual Studio so I haven't stepped through the code. How can I do that?
0
 

Author Closing Comment

by:printmedia
ID: 39910273
Thanks Craig. There was a bug in my code, when it clears the textbox it was going back to the LEAVE event and that's where the error was. So I added code to check if the textbox is empty then exit. And that fixed it.

I would still like to know how I can step through the debugger to see the contents of the form for future reference.

Thanks again!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now