Invalid cast exception when adding values from SQL CE SERVER tableto a lsitview - Why?

  • Thread starter Thread starter Loogie
  • Start date Start date
L

Loogie

I am trying to add values to a listview but it keeps throwing an error
saying

System.InvalidCastException was unhandled
Message="InvalidCastException"

I am using VB.Net 2005 compact framework

Here is my code and I put a message beside where the error is thrown

Dim cmd As New System.Data.SqlServerCe.SqlCeCommand("Select p_item,
p_code, p_name from product", ssceconn)
Dim myReader As SqlCeDataReader = cmd.ExecuteReader()

If Not clsGlobals.Form4 Is Nothing Then
clsGlobals.Form4.lsvProds.Items.Clear()
Dim lvi As ListViewItem
Do While myReader.Read()
lvi = clsGlobals.Form4.lsvProds.Items.Add(myReader("p_item"))
- ERROR THROWN HERE
'note - I tried lvi =
clsGlobals.Form4.lsvProds.Items.Add(myReader("p_item").ToString) but it
creates an error value of type string can not be converted to
system.windows.forms.listviewitem

lvi.SubItems.Add(myReader("p_code").ToString)
lvi.SubItems.Add(myReader("p_name").ToString)
Loop
End If


Any help appreciated
 
Are you getting back any values that can't be converted to a string (maybe
DBNull for example)?


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Managed Code in an Embedded World
www.OpenNETCF.com
 
Loogie,

Break the code up store myReader("p_item") to some temporary variable of
type object and check out what it type is and what its value is. Its
probably null.

Graham
 
Are you getting back any values that can't be converted to a string (maybe
DBNull for example)?


Here is what I did

Changed the error line to:

lvi = New system.windows.forms.listviewitem(myReader("p_item").ToString)

and just before the "Loop" line added:

clsGlobals.Form4.lsvProds.Items.Add(lvi)

This worked fine.

:L
 
Graham said:
Loogie,

Break the code up store myReader("p_item") to some temporary variable of
type object and check out what it type is and what its value is. Its
probably null.

Graham


Here is what I did

Changed the error line to:

lvi = New system.windows.forms.listviewitem(myReader("p_item").ToString)

and just before the "Loop" line added:

clsGlobals.Form4.lsvProds.Items.Add(lvi)

This worked fine.

:L
 
Back
Top