Listview Problems with subitems

TexG

Regular
Joined
Apr 2, 2003
Messages
88
Location
San Antonio Texas
I am adding to a listview with no problem but when I try and add a field of type "text" from access database with:

ListView1.Items(i).SubItems.Add(myReader("description"))

I get the following error!

An unhandled exception of type 'System.Reflection.AmbiguousMatchException' occurred in microsoft.visualbasic.dll

Additional information: No accessible overloaded 'ListViewSubItemCollection.Add' can be called without a narrowing conversion.

what does that mean and how can i fix it?

Thanks
 
Remember that every column in a DataSet/DataReader is an object. That object can be pretty much any type. Luckily, all objects have the ToString() method. If your column might be NULL (NULL in your database, System.DBNull.Value in code), you may want to trap for that and use "string.Empty" instead of column.ToString().

-Nerseus
 
I'll try to answer it before it's asked :)

Visual Basic:
If myReader("description") = System.DBNull.Value Then
    ListView1.Items(i).SubItems.Add(string.Empty) 
Else
    ListView1.Items(i).SubItems.Add(myReader("description").ToString()) 
End If

Of course, if the description is null, you may not want to add it at all. I'm just showing how you could detect null and add an empty string instead. By the way, that code may not compile - I typed it by hand (not in the IDE) but it should be close

-Nerseus
 
Back
Top