12,356,331 members (68,290 online)
Rate this:
See more:
I want to calculate total. For this I am using following code undertxtDisc_TextChanged event.

```qty = Convert.ToInt32(txtQty.Text)
rate = Convert.ToInt32(txtRate.Text)
disc = Convert.ToInt32(txtDisc.Text)

total = qty * rate
amt = total / disc
total1 = total - disc

txtAmt.Text = Convert.ToInt32(total1)```

Code calculate correct total when i put disc > 0, i.e If I use disc = 0 then system throws an error :Arithmetic operation resulted in an overflow.

Another problem is If first time I put disc = 12 & before move forward I changed it to 10 then system throws an error : Input string is not in correct format.
Posted 10-Jan-13 3:55am
Edited 10-Jan-13 3:58am
v2

Rate this:

## Solution 2

You must ensure all input values were correct before performing calculation.
I suggest TryParse instead Try Catch block

```If Integer.TryParse(txtQty.Text, qty) AndAlso Integer.TryParse(txtRate.Text, rate) AndAlso Integer.TryParse(txtDisc.Text, disc) AndAlso (disc > 0) Then
' Do calculation here
total = qty * rate
amt = total / disc ' I suggest: amt = total \ disc
total1 = total - disc
txtAmt.Text = Convert.ToInt32(total1).ToString()
Else
' Something wrong in input parameter
' Do not calculate, you can give a MessageBox to tell user
End If```
Rate this:

## Solution 1

Because you are dividing by zero?

Try:

```If total <> 0 And disc <> 0 Then
amt = total / disc
Else
amt = 0
End If
```

I don't get what you mean by the second question, '& before move forward I changed it to 10 ' - can you explain in more detail?
v4

Top Experts
Last 24hrsThis month
 OriginalGriff 320 ErBhati 195 Richard Deeming 175 Sergey Alexandrovich Kryukov 173 Karthik Bangalore 115
 OriginalGriff 9,958 Sergey Alexandrovich Kryukov 6,606 Dave Kreskowiak 3,269 ppolymorphe 2,441 Karthik Bangalore 2,381