参数匹配参数'text'无法从'DBNull'转换为'String'

时间:2013-12-07 16:20:19

标签: sql vb.net ms-access

尝试从Access数据库文件导入记录并在列表视图中显示记录(在Visual Basic中)。它有50%的时间可以工作,但似乎崩溃了:

    An unhandled exception of type 'System.Reflection.AmbiguousMatchException' occurred in Microsoft.VisualBasic.dll

    Additional information: Overload resolution failed because no Public 'Add' can be called with these arguments:

'Public Overrides Function Add(text As String) As System.Windows.Forms.ListViewItem':

    Argument matching parameter 'text' cannot convert from 'DBNull' to 'String'.

'Public Overrides Function Add(value As System.Windows.Forms.ListViewItem) As System.Windows.Forms.ListViewItem':

    Argument matching parameter 'value' cannot convert from 'DBNull' to 'ListViewItem'.

有问题的代码如下:

    ds.Clear()
        LVResults.Items.Clear()
        con.ConnectionString = dbProvider & dbSource
        con.Open()                                          'Open connection to the database
        sqlstatement = "SELECT * FROM records WHERE checkoutdate is NULL"
        da = New OleDb.OleDbDataAdapter(sqlstatement, con)
        da.Fill(ds, "allmembers")                           'Fill the data adapter
        con.Close()
        Dim recordCount, x As Short
        recordCount = 0
        x = 0
        recordCount = ds.Tables("allmembers").Rows.Count
        With ds.Tables("allmembers")
            Do Until x = recordCount
                LVResults.Items.Add(.Rows(x).Item(0))
                LVResults.Items(x).SubItems.Add(.Rows(x).Item(1))
                LVResults.Items(x).SubItems.Add(.Rows(x).Item(2))
                LVResults.Items(x).SubItems.Add(.Rows(x).Item(3))
                LVResults.Items(x).SubItems.Add(.Rows(x).Item(4))
                x = x + 1
            Loop
        End With

了解我这是非常明显的事情但是非常感谢帮助:) 最令人讨厌的是,它有时会起作用,但其他人则会抛出错误。

1 个答案:

答案 0 :(得分:1)

说实话,错误信息非常明显 - 某些字段包含空值,VB不会自动转换为空字符串或零。一种解决方案是编辑SQL语句以明确避免返回空值:

SELECT IIf(Surname Is Null, '', Surname), IIf(Forename Is Null, '', Forename),
  IIf(SomeIntField Is Null, 0, SomeIntField) FROM records WHERE CheckoutDate Is Null