提问人:I Forgot My Name 提问时间:9/22/2022 最后编辑:I Forgot My Name 更新时间:9/22/2022 访问量:36
ListViewItem 由于某种原因为 Null
ListViewItem is Null for some reason
问:
每当我尝试使用从与MySQL数据库通信的存储库函数返回的DataTable返回DataTable时,我都会遇到错误。请注意,在我的代码中定义如下:item
ListViewItem
奇怪的是,其他数据库可以成功填充到 ListView 中,其中甚至 null 值单元格也使用以下代码进行检查:
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
因此,该代码能够使用名为“null”的字符串填充 NULL 值单元格
但是,名为“users”的表是唯一具有上述错误的表。即使在存储库文件中,我也尝试测试核心代码如下图所示的功能:displayHistory()
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
对于这部分,所有表都将显示“表已填满”或“表为空”,其中“用户”表甚至从未触发过这两条消息中的任何一条。我怀疑sql命令本身挂起,但从未发生过超时(也许我没有等待足够长的时间)
我应该怎么做才能修复此错误?
答: 暂无答案
评论
ListView
DataTable
DataGridView
BindingSource
DataGridView
ListViewItems
ListView