从类型“DBNull”到“String”类型的转换无效。 [英] Conversion from type 'DBNull' to type 'String' is not valid.

查看:141
本文介绍了从类型“DBNull”到“String”类型的转换无效。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人,我需要一些帮助。我的朋友让我查看他的代码,因为它有一些错误。基本上代码的这一部分是当我点击按钮时,它会自动从Access表中获取数据并将其插入到列表视图中。以前它正在工作,但是当我玩弄表格中的数据时,它突然想出了这个错误。从类型DBNull到String类型的转换无效。我在VB.net仍然很新鲜,所以我不知道是什么问题。这是代码段,



 私有  Sub  btnShow_Click( ByVal  sender  As  System。 Object  ByVal  e  As  System.EventArgs)句柄 btnShow.Click 
Dim li As ListViewItem
如果 btnShow.Text = 全部显示 然后
LstCustomer.Items.Clear()
Com.CommandText = 从tblCustomer中选择*
Dr = Com.ExecuteReader
执行 while Dr.Read = True
li = LstCustomer.Items.Add(Dr( 0 ), 0
li。 SubItems
.Add( 1 )。文本= Dr( 1
。添加( 2 )。文本= Dr( 2 ' 错误在这里
.Add( 3 )。Text = Dr( 3
.Add( 3 )。Text = Dr( 3
.Add( 4 )。文本= Dr( 4
.Add( 5 )。文字=博士( 5
.Add( 6 )。文本= Dr( 6
.Add( 7 )。文字=博士( 7
.Add( 8 )。文本= Dr( 8
.Add( 9 )。文本= Dr( 9
.Add( 10 )。文本= Dr( 10
' MsgBox(IsDBNull(Dr(9)))
结束 使用
循环
Dr.Close()
btnShow.Text = 不显示
Else
LstCustomer.Items.Clear()
btnShow.Text = 全部显示
结束 如果
结束 Sub





任何帮助都会非常感激〜:)

解决方案

这只是意味着该项没有值,所以你需要测试 DBNull 然后尝试将其添加到 ListView


作为理查德和Kschuler的答案,简单的方法就是这样。

使用li.SubItems 
.Add( 1 )。文本= Dr( 1
.Add( 2 ).Text = If(IsDBNull(Dr( 2 ), string .Empty,Dr( 2 ))
.Add( 3 )。文本= Dr( 3
.Add( 3 ).Text = Dr( 3
.Add( 4 )。 = Dr( 4
.Add( 5 )。Text = Dr( 5
.Add( 6 )。文本= Dr( 6
.Add( 7 )。文字=博士( 7
.Add( 8 )。文本= Dr( 8
。添加( 9 )。文本= Dr( 9
.Add( 10 )。文本= Dr( 10
' MsgBo x(IsDBNull(Dr(9)))
结束


Richard MacCutchan是正确的。您正在从数据库中提取记录,并且由于某种原因,一个或多个字段中没有数据。在将其添加到ListView之前,您需要检查DBNull.Value。以下是有关DBNull.Value的MSDN文档。它包含一个函数示例,它检查null并且不返回任何内容而不是DBNull.Value。



http://msdn.microsoft.com/en-us/library/system.dbnull.value.aspx [ ^ ]



您无法在列表视图中存储DBNull.Value。您必须手动将其转换为其他内容。


hai there guys, i need some help here. My friend ask me to look into his code because it got some errors. Basically what this part of the code does is when I click on the button, it automatically fetch the data from the Access table and insert it into a listview. Previously it is working, but when i play around with the data in the table, it suddenly came up with this error. Conversion from type 'DBNull' to type 'String' is not valid. I'm still fresh in VB.net so I don't know what's the problem. Here is the code segment,

Private Sub btnShow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShow.Click
        Dim li As ListViewItem
        If btnShow.Text = "Show All" Then
            LstCustomer.Items.Clear()
            Com.CommandText = "Select * from tblCustomer"
            Dr = Com.ExecuteReader
            Do While Dr.Read = True
                li = LstCustomer.Items.Add(Dr(0), 0)
                With li.SubItems
                    .Add(1).Text = Dr(1)
                    .Add(2).Text = Dr(2) 'the error is here
                    .Add(3).Text = Dr(3) 
                    .Add(3).Text = Dr(3)
                    .Add(4).Text = Dr(4)
                    .Add(5).Text = Dr(5)
                    .Add(6).Text = Dr(6)
                    .Add(7).Text = Dr(7)
                    .Add(8).Text = Dr(8)
                    .Add(9).Text = Dr(9)
                    .Add(10).Text = Dr(10)
                    'MsgBox(IsDBNull(Dr(9)))
                End With
            Loop
            Dr.Close()
            btnShow.Text = "Unshow"
        Else
            LstCustomer.Items.Clear()
            btnShow.Text = "Show All"
        End If
    End Sub



any help would be greatly appreciated~ :)

解决方案

It just means that the item has no value, so you need to test for DBNull before trying to add it to the ListView.


As an extension of the answers by Richard and Kschuler, the easy way to do this is something like this.

With li.SubItems
   .Add(1).Text = Dr(1)
   .Add(2).Text = If(IsDBNull(Dr(2), string.Empty, Dr(2))
   .Add(3).Text = Dr(3) 
   .Add(3).Text = Dr(3)
   .Add(4).Text = Dr(4)
   .Add(5).Text = Dr(5)
   .Add(6).Text = Dr(6)
   .Add(7).Text = Dr(7)
   .Add(8).Text = Dr(8)
   .Add(9).Text = Dr(9)
   .Add(10).Text = Dr(10)
   'MsgBox(IsDBNull(Dr(9)))
End With


Richard MacCutchan is correct. You are pulling a record from the database and for some reason there isn't data in one or more of the fields. You need to check for DBNull.Value before adding it to the ListView. Here is the MSDN documentation about DBNull.Value. It includes a sample of a function that checks for null and returns nothing instead of the DBNull.Value.

http://msdn.microsoft.com/en-us/library/system.dbnull.value.aspx[^]

You can't store a DBNull.Value in a listview. You have to convert it to something else manually.


这篇关于从类型“DBNull”到“String”类型的转换无效。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆