Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 915803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:56:57+00:00 2026-05-15T17:56:57+00:00

I am puzzled by why it does not retrieve the data and keep saying

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T17:56:58+00:00Added an answer on May 15, 2026 at 5:56 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 485k
  • Answers 485k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You basically need to do two things: Create a Filter… May 16, 2026 at 7:40 am
  • Editorial Team
    Editorial Team added an answer If I'm understanding your question correctly, you want to cache… May 16, 2026 at 7:40 am
  • Editorial Team
    Editorial Team added an answer Re separating business logic from GUI: If you intend to… May 16, 2026 at 7:40 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.