Input string was not in a correct format. vb.net

-1

I am trying to sum the contents of two text boxes one of them having data from a database but I keep getting the error above with these details

    System.FormatException
     HResult=0x80131537
     Message=Input string was not in a correct format.
     Source=mscorlib
     StackTrace:

    at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)

    at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)

   at System.Int32.Parse(String s)

     at SchoolManager.Pay_Fees.TxtTermFee_TextChanged(Object sender, EventArgs e) in C:\Users\MASENO\Source\Repos\SchoolManager\SchoolManager\Pay Fees.vb:line 271

    at System.Windows.Forms.Control.OnTextChanged(EventArgs e)

   at System.Windows.Forms.TextBoxBase.OnTextChanged(EventArgs e)

   at System.Windows.Forms.Control.set_Text(String value)

  at System.Windows.Forms.TextBoxBase.set_Text(String value)

  at System.Windows.Forms.TextBox.set_Text(String value)

  at SchoolManager.Pay_Fees.InitializeComponent() in 

  C:\Users\MASENO\Source\Repos\SchoolManager\SchoolManager\Pay Fees.Designer.vb:line 158
 at SchoolManager.Pay_Fees..ctor() in C:\Users\MASENO\Source\Repos\SchoolManager\SchoolManager\Pay Fees.vb:line 9

When I test the textbox with the data from the database using the tryparse() methord I get true meaning the content is avalid integer

       TxtTotalFee.Text = Integer.TryParse(TxtPBalance.Text,0)

How can I get to add the two data?

Here is my code

   Private Sub TxtTermFee_TextChanged(sender As Object, e As EventArgs) Handles TxtTermFee.TextChanged
    TxtTotalFee.Text = Integer.Parse(TxtPBalance.Text) + Integer.Parse(TxtTermFee.Text)
End Sub

Thanks.

vb.net
textbox
asked on Stack Overflow Mar 14, 2019 by (unknown user) • edited Mar 14, 2019 by (unknown user)

1 Answer

1

You are on the right track with the tryparse() except that you are not using the converted value.

change your code to this.

    Private Sub TxtTermFee_TextChanged(sender As Object, e As EventArgs) Handles TxtTermFee.TextChanged
    ' Input String.
    Dim value As String = TxtPBalance.Text

    ' Use Integer.TryParse.
    Dim i As Integer
    If (Integer.TryParse(value, i)) Then
        If TxtTermFee.Text = "" Then
        Else
            TxtTotalFee.Text = i + TxtTermFee.Text
        End If

    End If
End Sub
answered on Stack Overflow Mar 14, 2019 by The Only Smart Boy

User contributions licensed under CC BY-SA 3.0