1

I'm Trying to use event textbox change but I have an error "Input string was not in a correct format "

Is there something wrong with my code ?

or

Is there something wrong with my code implementation ?

Please Guide me

Thanks

Public Class Payment
    Private salesservices As New SalesService()
    Private Sub Payment_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim sales = salesservices.GetSales(LblInvono.Text)
        LabelTotalvalue.Text = sales.Totalinv.ToString("N0")
        txtDiscount.Text = sales.DiscountTotal.ToString("N0")
        txtPayment.Text = (sales.CashPayment + sales.NCashPayment).ToString("N0")
        LabelChange.Text = ((sales.CashPayment + sales.NCashPayment + sales.DiscountTotal) - sales.Totalinv).ToString("N0")
    End Sub
Private Sub OnTextChanged(sender As Object, e As EventArgs) Handles txtPayment.TextChanged, txtDiscount.TextChanged
        Dim payment As Double = 0
        Dim discount As Double = 0
        If Not String.IsNullOrEmpty(txtPayment.Text) OrElse Not String.IsNullOrEmpty(txtDiscount.Text) Then
           'Error code below this line  
           payment = Convert.ToDouble(txtPayment.Text)
            discount = Convert.ToDouble(txtDiscount.Text)
            LabelChange.Text = (Convert.ToDouble(LabelTotalvalue.Text) - discount - payment).ToString("N0")
        Else
            LabelChange.Text = "-" & Convert.ToDouble(LabelTotalvalue.Text).ToString("N0")
        End If
    End Sub
    Private Sub OnText_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtPayment.KeyPress, txtDiscount.KeyPress
        If Not Char.IsControl(e.KeyChar) AndAlso Not Char.IsDigit(e.KeyChar) Then
            e.Handled = True
        End If
    End Sub
    Private Sub txtPayment_KeyUp(sender As Object, e As KeyEventArgs) Handles txtPayment.KeyUp
        txtPayment.Text = txtPayment.Text.Replace(",", "")
        Dim discount As Double = 0
        Dim payment As Double = 0
        If Not String.IsNullOrEmpty(txtPayment.Text) Then
            payment = Convert.ToDouble(txtPayment.Text)
            LabelChange.Text = (Convert.ToDouble(LabelTotalvalue.Text) - discount - payment).ToString("N0")
            txtPayment.Text = Convert.ToDouble(txtPayment.Text).ToString("N0")
            txtPayment.[Select](txtPayment.Text.Length, 0)
        End If
    End Sub
    Private Sub txtDiscount_KeyUp(sender As Object, e As KeyEventArgs) Handles txtDiscount.KeyUp
        txtDiscount.Text = txtDiscount.Text.Replace(",", "")
        Dim discount As Double = 0
        Dim payment As Double = 0
        If Not String.IsNullOrEmpty(txtDiscount.Text) Then
            discount = Convert.ToDouble(txtDiscount.Text)
            LabelChange.Text = (Convert.ToDouble(LabelTotalvalue.Text) - discount - Payment).ToString("N0")
            txtDiscount.Text = Convert.ToDouble(txtDiscount.Text).ToString("N0")
            txtDiscount.[Select](txtDiscount.Text.Length, 0)
        End If
    End Sub
End Class
6
  • 2
    It's recommended to use Decimal for financial purposes. Also, use TryParse instead such as Decimal.TryParse
    – user246821
    Commented Jul 8 at 4:28
  • 3
    The first thing you should have done was look at the string in question and that's the first thing you should have told us too. If you're told that it's in the wrong format, knowing what format it's in would be the place to start. Commented Jul 8 at 4:40
  • 2
    Your OnTextChanged is shadowing a method on your form. You should fix that. Commented Jul 8 at 8:12
  • @Enigmativity , Thank you for your response. Your OnTextChanged is shadowing a method on your form You are right, how do I fix the warning?
    – dlaksmi
    Commented Jul 8 at 8:30
  • 1
    @dlaksmi - Change the name of the method. Maybe TextBox_OnTextChanged? But also keep in mind that the On* naming convention is for the raising of events, not the handling. You should probably call it TextBox_TextChanged. Commented Jul 8 at 23:15

2 Answers 2

-1

Method Convert.ToDouble(...) works only if you are sure that the string contains a numerical value. Otherwise, it's better to use Double.TryParse method, which, as the documentation says, 'does not throw an exception if the conversion fails'.

Here's an example of how you can rewrite the lines that causes the error. Instead of:

payment = Convert.ToDouble(txtPayment.Text)

write

Double.TryParse(txtPayment.Text, payment)
4
  • actually If Not String.IsNullOrEmpty(txtPayment.Text) OrElse Not String.IsNullOrEmpty(txtDiscount.Text) Then Double.TryParse(txtPayment.Text, payment) Double.TryParse(txtDiscount.Text, discount) Your answer with the answer @Enigmativity if I combine it gives me a solution
    – dlaksmi
    Commented Jul 11 at 8:44
  • @dlaksmi Glad to have been helpful!
    – Calaf
    Commented Jul 11 at 9:13
  • I'm just confused in marking the answers in this post because the 2 answers provide me with a solution
    – dlaksmi
    Commented Jul 11 at 9:35
  • @Calaf - Please take the details in my answer and add to yours. I'd be happy for you to get the up-vote and the accepted answer. Commented Jul 11 at 23:34
-1

You need AndAlso, not OrElse.

If Not String.IsNullOrEmpty(txtPayment.Text) AndAlso Not String.IsNullOrEmpty(txtDiscount.Text) Then

Or this:

If Not (String.IsNullOrEmpty(txtPayment.Text) OrElse String.IsNullOrEmpty(txtDiscount.Text)) Then

Your current code is explicitly requiring one to be Nothing (or Null).

3
  • In this way, if there is a payment without a discount, it will not be saved.
    – Calaf
    Commented Jul 8 at 8:31
  • @Calaf - It seems that the OP is mandating a discount. I don't see what you're saying. Commented Jul 8 at 10:02
  • @Enigmativity , actually If Not String.IsNullOrEmpty(txtPayment.Text) OrElse Not String.IsNullOrEmpty(txtDiscount.Text) Then Double.TryParse(txtPayment.Text, payment) Double.TryParse(txtDiscount.Text, discount) Your answer with the answer @Calaf if I combine it gives me a solution
    – dlaksmi
    Commented Jul 11 at 8:43

Not the answer you're looking for? Browse other questions tagged or ask your own question.