user2956633
user2956633

Reputation: 1

The error is Conversion from string "" to type 'Double' is not valid

Private Sub btntambah_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btntambah.Click
    Dim n As Integer
    For n = 0 To lv1.Items.Count - 1
        If lv1.Items(n).Text = txtkdbrng.Text Then
            MsgBox("Data Buku Sudah Ada Dalam List")
            Exit Sub
        End If
    Next
    lv1.Items.Add(txtkdbrng.Text)
    lv1.Items(n).SubItems.Add(txtnmbrng.Text)
    lv1.Items(n).SubItems.Add(txtharga.Text)
    lv1.Items(n).SubItems.Add(txtjmlhhrg.Text)
    lv1.Items(n).SubItems.Add(txttotal.Text)
    lv1.Items(n).SubItems.Add(txtjmlpsn.Text)
    lv1.Items(n).SubItems.Add(txtspesifikasi.Text)
    txttotal.Text = Format(CDbl(Total()), "###, ###, ###")
    ClearBarang()
    txtkdbrng.Focus()
End Sub

Function Total() As Double
    Dim ttl As Double = 0
    If Not Double.TryParse(txttotal.Text, Total) Then
        Total = 0
    End If
End Function

Private Sub btncetak_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btncetak.Click
    If txtnosph.Text = "" Or lv1.Items.Count = 0 Then
        MsgBox("Data Belum Lengkap", MsgBoxStyle.Information, "INFORMASI")
    Else
        Dim nilaikembali1, nilaikembali2 As Integer
        objsph.PNoSph = txtnosph.Text
        objsph.PTglSph = Format(dtpsph.Value, "yyy-MM-dd")
        objsph.PKdCs = txtkdcstmr.Text
        nilaikembali1 = objsph.Simpan()
        'menyimpan ke tabel Ada menggunakan perulangan
        For x As Integer = 0 To lv1.Items.Count - 1
            objada.PNoSph = txtnosph.Text
            objada.PKdBrg = lv1.Items(x).SubItems(1).Text
            objada.PKdBrg = CDbl(lv1.Items(x).SubItems(2).Text)
            objada.PKdBrg = CDbl(lv1.Items(x).SubItems(3).Text)
            nilaikembali2 = objada.Simpan()
            Dim objbarang As New ClsBarang
            objbarang.PKdBrg = lv1.Items(x).SubItems(0).Text
        Next
        If nilaikembali1 = 1 And nilaikembali2 = 1 Then
            MsgBox("Data Berhasil Disimpan", MsgBoxStyle.Information, "INFORMASI")
        End If
        ClearForm()
        ClearBarang()
        ClearCustomer()
        txtnosph.Text = objsph.AutoNumber
    End If
End Sub

Upvotes: 0

Views: 2963

Answers (2)

Karl Anderson
Karl Anderson

Reputation: 34846

The problem is with this code:

Function Total() As Double
    Dim ttl As Double = 0
    If Not Double.TryParse(txttotal.Text, Total) Then
        Total = 0
    End If
End Function

First off you are using the less well known VB6 style implicit return variable that matches the name of the function, instead use the declared Double in your code named ttl and return the value, like this:

Function Total() As Double
    Dim ttl As Double = 0
    If Double.TryParse(txttotal.Text, ttl) Then
        Return ttl
    Else
        ' Attempted conversion of text to Double type failed
        ' Do something here, raise error, alert user, etc.
        ' Returning zero may or may not be acceptable, if it is return zero
        Return 0
    End If
End Function

Using the Return syntax produces much clearer code, in that it does not assume the reader of your code knows the implicit variable that matches the name of the function concept. Always use Return in a Function.

Note: If a total value of zero is not indicative of a problem, then you will need to alter your function to return Nullable(Of Double) instead of just Double and then you can alter the Else portion of the TryParse() result to this:

Function Total() As Nullable(Of Double)
    Dim ttl As Double = 0
    If Double.TryParse(txttotal.Text, ttl) Then
        Return ttl
    Else
        ' Attempted conversion of text to Double type failed
        ' So return null
        Return Nothing
    End If
End Function

Now the caller of your Total function will need to account for Nothing being returned and react accordingly.

Upvotes: 2

LINQ2Vodka
LINQ2Vodka

Reputation: 3036

This means the variable being explicitly or implicitly casted/converted to double is actually an empty string and cannot be proceed.

Upvotes: 1

Related Questions