ListViewItem is Null for some reason

39 Views Asked by At

This is the error I got whenever I tried to populate my ListView with "users" collection using DataTable returned from a repository function that communicates with MySQL database. Note that item is defined as ListViewItem in my code:

error: Value cannot be null. Parameter name: item

The weird thing is, other databases can be populated into the ListView successfully where even null value cells are also checked with the following code:

Dim table As New DataTable

                table = repo.displayHistory()
                Dim dr As DataTableReader
                dr = table.CreateDataReader
                Dim a As String = "null"

                If table.Rows.Count > 0 Then
                    Do While dr.Read
                        Dim item As New ListViewItem(dr(columnNames(0)).ToString) 'this is ID column
                        For j As Integer = 1 To repo.getColumnCount - 1
                            If String.IsNullOrEmpty(dr(columnNames(j)).ToString) Then
                                item.SubItems.Add(a)
                            ElseIf Not String.IsNullOrEmpty(dr(columnNames(j)).ToString) Then
                                item.SubItems.Add(dr(columnNames(j).ToString))
                            End If
                        Next
                        lstOrderHistory.Items.Add(item)
                    Loop
                    lstOrderHistory.View = View.Details
                End If

Therefore, the code is able to populate NULL value cells with a string named "null" However, the table named "users" is the only table that has the abovementioned error. Even in repository file, I also tried to test displayHistory() function where the core code is as shown below:

If IsNothing(tableName) Then
            MessageBox.Show("Please enter a table name.")
            Return Nothing
        Else
            Try
                MySqlConn.Open()
                sqlstr = "SELECT * FROM " & tableName & ";"  'tableName is public property that reads table name from user
                dttable = MysqlClient.DataHandler.GetDataTable(sqlstr, conn, True)
                If Not IsNothing(dttable) Then
                    If dttable.Rows.Count > 0 Then
                        MessageBox.Show("table is filled!")
                        Return dttable
                    ElseIf dttable.Rows.Count = 0 Then
                        MessageBox.Show("table is empty!")
                        Return Nothing
                    End If
                End If
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End If

For this part, all tables will show either "table is filled" or "table is empty" where "users" table never even triggered any of these two messages. I suspect that the sql command itself was hanging but timeout never occurred (maybe i didnt wait long enough)

What should i do to fix this error?

0

There are 0 best solutions below