I have the following code:
tb_Job_No.Text is a combobox

This is the code

Public Sub RepCD_process()

        ssql_Job = "SELECT * FROM Rep_Dist"
        ssql_Job = ssql_Job + " WHERE (Job_Number = '" & tb_Job_No.Text & "') "

        Dim Main_Form As New Main_Form
        With Main_Form

            .DC_Reps.Open()
            .DS_RepDist1.Clear()
            .DA_RepDist.SelectCommand.CommandText = ssql_Job
            .DA_RepDist.Fill(.DS_RepDist1)

            If .DS_RepDist1.Rep_Dist.Rows.Count <> 0 Then
                Dim str(9) As String
                Dim listitem As ListViewItem

                Me.ListView5.Items.Clear()

                X = 0
                Do
                    str(0) = .DS_RepDist1.Rep_Dist(X).Rep_No.ToString()
                    ssql_Rep = "SELECT * FROM Rep_List"
                    ssql_Rep = ssql_Rep + " WHERE (Rep_Number = '" & .DS_RepDist1.Rep_Dist(X).Rep_No.ToString & "') "
                    .DS_Rep_List1.Clear()
                    .DA_Rep_List.SelectCommand.CommandText = ssql_Rep
                    .DA_Rep_List.Fill(.DS_Rep_List1)
                    str(1) = .DS_Rep_List1.Rep_List(0).Rep_Company_Name.ToString()
                    str(2) = .DS_RepDist1.Rep_Dist(X).item_number.ToString()
                    str(3) = .DS_RepDist1.Rep_Dist(X).Comm_Dist.ToString()
                    str(4) = .DS_RepDist1.Rep_Dist(X).Comm_Amt.ToString()
                    str(5) = .DS_RepDist1.Rep_Dist(X).Comm_Percent.ToString()

                    'If Not (IsDBNull(DataTable.Rows(X).Item("MyValue"))) Then
                    '    myString = DataTable.Rows(X).Item("MyValue")
                    'End If

                    If (IsDBNull(.DS_RepDist1.Rep_Dist(X).Item("Ck_Amt"))) Then
                        str(6) = 0
                    Else
                        str(6) = .DS_RepDist1.Rep_Dist(X).Item("Ck_Amt")
                    End If
                    If Not (IsDBNull(.DS_RepDist1.Rep_Dist(X).Item("CK_Date"))) Then
                        str(7) = ""
                    Else
                        str(7) = .DS_RepDist1.Rep_Dist(X).Item("CK_Date")

		 The error I'm getting is" Conversion from type 'DBNull' to type 'String' is not valid.

                    End If
                    If Not (IsDBNull(.DS_RepDist1.Rep_Dist(X).Item("Ck_No"))) Then
                        str(8) = ""
                    Else
                        str(8) = .DS_RepDist1.Rep_Dist(X).Item("Ck_No")
                    End If
                    
                    'If str(0) = .DS_RepDist1.Rep_Dist(X).Rep_No.ToString Then

                    'str(9) = str(4) - str(6)

                    listitem = New ListViewItem(str)
                    Me.ListView5.Items.Add(listitem)
                    ' End If

                    X += 1
                Loop Until X = .DS_RepDist1.Rep_Dist.Rows.Count

                Me.ListView5.Items.Clear()
          End If

        .DC_Reps.Close()
        End With
        If CBRepName.Text.Count <> 0 Then
            Me.CBRepName.Text = Me.CBRepName.Text(0).ToString()
        End If

    End Sub

Can't seem to get pasted this, when it is remark out all works. Can someone give me a suggestion on how to correct this and get data into the listview?

You could edit the table so it has a default of an empty string instead of null, or use a try/catch method inside a loop, leaving the array item as an empty string for that index, which will work unless you need to distinguish between null and empty string.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.