all groups > dotnet compact framework > may 2007 >
You're in the

dotnet compact framework

group:

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


Re: Invalid cast exception when adding values from SQL CE SERVER table to a lsitview - Why? ctacke/
5/28/2007 9:46:19 PM
dotnet compact framework:
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


[quoted text, click to view]

Invalid cast exception when adding values from SQL CE SERVER table to a lsitview - Why? Loogie
5/28/2007 10:12:17 PM
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


Re: Invalid cast exception when adding values from SQL CE SERVER table to a lsitview - Why? Graham McKechnie
5/29/2007 12:00:00 AM
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


[quoted text, click to view]

Re: Invalid cast exception when adding values from SQL CE SERVER table to a lsitview - Why? Loogie
5/29/2007 7:24:06 PM
[quoted text, click to view]


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.

Re: Invalid cast exception when adding values from SQL CE SERVER table to a lsitview - Why? Loogie
5/29/2007 7:24:26 PM
[quoted text, click to view]


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.

AddThis Social Bookmark Button