Solved

Specified Cast Is not Valid VB.NET

Posted on 2009-04-10
7
897 Views
Last Modified: 2013-11-26
Hello,

I'm working on a project and I'm almost there but keep running into errors....

I'm getting the error "Specified Cast Is Not Valid" for the following line:

itmListItem.SubItems.Add(myData.GetString(shtCntr))

This code is to populate a listview.
Any help is greatly appreciated!

thanks!

Imports System.Data.OleDB
 
Public Class ListViewData
 
    Public Sub FillListView(ByRef MyListView As ListView, _
                                                        ByRef myData As OleDbDataReader)
        Dim lvwColumn As ColumnHeader
        Dim itmListItem As ListViewItem
 
        Dim strTest As String
 
        Dim shtCntr As Short
 
        MyListView.Clear()
        For shtCntr = 0 To myData.FieldCount() - 1
            lvwColumn = New ColumnHeader()
            lvwColumn.Text = myData.GetName(shtCntr)
            MyListView.Columns.Add(lvwColumn)
        Next
 
        Do While myData.Read
            itmListItem = New ListViewItem()
            strTest = IIf(myData.IsDBNull(0), "", myData.GetString(0))
            itmListItem.Text = strTest
 
            For shtCntr = 1 To myData.FieldCount() - 1
                If myData.IsDBNull(shtCntr) Then
                    itmListItem.SubItems.Add("")
                Else
                    itmListItem.SubItems.Add(myData.GetString(shtCntr))
                End If
            Next shtCntr
 
            MyListView.Items.Add(itmListItem)
        Loop
    End Sub
End Class
 
 
 
 
 
***Execute Code***
 
Private Sub cmdSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSearch.Click
 
        Dim myCon As OleDbConnection
        Dim sqlCmd As OleDbCommand = New OleDbCommand("SELECT PendingClaimNum, Last, First, DatePending, Custodian FROM Pending WHERE Last Like '" & txtLastName.Text & "'")
        Dim myData As OleDbDataReader
        Dim itmListItem As ListViewItem
        Dim lvhHelper As ListViewData = New ListViewData()
        myCon = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\diw07\My Documents\Daily Backups\phone memo backend.mdb")
 
        Try
            myCon.Open()
            sqlCmd.Connection = myCon
            myData = sqlCmd.ExecuteReader
            lvhHelper.FillListView(MyListView, myData)
            myCon.Close()
        Catch eSql As System.Data.OleDb.OleDbException
            MessageBox.Show(eSql.ToString)
        End Try
 
    End Sub

Open in new window

0
Comment
Question by:c9k9h
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 18

Expert Comment

by:Sethi
ID: 24118210
Try adding ToString in the end like this:
itmListItem.SubItems.Add(myData.GetString(shtCntr).ToString)
0
 

Author Comment

by:c9k9h
ID: 24118317
Same error.... Is this another SQL related issue?
0
 
LVL 18

Accepted Solution

by:
Sethi earned 500 total points
ID: 24118404
Nope. Looks like you are not using the dataReader correctly. Please see the following link for a sample code in the end of the page:
http://msdn.microsoft.com/en-us/library/haa3afyz(VS.71).aspx
0
Application Discovery Service in AWS

In the era of the cloud, customers migrating away from their existing on-premise infrastructure. This requires lots of planning, strategies, and effort to identify their existing resources and determine how best to migrate.  Datacenter migrations happen in four phases -

 

Author Comment

by:c9k9h
ID: 24130670
I'm not sure that is it either.. I changed the query a bit to hit another (simpler table) and it worked... well kind of.

I tried this query:

Dim sqlCmd As OleDbCommand = New OleDbCommand("SELECT EmpID, LastName, FirstName FROM Employees")

And it returned results.

The results were only the values in the column "EmpID" and they were put in columns from left to right in the list... Not what I was trying to accomplish... Any ideas?
0
 
LVL 18

Expert Comment

by:Sethi
ID: 24130700
I am sorry, I have no idea on this.
0
 

Author Comment

by:c9k9h
ID: 24131151
Okay No Problem!

It turns out it was a problem with Date/TIme columns, I'm going to use a datagrid instead i think.


Thanks!
0
 
LVL 18

Expert Comment

by:Sethi
ID: 24131567
Sounds good.
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question