Conversion of string " to integer type is not valid

Asked

Viewed 3,679 times

1

I am creating a simple program that records the total sales and the value already earned with sales, however, after the user put the sale value the error program.

The error is as follows:

Conversion of string " to integer type is not valid

Before it was working right, however only had 1 IF, now I have in total 10 IF, which register according to the number of sales the value in different variables, the code in question:

Public Class addvenda
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles boxvenda.TextChanged

End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    If My.Settings.numerovendas = 0 Then
        My.Settings.vendas += 1
        My.Settings.numerovendas += 1
        My.Settings.venda1 = boxvenda.Text
        My.Settings.totalvendas += boxvenda.Text
        Me.Close()
    End If
    If My.Settings.numerovendas = 1 Then
        My.Settings.vendas += 1
        My.Settings.numerovendas += 1
        My.Settings.venda2 = boxvenda.Text
        My.Settings.totalvendas += boxvenda.Text
        Me.Close()
    End If
    If My.Settings.numerovendas = 2 Then
        My.Settings.vendas += 1
        My.Settings.numerovendas += 1
        My.Settings.venda3 = boxvenda.Text
        My.Settings.totalvendas += boxvenda.Text
        Me.Close()
    End If
    If My.Settings.numerovendas = 3 Then
        My.Settings.vendas += 1
        My.Settings.numerovendas += 1
        My.Settings.venda4 = boxvenda.Text
        My.Settings.totalvendas += boxvenda.Text
        Me.Close()
    End If
    If My.Settings.numerovendas = 4 Then
        My.Settings.vendas += 1
        My.Settings.numerovendas += 1
        My.Settings.venda5 = boxvenda.Text
        My.Settings.totalvendas += boxvenda.Text
        Me.Close()
    End If
    If My.Settings.numerovendas = 5 Then
        My.Settings.vendas += 1
        My.Settings.numerovendas += 1
        My.Settings.venda6 = boxvenda.Text
        My.Settings.totalvendas += boxvenda.Text
        Me.Close()
    End If
    If My.Settings.numerovendas = 6 Then
        My.Settings.vendas += 1
        My.Settings.numerovendas += 1
        My.Settings.venda7 = boxvenda.Text
        My.Settings.totalvendas += boxvenda.Text
        Me.Close()
    End If
    If My.Settings.numerovendas = 7 Then
        My.Settings.vendas += 1
        My.Settings.numerovendas += 1
        My.Settings.venda8 = boxvenda.Text
        My.Settings.totalvendas += boxvenda.Text
        Me.Close()
    End If
    If My.Settings.numerovendas = 8 Then
        My.Settings.vendas += 1
        My.Settings.numerovendas += 1
        My.Settings.venda9 = boxvenda.Text
        My.Settings.totalvendas += boxvenda.Text
        Me.Close()
    End If
    If My.Settings.numerovendas = 9 Then
        My.Settings.vendas += 1
        My.Settings.numerovendas += 1
        My.Settings.venda10 = boxvenda.Text
        My.Settings.totalvendas += boxvenda.Text
        Me.Close()
    End If

End Sub

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick

End Sub

End Class

1 answer

1


The error must be happening because the text that is in boxvenda.Text is empty and cannot be converted to a valid number.

I think this way you can avoid the error, and at the same time greatly optimize your code:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim dblBoxVenda As Double = 0

    If My.Settings.numerovendas >= 0 And My.Settings.numerovendas <= 9 Then
        'Converter o valor da TextBox para Double, caso seja possível
        'Se não for possível, o valor é "0"
        Double.TryParse(boxvenda.Text, dblBoxVenda)

        'Pegar no setting correspondente
        My.Settings.Item(String.Format("venda{0}", My.Settings.numerovendas + 1)) = dblBoxVenda
        My.Settings.vendas += 1
        My.Settings.numerovendas += 1
        My.Settings.totalvendas += dblBoxVenda
        My.Settings.Save()

        Close()
    End If
End Sub

Remember to mention the My.Settings.Save(), otherwise the changes will not be saved in the click button.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.