Question: I am having trouble with the below question. I have coded it but it seems to have problems with the decCommission.ToString command. Why is it
I am having trouble with the below question. I have coded it but it seems to have problems with the decCommission.ToString command. Why is it having this issue, and what is the best way to fix it? I will also post my code and the error message below.
6. Open the Commission Solution (Commission Solution.sln) file contained in the ClearlyVB2012\Chap15\Commission Solution folder. The Display buttons Click event procedure should display commission amounts in the txtCommission control. Display the commission amounts using sales amounts of $10,000 through $13,000 in increments of $1,000. Use rates from 2% through 5% in increments of 1%. Display the commission amounts by sales amount within rate. (In other words, display the four commission amounts for the 2% rate, then the four commission amounts for the 3% rate, and so on.) Code the procedure. Save the solution and then start and test the application. Close the Code Editor window and then close the solution.
The error message:
An unhandled exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll
Additional information: Conversion from string "C2" to type 'Integer' is not valid.
My current code:
Public Class frmMain
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click Me.Close() End Sub
Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click ' displays commision rates and amounts Dim decCommission
' clear commission box txtCommission.Text = String.Empty
' calculate and display commission For decRates As Decimal = 0.02 To 0.05 Step 0.01 txtCommission.Text = txtCommission.Text & ControlChars.NewLine & decRates.ToString("P0") & ":" & ControlChars.NewLine For intAmount As Integer = 10000 To 13000 Step 1000 decCommission = intAmount * decRates txtCommission.Text = txtCommission.Text & intAmount.ToString("C0") & " -> " & decCommission.ToString("C2") & ControlChars.NewLine Next intAmount Next decRates End Sub End Class
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
