`DBNull`

before trying to add it to the `ListView`

.```
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`

.Comments

test for DBNull??meaning??sorry i'm not that familiar with VB.

Then why are you trying to fix this code? Go back to your friend and show him my comments.

i've showed him your comment and even he doesn't know how.

How did he know to write the code that you put in the question? It's a simple

`if`

statement.He even has the code in the comment towards the end of the snippet posted in the question.

that's the mysteries you and i both want to know. But i think i already get enough information here. Thanks! :)

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.

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.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject,
503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada
+1 416-849-8900 x 100