开发者

Conversion error in vb.net?

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

Conversion error in vb.net?


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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜