Resolved Need help about Conversion error

crystaluz

Well-known member
Joined
Feb 17, 2009
Messages
55
Programming Experience
Beginner
Hello there.. I have a problem with "Conversion from string "" to type integer is not valid".

I put this code into form load. All datatype in my database is Varchar(100).


VB.NET:
Private Sub PopulatePekerjaList()

        Dim cnSQL As SqlConnection
        Dim cmSQL As SqlCommand
        Dim drSQL As SqlDataReader
        Dim strSQL As String
        Dim objListItem As ListPekerja

        Try
            
            strSQL = "SELECT employeeName, emel FROM employee"

            cnSQL = New SqlConnection(ConnectionString)
            cnSQL.Open()

            cmSQL = New SqlCommand(strSQL, cnSQL)
            drSQL = cmSQL.ExecuteReader()

            lstPekerja.Items.Clear()
             
            Do While drSQL.Read()
                objListItem = New ListPekerja(drSQL.Item("employeename").ToString(), _
                                           CStr(drSQL.Item("emel")))
                lstPekerja.Items.Add(objListItem)
            Loop

            If lstPekerja.Items.Count > 0 Then
                lstPekerja.SetSelected(0, True)
            End If

            ' Close and Clean up objects
            drSQL.Close()
            cnSQL.Close()
            cmSQL.Dispose()
            cnSQL.Dispose()

        Catch e As SqlException
            MsgBox(e.Message, MsgBoxStyle.Critical, "SQL Error")

        Catch e As Exception
            MsgBox(e.Message, MsgBoxStyle.Critical, "General Error")
        End Try

End Sub

Why the error keeps pop up? Thank you... :(
 
Last edited:
Presumably the error message makes sense: you cannot convert an empty string to a number. What line does the error occur on? Where on that line are you converting a string to a number? Why are you converting an empty string to a number? Is it a logic error or is it just that you need to test for an empty string first and act only if it's not?
 
Thank you for replying..Erm.. what do you mean by empty string? It occurs in this line

VB.NET:
objListItem = New ListPekerja(drSQL.Item("employeename").ToString(), _
                                   CStr(drSQL.Item("emel")))
:D
 
What do think an empty string is? What is a string? It's a series of characters, right? If an empty egg carton is an egg carton with no eggs in it, what do you think an empty string is?

Now, as you can see in your code, you're calling ListPekerja and passing two strings:
VB.NET:
objListItem = New ListPekerja(drSQL.Item("employeename").[B][U]ToString[/U][/B](), _
                                   [B][U]CStr[/U][/B](drSQL.Item("emel")))
I'm not sure why you chose to use different ways to get the two strings but that's by the by. I'm guessing that at the employee name is not a problem because you wouldn't try to convert that to a number. what about 'emel'? What does it represent? What is it's value? Is it maybe an empty string that you're trying to use as a number?
 
Yes.. that's my mistake. Just noticed it..Hehe.. Thank you jmchilhinney. You helped me a lot in this forum.. Sometimes I cannot trace where the mistake is.. SO sad..
 
Back
Top