views:

33

answers:

1

I am puzzled by why it does not retrieve the data and keep saying i have a error "InvalidCastException", i am currently doing these in compact framework and is totally new to it, i searched around looking for a way to get data into a listview base on what little i know about java . these are my creation of table and inserting during formload

 Dim createTable3 As SqlCeCommand = connection.CreateCommand
        createTable3.CommandText = "CREATE TABLE product(product_id int IDENTITY(0,1) PRIMARY KEY,product_name nvarchar(50),bought_price float,sales_price float)"
        connection.Open()
        createTable3.ExecuteNonQuery()
 Dim insertCmd2 As SqlCeCommand = connection.CreateCommand
        insertCmd2.CommandText = "INSERT INTO product(product_name,bought_price)Values('Food',1.00)"
        insertCmd2.ExecuteNonQuery()

        Dim insertCmd3 As SqlCeCommand = connection.CreateCommand
        insertCmd3.CommandText = "INSERT INTO product(product_name,bought_price)Values('Orange',1.50)"
        insertCmd3.ExecuteNonQuery()

        Dim insertCmd4 As SqlCeCommand = connection.CreateCommand
        insertCmd4.CommandText = "INSERT INTO product(product_name,bought_price)Values('Apple',3.00)"
        insertCmd4.ExecuteNonQuery()

        Dim insertCmd5 As SqlCeCommand = connection.CreateCommand
        insertCmd5.CommandText = "INSERT INTO product(product_name,bought_price)Values('Fruit',2.00)"
        insertCmd5.ExecuteNonQuery()
        connection.Close()

this is a code for a get info button

 Dim itmListItem As ListViewItem
        Dim shtFieldCntr As Short

        Dim loopCmd As SqlCeCommand = connection.CreateCommand
        loopCmd.CommandText = "SELECT * FROM product"
        connection.Open()
        Dim dr As SqlCeDataReader = loopCmd.ExecuteReader

        Do While dr.Read
            itmListItem = New ListViewItem()

            If dr.IsDBNull(dr(0)) Then
                itmListItem.Text = ""
            Else
                itmListItem.Text = dr(0)
            End If

            For shtFieldCntr = 1 To dr.FieldCount()
                If dr.IsDBNull(shtFieldCntr) Then
                    itmListItem.SubItems.Add("")
                Else
                    itmListItem.SubItems.Add(dr.GetString(shtFieldCntr)) ' error this line
                End If
            Next shtFieldCntr

            lvProduct.Items.Add(itmListItem)
        Loop
        connection.Close()

picture alt text

+2  A: 

If you review the documentation for SqlCeDataReader.GetString, it mentions under "Remarks" that:

No conversions are performed; therefore, the data retrieved must already be a string.

This will be why you're getting an InvalidCastException. None of your fields, except for product_name, are strings. What you need to do is something like:

itmListItem.SubItems.Add(Convert.ToString(dr.GetValue(shtFieldCntr)))

When you know what all the fields in a table are, it would make more sense to call them and then add them to SubItems explicitly i.e:

Dim productId As Int32 = dr.GetInt32(dr.GetOrdinal("product_id"))
Dim productName As String = dr.GetString(dr.GetOrdinal("product_name"))

itmListItems.SubItems.Add(productId)
itmListItems.SubItems.Add(productName)
Rob
thanks, however do you know how to fix the maximum index problem?because the first column which is the id number the last row is empty when it shouldnt be , which i think is due to the fieldcount i have to -1
kyrogue
@kyrogue - I've added to the answer in a way that hopefully answers your comment.... It's always better, from a maintainability perspective - if nothing else, to call out the names of the columns you're accessing, rather than looping round them :)
Rob
hi, i did not use your way, because what was taught to me was to use a loop, it was basically a damn crash course, and the ms library information are not as information as java's its like they are targetted for people who already know it. however may u take a look at the picture i have posted and why is there a missing value for the ID?
kyrogue