VS 2010 [RESOLVED] HELP! Conversion from string "" to type 'Double' is not valid.-VBForums
Results 1 to 7 of 7

Thread: [RESOLVED] HELP! Conversion from string "" to type 'Double' is not valid.

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2011
    Location
    Australia
    Posts
    4

    Resolved [RESOLVED] HELP! Conversion from string "" to type 'Double' is not valid.

    Hi all,
    I'm a first year student and in my third week of tutorial work I've hit a landmine and after spending hours on it, I just can't seem to get around why I am getting this error now.

    Before I added the 'Msgbox' function, it worked fine however since adding it and executing a function on my form, the system crashes and produces the following error 'Conversion from string "" to type 'Double' is not valid.' near my 'dInitial = CDbl(txtInitial.Text)' line of coding.

    This is my coding below

    Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click

    'calculates the required Payments e.g. Monthly, Yearly and Total'

    Dim dInitial As Double
    Dim dMonthPay As Double
    Dim dYearlyPayment As Double
    Dim dTotalPayment As Double

    If txtInitial.Text <> "" Or txtLengthOfLoan.Text <> "" Or txtInterestRate.Text <> "" Then
    Else
    MsgBox("No data entered, please enter data")
    txtInitial.Focus()
    End If

    dInitial = CDbl(txtInitial.Text)
    dMonthPay = (CDbl(txtInitial.Text) * (CDbl(txtInterestRate.Text) / 100) / 12)
    lblMnthPay.Text = FormatNumber(dMonthPay + (txtInitial.Text / txtLengthOfLoan.Text / 12))

    dInitial = CDbl(txtInitial.Text)
    dYearlyPayment = (lblMnthPay.Text * 12)
    lblYrPay.Text = dYearlyPayment

    dInitial = CDbl(txtInitial.Text)
    dTotalPayment = (lblMnthPay.Text * 12 * txtLengthOfLoan.Text)
    lblTotPay.Text = dTotalPayment

    End Sub

    Help on this would be greatly appreciated.
    Thank you

  2. #2
    Addicted Member
    Join Date
    Oct 2009
    Posts
    212

    Re: HELP! Conversion from string "" to type 'Double' is not valid.

    To break this down to layman's terms....

    You're asking it to convert "" to a number. I'm not sure what number you think it should pick....

    If you convert "3" to a number its 3
    I'm not sure how it could convert "" to a number....

    While you test to see if txtInitial.Text <> "" you only show a messagebox, and then try to perform the conversion, perhaps you need to move the conversation/math code to a different place?

    While the following is not your solution it is something to know for the future....
    sometimes in the middle of a sub you need to exit the sub and you use "Exit Sub", but you need to move your mathmatical code somewhere inside your "test" to see if txtInitial.Tex <> "", I'm sure you can figure this out....use basic logic.
    Have you tried Google?

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    88,133

    Re: HELP! Conversion from string "" to type 'Double' is not valid.

    The error message is fairly self-explanatory: you can't convert an empty string to a number. This is an example of why a real-world application would very rarely use CDbl to convert the contents of a TextBox to a number. You're obviously clicking your Button without entering anything into your TextBox, which is just the sort of thing that real users do. It's for that reason that validation is essential in real-world apps.

    In a learning context though, you are often told things like "you can assume that all input is valid" so that you can concentrate on processing the data without having to worry about validation. If that's so in your case then you must make sure that you always enter valid data when testing.

    If you can't make that assumption then you must validate first. You already know that an empty string is invalid, but even a non-empty string doesn't guarantee a valid number. It's for that reason that most real-world apps will use a TryParse method, e.g. Double.TryParse, to both validate and convert in one pass.

    2007-2014

    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts) | WP8 Turnstile Feather Transition with Pivot Control
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Addicted Member
    Join Date
    Oct 2009
    Posts
    212

    Re: HELP! Conversion from string "" to type 'Double' is not valid.

    Also, it will probably serve you well to use...

    Messagebox.show("This is the .net way to show a message box")

    verse

    MsgBox("This is the VB6 way to show a message box")
    Have you tried Google?

  5. #5
    Addicted Member
    Join Date
    Oct 2009
    Posts
    212

    Re: HELP! Conversion from string "" to type 'Double' is not valid.

    jmcilhinney makes a great point (as usual)

    How do you convert "Hello World" to a number, simply isnt going to work, and it wont get caught in your "test" if then...
    Have you tried Google?

  6. #6

    Thread Starter
    New Member
    Join Date
    Mar 2011
    Location
    Australia
    Posts
    4

    Re: HELP! Conversion from string "" to type 'Double' is not valid.

    thanks for the quick reply everyone
    i just couldn't figure out why it wasn't working after entering the 'msgbox' but now i've figured out what to do.
    this forum is going to be very helpful
    thanks

  7. #7
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    88,133

    Re: [RESOLVED] HELP! Conversion from string "" to type 'Double' is not valid.

    I just had a closer look at your code and this part is dodgy for several reasons:
    vb.net Code:
    1. If txtInitial.Text <> "" Or txtLengthOfLoan.Text <> "" Or txtInterestRate.Text <> "" Then
    2. Else
    3.     MsgBox("No data entered, please enter data")
    4. txtInitial.Focus()
    5. End If
    First of all, you should NEVER have an empty If block. If you do, you should invert the condition, move the code from the Else block to the If block and get rid of the Else block.

    Second, you should almost always use AndAlso and OrElse in preference to And and Or.

    Third, as suggested by 7777, you don't exit out when you find an invalid state, so the subsequent code will be executed no matter what.

    Finally, your conditional code isn't doing what it should. It should be continuing only if all three fields are not empty, which is equivalent to not continuing if any field is empty. Your code doesn't test for either of those fields.

    2007-2014

    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts) | WP8 Turnstile Feather Transition with Pivot Control
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.