Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 907
  • Last Modified:

Specified Cast Is not Valid VB.NET

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
c9k9h
Asked:
c9k9h
  • 4
  • 3
1 Solution
 
SethiCommented:
Try adding ToString in the end like this:
itmListItem.SubItems.Add(myData.GetString(shtCntr).ToString)
0
 
c9k9hAuthor Commented:
Same error.... Is this another SQL related issue?
0
 
SethiCommented:
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
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
c9k9hAuthor Commented:
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
 
SethiCommented:
I am sorry, I have no idea on this.
0
 
c9k9hAuthor Commented:
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
 
SethiCommented:
Sounds good.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now