请帮我解决错误 [英] Please help me regarding the error
本文介绍了请帮我解决错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好
我收到以下错误消息..请告诉我该怎么解决
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'.
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
推荐答案
如果看不到引发此异常的代码,就不可能告诉您出什么问题了.
但是,从该消息中,您正在尝试将一种异常类型强制转换为OleDbDataReader.显然这是行不通的.
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.
这是实际的问题
this is actual problem
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
查看带下划线的部分进行比较
发生异常时,它返回ex,它是 Exception class
的对象
通常情况下,它会返回 OleDbDataReader mydata
还有你的
FillListView将第二个参数用作 OleDbDataReader
因此,请更改该return语句,
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,
Catch ex As Exception
Return Nothing 'or Return new OleDbDataReader()
End Try
祝您编码愉快!
:)
Happy Coding!
:)
这篇关于请帮我解决错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文