Solved

InvalidCastException was unhandled VB.NET visual studio 2008

Posted on 2014-03-06
7
1,605 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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

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

Suggested Solutions

Title # Comments Views Activity
Modal Popup Extender control 1 53
Help with error in Query 2 39
Code enhancement 4 34
VB.net VSTO Excel Ribbon error 4 14
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

696 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