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

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
 
G

Guest

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
 
G

Graham McKechnie

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
 
L

Loogie

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
 
L

Loogie

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top