Solved

Label3.Text = Label1.Text * Label2.Text  -> Input string was not in a correct format

Posted on 2010-09-21
8
498 Views
Last Modified: 2012-05-10
I've been working on this half the day and it's ticking me off.

I've got four text boxes for input.  They are restricted to numeric values only with JavaScript.

Then there are three labels.

Label1 gets its value from TextBox1 * TextBox2
Label2 gets its value from TextBox3 * TextBox4
Label3 gets its valaue from Label1 * Label2

The work is done in the "CalculateButton" click event.

In the last IF/THEN,
If Double.TryParse(lblNumberTrainingHours.Text, 0) Then
is failing, causing tNumber to be zero thus making AmountReimbursed equal zero.

lblNumberTrainingHours.Text contains a correctly calculated value of "140" so I don't know why its failing the Double.TryParse.

This has gotten so convoluted in my efforts to make it calculate those few values and not crash if there are no values to calculate.  It could probably be done more correctly with, like three lines of code.  ;-)
Protected Sub btnRecalculate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRecalculate.Click
        Call UpdateTotals()
    End Sub

    Protected Sub UpdateTotals()
        '##################################################################################From txtTrainingPaymentPercent.TextChanged
        Dim Wage, Percent, Rate As Double
        If Double.TryParse(txtTraineeEntryWage.Text, 0) Then
            'Wage = Double.TryParse(txtTraineeEntryWage.Text, 0)
            Wage = CType(txtTraineeEntryWage.Text, Double)
        Else
            Wage = 0
        End If
        If Double.TryParse(txtTrainingPaymentPercent.Text, 0) Then
            'Percent = Double.TryParse(txtTrainingPaymentPercent.Text, 0)
            Percent = CType(txtTrainingPaymentPercent.Text, Double)
        Else
            Percent = 0
        End If

        Rate = Wage * Percent / 100

        lblTrainingPaymentHourlyRate.Text = Rate.ToString ' txtTraineeEntryWage.Text * txtTrainingPaymentPercent.Text / 100

        '##################################################################################From txtHoursPerWeek.TextChanged
        Dim tWeeks, tHours, tRate, tNumber, TrainingHours, AmountReimbursed As Double
        If Double.TryParse(txtLengthTrainingWeeks.Text, 0) Then
            'tWeeks = Double.TryParse(txtLengthTrainingWeeks.Text, 0)
            tWeeks = CType(txtLengthTrainingWeeks.Text, Double)
        Else
            tWeeks = 0
        End If
        If Double.TryParse(txtHoursPerWeek.Text, 0) Then
            'tHours = Double.TryParse(txtHoursPerWeek.Text, 0)
            tHours = CType(txtHoursPerWeek.Text, Double)
        Else
            tHours = 0
        End If
        If Double.TryParse(lblTrainingPaymentHourlyRate.Text, 0) Then
            'tRate = Double.TryParse(lblTrainingPaymentHourlyRate.Text, 0)
            tRate = CType(lblTrainingPaymentHourlyRate.Text, Double)
        Else
            tRate = 0
        End If
        If Double.TryParse(lblNumberTrainingHours.Text, 0) Then
            'tNumber = Double.TryParse(lblNumberTrainingHours.Text, 0)
            tNumber = CType(lblNumberTrainingHours.Text, Double)
        Else
            tNumber = 0
        End If

        TrainingHours = tWeeks * tHours
        AmountReimbursed = tRate * tNumber

        lblNumberTrainingHours.Text = TrainingHours.ToString ' tHours * tWeeks ' txtLengthTrainingWeeks.Text * txtHoursPerWeek.Text
        lblMaxAmountReimbursed.Text = AmountReimbursed.ToString ' tRate * tNumber ' lblTrainingPaymentHourlyRate.Text * lblNumberTrainingHours.Text


    End Sub

Open in new window

0
Comment
Question by:megnin
[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
  • 4
  • 4
8 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 33727946
You appear to be using TryParse incorrectly. The second parameter to TryParse is a reference paramter. If TryParse succeeds, then the ref parameter will already have the value as a double stored in it--there is no need to cast it using CType. Here is an example:
Dim value As String = "43.5"
Dim result As Double

If Double.TryParse(value, result) Then
    ' TryParse succeeded and result now is the double value 43.5. No cast required.
End If

Open in new window

0
 
LVL 1

Author Comment

by:megnin
ID: 33727986
Yes, I see.  Just goes to show, just because you "Google" something doesn't mean it's correct.  ;-)
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 33728038
>>  just because you "Google" something doesn't mean it's correct

That's what EE is for  ;)
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 1

Author Comment

by:megnin
ID: 33728096
I've got it like this now and it works:
If Double.TryParse(txtTraineeEntryWage.Text, Wage) Then
  ' Wage = CType(txtTraineeEntryWage.Text, Double)
Else
Wage = 0
End If
If I just say,
Double.TryParse(txtTraineeEntryWage.Text, Wage)
and the TryParse fails what with the value of Wage be?
Should I do this?
Dim Wage As Double = 0
Then if it fails will it just be "0"?
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 33728174
I think this will suit you and clarify:
Dim Wage As Double

If Not Double.TryParse(value, Wage) Then
    ' TryParse failed. Set value equal to zero
    Wage = 0
End If

' If TryParse succeeds, then the "If" is never entered. Because the
'  second param to TryParse is ByRef, Wage will already be set if
'  TryParse succeeds. If TryParse fails, then the "If" is entered
'  and Wage is set to zero

Open in new window

0
 
LVL 1

Author Comment

by:megnin
ID: 33728248
Thank you for the explanation.  That helps.
0
 
LVL 1

Author Closing Comment

by:megnin
ID: 33728261
Thank you for making it easy to understand.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 33728605
NP. Glad to help  :)
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

617 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