我正在为列表视图进行编码以显示其数据库中的记录,并且在运行该错误时,我真的不知道这是什么问题:


  过载解析失败,因为无法使用“公共”添加调用
  这些参数:'公共函数Add(文本为字符串)为
  System.Windows.Forms.ListViewItem.ListViewSubItem':参数
  匹配的参数“文本”不能从“ DBNull”转换为“字符串”。
  '公共功能添加(项目为
  System.Windows.Forms.ListViewItem.ListViewSubItem)为
  System.Windows.Forms.ListViewItem.ListViewSubItem':参数
  匹配参数“ item”不能从“ DBNull”转换为
  “ ListViewSubItem”。


Imports MySql.Data.MySqlClient

Public Class Form3

Public sConnection As New MySqlConnection
Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    If sConnection.State = ConnectionState.Closed Then
        sConnection.ConnectionString = "server=localhost;user id=root;database=db"
        sConnection.Open()
    End If

    LoadPeople()

End Sub

Public Sub LoadPeople()
    Dim sqlQuery As String = "select * from candidate"
    Dim sqlAdapter As New MySqlDataAdapter
    Dim sqlCommand As New MySqlCommand
    Dim TABLE As New DataTable
    Dim i As Integer

    With sqlCommand
        .CommandText = sqlQuery
        .Connection = sConnection
    End With

    With sqlAdapter
        .SelectCommand = sqlCommand
        .Fill(TABLE)
    End With

    For i = 0 To TABLE.Rows.Count - 1
        With lvPeople
            .Items.Add(TABLE.Rows(i)("cid"))
            With .Items(.Items.Count - 1).SubItems
                .Add(TABLE.Rows(i)("cpos"))
                .Add(TABLE.Rows(i)("cfname"))
                .Add(TABLE.Rows(i)("cmname"))
                .Add(TABLE.Rows(i)("clname"))
                .Add(TABLE.Rows(i)("cyr"))
                .Add(TABLE.Rows(i)("cparty"))
            End With
        End With
    Next


End Sub
End Class


它指向这个.Add(TABLE.Rows(i)("cpos"))

最佳答案

将其添加到ListView之前,请检查该值是否不是DbNull。您可以使用这样的功能

Private Function AddFieldValue(row As DataRow, fieldName As String) As String
   If Not DbNull.Value.Equals(row.Item(fieldName)) Then
      Return CStr(row.Item(fieldName))
   Else
      Return Nothing
   End If
End Function


以这种方式使用

.Add(AddFieldValue(TABLE.Rows(i), ("cpos")))

07-27 14:07