Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all

I am getting the below error..please tell me what the solution

Unable to cast object of type 'System.InvalidOperationException' to type 'System.Data.OleDb.OleDbDataReader'.


VB
Private Sub FillList()
    With lvList .Clear() 
        .View = View.Details
        .FullRowSelect = True
        .GridLines = True
        .Columns.Add("Customer ID", 100)
        .Columns.Add("Company Name", 150)
        .Columns.Add("Contact Name", 140)
        .Columns.Add("Contact Title", 130)
        .Columns.Add("Address", 200)
        FillListView(lvList, GetData(sSql)) 'This is line that raises the error 
    End With
End Sub

Public Function GetData(ByVal sSQL As String)
    Dim cnCustomers As OleDbConnection Dim sqlCmd As OleDbCommand = New OleDbCommand(sSQL)
    Dim myData As OleDbDataReader cnCustomers = New OleDbConnection(cnString)
        Try
            cnCustomers.Open()
            sqlCmd.Connection = cnCustomers
            myData = sqlCmd.ExecuteReader
            Return myData
        Catch ex As Exception
            Return ex 
        End Try
End Function
Posted
Updated 3-Nov-12 4:03am
v3
Comments
Aarti Meswania 2-Nov-12 8:51am    
For quick solution of this issue,
paste code of FillListView() and getdata()
and tell us in which line you get that error
[no name] 2-Nov-12 8:53am    
Private Sub FillList()
With lvList
.Clear()

.View = View.Details
.FullRowSelect = True
.GridLines = True
.Columns.Add("Customer ID", 100)
.Columns.Add("Company Name", 150)
.Columns.Add("Contact Name", 140)
.Columns.Add("Contact Title", 130)
.Columns.Add("Address", 200)

FillListView(lvList, GetData(sSql))//error line
End With
End Sub
===========================================================================
Public Function GetData(ByVal sSQL As String)
Dim cnCustomers As OleDbConnection
Dim sqlCmd As OleDbCommand = New OleDbCommand(sSQL)
Dim myData As OleDbDataReader

cnCustomers = New OleDbConnection(cnString)

Try
cnCustomers.Open()

sqlCmd.Connection = cnCustomers

myData = sqlCmd.ExecuteReader

Return myData
Catch ex As Exception
Return ex
End Try
End Function
Akinmade Bond 3-Nov-12 10:05am    
You asked the compiler to return the error, when an error is caught.

Without seeing the code that's throwing this exception it's impossible to tell you what's wrong.

But, from the message, you're trying to cast one of the Exception types to an OleDbDataReader. That's obviously not going to work.
 
Share this answer
 
Comments
[no name] 2-Nov-12 8:20am    
FillListView(lvList, GetData(sSql))

In this line i m getting the error
Dave Kreskowiak 2-Nov-12 9:40am    
In your GetData method, in the Try/Ctach block, you have "Return ex". You're returning the Exception object as if it were the data!

First, you don't have any return type on the Function header, so you can return anything you want without type checking. This is bad practice.

Second, you don't Return an Exception. You either handle it, or rethrow it.

Seriously, I think you need to pick up a beginners book on VB.NET and work through it.
this is actual problem

VB
Public Function GetData(ByVal sSQL As String)
        Dim cnCustomers As OleDbConnection
        Dim sqlCmd As OleDbCommand = New OleDbCommand(sSQL)
        Dim myData As OleDbDataReader

        cnCustomers = New OleDbConnection(cnString)

        Try
            cnCustomers.Open()

            sqlCmd.Connection = cnCustomers

            myData = sqlCmd.ExecuteReader

            Return myData
        Catch ex As Exception
            Return ex 
        End Try
    End Function

see underlined portion compare them
when exception occur it return ex which is object of Exception class
and in normal case it returns OleDbDataReader mydata

and your
FillListView is taking second argument as OleDbDataReader

so change that return statement,

VB
Catch ex As Exception
         Return Nothing  'or Return new OleDbDataReader()
     End Try


Happy Coding!
:)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900