Question

I'm using asp.net vb and website is on client side. I have no idea how to perform this calculation. The website will set a payment plan. The time frame is 12 months and the minimum amount of a payment can be 25, but if they enter more than 25 is okay. 1. If the balance is less than 25 then a label will show them that they can't set pmt plan, but then if I change it to the amount of the balance, nothing happens and it still shows the error of the label and doesn't let me move on if I have it right. 2. How can I make sure the user stays in between guidelines or they cannot move on to the congratulations page? Below is my code but it doesn't work, my logic doesn't work. Can someone please help me with this. This is the default page:

    <form id="form1" runat="server">
<div>
Enter balance<br />
    <asp:TextBox ID="Balance" runat="server"></asp:TextBox>
    <br />
    Enter amount to pay<br />
    <asp:TextBox ID="PmtAmount" runat="server" AutoPostBack="True"></asp:TextBox>
    <br />
    <asp:Label runat="server" ID="lblError" Text=""></asp:Label>
    <br />
    <br />
    <asp:Button ID="Button1" runat="server" Text="submit" Width="90px" />
</div>
</form>

Code behind:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    If Page.IsPostBack Then
    End If
End Sub

Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim pmt As Decimal
    Dim bal As Decimal
    Dim minpmt As Decimal

    If Not Decimal.TryParse(Balance.Text, bal) OrElse _
       Not Decimal.TryParse(PmtAmount.Text, pmt) Then
        lblError.Visible = True
        lblError.Text = "Balance and Payment Amount must be a decimal"
    Else
        If bal < 25.0 Then
            lblError.Visible = True
            lblError.Text = "You can't set a pymt plan, please pay in full"
        ElseIf pmt < 25.0 Then
            lblError.Visible = True
            lblError.Text = "min is 25.00"
        ElseIf bal > 300.0 Then
            minpmt = bal / 12
            lblError.Visible = True
            lblError.Text = "your min pmt is " & Math.Round(minpmt, 2)
        ElseIf pmt > (bal / 12) Then
            Response.Redirect("default2.aspx")
        End If
    End If
Was it helpful?

Solution

The following pseudocode should help get you going in the right direction:

if balance is less than 25 then
    show label with text "You can't set a payment plan"
else if payment is less then 25 then
    show label with text "Minimum payment is 25"
else if paymentamount > balance / 12 then
    redirect to page 2

Also, doing things like pmt = Balance.Text / TF is dangerous - if Balance.Text is not numeric, you will get an exception.

I suggest using Decimal.TryParse on both Balance.Text and PmtAmount.Text to convert the text to a decimal (or show an error if the conversion fails).

EDIT

If Decimal.TryParse(Balance.Text, bal) < 25.0 Then is not the correct way to use Decimal.TryParse.

Decimal.TryParse returns a boolean and stores the result of the conversion in the out parameter, so you need to do something like this:

Dim bal As Decimal

If Not Decimal.TryParse(Balance.Text, bal) Then
   ' Set the error label letting the user know to enter a number
End If

If the conversion was successful, bal will have the result of the conversion, and you can use it later in your code.

You can modify the pseudo-code above to something like this:

if balance is not a number or payment amount is not a number
    show label with text "balance and payment amount must be a decimal value"
else
    if balance is less than 25 then
        show label with text "You can't set a payment plan"
    else if payment is less then 25 then
        show label with text "Minimum payment is 25"
    else if paymentamount > balance / 12 then
        redirect to page 2

In a nutshell, use Decimal.TryParse on both balance and payment amount - if either one fails, show an error message.

Otherwise (else) do the rest of your validation using the converted values contained in the out variables.

Code

You're on the right track - your final code would look something like this:

Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim TF As Decimal = 12  'TimeFrame 
    Dim Min As Decimal = 25  'Minimum payment plan amount
    Dim pmt As Decimal
    Dim bal As Decimal

    If Not Decimal.TryParse(Balance.Text, bal) OrElse _
       Not Decimal.TryParse(PmtAmount.Text, pmt) Then
        lblError.Visible = True
        lblError.Text = "Balance and Payment Amount must be a decimal"
    Else
        If bal < 25.0 Then
            lblError.Visible = True
            lblError.Text = "You can't set a pymt plan, please pay in full"
        ElseIf pmt < 25.0 Then
            lblError.Visible = True
            lblError.Text = "min is 25.00"
        ElseIf pmt > (bal / 12) Then
            Response.Redirect("default2.aspx")
        End If
    End If
End Sub
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top