Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

InvalidCastException was unhandled VB.NET visual studio 2008

Posted on 2014-03-06
7
Medium Priority
?
1,657 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 21

Accepted Solution

by:
Craig Wagner earned 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

604 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