1

我有一个函数(在 vb.net 上)从 XMLWebService 获取数据:

Private Function GetDataSchedule() As DataTable
    Dim xTable As Data.DataTable
    xTable = xMaster.GetSchedule()

    'Bind to DataTable
    Dim DT As New System.Data.DataTable
    DT.Load(xTable.DefaultView) '--> When I set a breakpoint, the error start from here

    Return DT
End Function

然后是调用函数的GetDataSchedule()函数:

Public Sub ShowDataSchedule()
    Dim DSSchedule As New System.Data.DataSet
    DSSchedule.Tables.Add(GetDataSchedule) 

End Sub

但是当我执行代码时,它会导致收到错误消息: Unable to cast object of type 'System.Data.DataView' to type 'System.Data.IDataReader'.

当我只是执行GetDataSchedule()函数时,它返回值,但是当我单独调用函数时,它得到了错误。我错过了什么吗?需要你的帮助。谢谢...

4

2 回答 2

0

Try this

DSSchedule.Tables.Add(GetDataSchedule().Copy())

Although, since you might get back a null reference from GetDataSchedule(), it would be better to re factor your code a bit:

Dim schedule as Data.DataTable = GetDataSechedule()
If Not IsNothing(schedule) Then
    DSSchedule.Tables.Add(schedule.Copy())
End If

Otherwise, your code will go boom if you try to perform a .Copy() on a null reference.

于 2012-04-25T16:11:13.203 回答
0

Load 函数只接受 DataReaders 作为参数(例如:SqlDataReader),但您已经有一个数据表。有 DT = xTable 就足够了。

于 2012-04-26T06:25:37.663 回答