Link to home
Start Free TrialLog in
Avatar of Jacque Scott
Jacque ScottFlag for United States of America

asked on

Subtracting numbers from two text boxes

I dynamically populate rows of data.  I need to get the amount due for a line item.  

Items from DB:
OriginalAmount = Dollar amount owed
totalPayment = How much has been paid so far.

Input items:
txtAmtPatientPaid = input box
txtAmtPaid = input box

The user fills out the form and I use Javascript to update the AmtDueTemp field which works correctly.  When the user hits the 'Save' button the validation is run on the .aspx.vb page.  If there is an error I want all populated fields to remain populated on postback.

If I only subtract the amount from the DB it works but now I need to subtract the two input box fields. How can I do the calculation below?  I believe I need to change the field type to a number but I don't know how.

Any suggestions?
dblAmtDueTemp = FormatNumber(rd("OriginalAmount") - rd("totalPayment") - Request.Form("txtAmtPatientPaid" & i) - Request.Form("txtAmtPaid" & i), 2)

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jacque Scott

ASKER

I still get the same error:

Input string was not in a correct format
I did not use the 'Try' or 'Catch'.  Would that make a difference?

I also used convert.ToDouble and got the same error.
Once I added the 'Try' and 'Catch' it worked.

What does that do?
It shouldn't make a difference--you just will have an unhandled exception if the values passed to Convert.ToDecimal (or ToDouble) do not represent valid numbers.

See if this works better:

Try
    dblAmtDueTemp = Convert.ToDouble(rd("OriginalAmount")) - Convert.ToDouble(rd("totalPayment")) - Convert.ToDouble(Request.Form("txtAmtPatientPaid" & i)) - Convert.ToDouble(Request.Form("txtAmtPaid" & i))
Catch ex As System.FormatException
    ' Conversion failed
End Try

Open in new window

I thought it worked but it doesn't.  Because of the 'Try' and 'Catch' it gives a number and not an error but it doesn't calculate correctly and then all the rows get the same number.
With which version?
Both.

I ended up doing the below:
If Request.Form("txtAmtPatientPaid" & i) = "" Then
                                                dblAmtPatientPaidtemp = 0
                                            Else
                                                dblAmtPatientPaidtemp = Request.Form("txtAmtPatientPaid" & i)
                                            End If
                                            If Request.Form("txtAmountPaid" & i) = "" Then
                                                dblAmtPaidtemp = 0
                                            Else
                                                dblAmtPaidtemp = Request.Form("txtAmountPaid" & i)
                                            End If
                                            dblAmtDueTemp = FormatNumber(rd("OriginalAmount") - rd("totalPayment") - (dblAmtPatientPaidtemp + dblAmtPaidtemp), 2)

Open in new window