Solved

InvalidCastException was unhandled VB.NET visual studio 2008

Posted on 2014-03-06
7
1,583 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

810 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