从类型dbnull到type string的转换无效

时间:2016-02-15 04:59:45

标签: vb.net ms-access

我总是得到这个消息框,列中有一个空白,有时间我真的不需要包含数据。我该怎么办?

Private Sub dataasset()
    With assetdata
        codelbl.Text = .CurrentRow.Cells(0).Value
        codename.Text = .CurrentRow.Cells(1).Value
        codedepartment.Text = .CurrentRow.Cells(2).Value
        assetdescrip.Text = .CurrentRow.Cells(3).Value
        custodian1.Text = .CurrentRow.Cells(4).Value
        serialnumber1.Text = .CurrentRow.Cells(5).Value
        acquisitioncost.Text = .CurrentRow.Cells(6).Value
        dateacquired1.Text = .CurrentRow.Cells(7).Value
        location1.Text = .CurrentRow.Cells(8).Value
        prnumber1.Text = .CurrentRow.Cells(9).Value
        purchaseorder1.Text = .CurrentRow.Cells(10).Value
        condition.Text = .CurrentRow.Cells(11).Value
        remarks.Text = .CurrentRow.Cells(12).Value
    End With
End Sub

1 个答案:

答案 0 :(得分:0)

除了给定的答案,我想我也想分享我的“方式”。

在.NET中

codelbl.Text = IIF(IsDBNull(.CurrentRow.Cells(0).Value),"",.CurrentRow.Cells(0).Value) '"" or "N/A" depending on how you want a blank data to be displayed
...

或者为了避免在.NET中,您可以在查询中使用它:

SELECT isnull(YourFieldStr,''),isnull(YourFieldVal,0) FROM YOUR_TABLE