Solved

InvalidCastException was unhandled VB.NET visual studio 2008

Posted on 2014-03-06
7
1,551 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
Comment Utility
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
Comment Utility
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
Comment Utility
By the way txtQty2 and txtQty4 are initially loaded like this:

txtQty2.Text = 999999
txtQty4.Text = 999999

Open in new window

0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 21

Accepted Solution

by:
Craig Wagner earned 500 total points
Comment Utility
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
Comment Utility
It should be noted this code is in the txtQty4 LEAVE event.
0
 

Author Comment

by:printmedia
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 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

11 Experts available now in Live!

Get 1:1 Help Now