mardi 5 mai 2015

A first chance of exception type 'System.FormatException' occurred in mscorlib.dll

I am having some major issues with a program I am writing. I have gotten this far but I can't find the issue with my code at all. The program is supposed to calculate the total cost of an appliance.

All of the appliances in the list work except for the "Washer" which requires me to take the "CostPerGallon" and the "GallonsUsed" and multiply them to find a WaterCost. I add the WaterCostand the "CostPerDay" I've calculated to find the total. For some reason I'm getting this exception ONLY when I try to calculate the Washer cost.

I have tried to find where the exception is occurring, but I am unable to pinpoint exactly where. The best guess I can come up with is the nested "IF" statement that does the calculations for dblCostPerDay and dblWaterCost and then adds them together.

I have put TryParse into both the "GallonsUsed" and "CostPerGallon" inputs to try and catch and issues, but that's still not working. Here is the code:

    ElseIf lstAppliances.SelectedItem = "Washer" Then
        If Double.TryParse(txtGallonsUsed.Text, dblGallonsDay) = False Then
            lblErrorMsg.Text = "Gallons Used must be a number."
            lblErrorMsg.Visible = True
        Else
            dblGallonsDay = Convert.ToDouble(txtGallonsUsed.Text)
        End If
    Else 'If all of the inputs pass validations, the calculation will take place
        dblCostPerHour = Convert.ToDouble(txtCostPerHour.Text)
        dblHoursPerDay = Convert.ToDouble(txtHoursDay.Text)
        dblPowerNeeded = Convert.ToDouble(txtPowerNeeded.Text)
        If lstAppliances.SelectedItem = "Washer" Then
            dblCostPerDay = dblCostPerHour * dblHoursPerDay * dblPowerNeeded
            dblWaterCost = dblCostPerGallon * dblGallonsDay
            dblCostPerDay = dblCostPerDay + dblWaterCost
        Else
            dblCostPerDay = dblCostPerHour * dblHoursPerDay * dblPowerNeeded
        End If
        dblTotalCost = dblTotalCost + dblCostPerDay
        txtApplianceSelection = lstAppliances.SelectedItem.ToString
        txtListString = (txtApplianceSelection & "     " & dblHoursPerDay & "     $" & Math.Round(dblCostPerDay, 2) & "     $" & Math.Round(dblTotalCost, 2)) 'Here we build our string
        ListView1.Items.Add(txtListString)

    End If
End Sub

Aucun commentaire:

Enregistrer un commentaire