2012-04-25 31 views
1

我有一个函数(上vb.net)来从XMLWebService获得数据:无法投射型“System.Data.DataView”的目的为类型“System.Data.IDataReader”

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()函数,它返回值,但是当我分别调用函数时,它会出错。我错过了什么吗?需要你的帮助。谢谢...

回答

0

试试这个

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

虽然,因为你可能会回来,从GetDataSchedule()一个空引用,倒不如重新因子代码中的位:

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

否则,如果您尝试对空引用执行.Copy(),代码将会变为繁荣

+0

嗨,谢谢..它不起作用。我仍然收到了这条消息,我认为问题不在于回复null参考,但我不太确定。 – 2012-04-26 02:12:13

+0

对不起,如果我不清楚。我认为你的问题的解决方案是上面的Copy()方法。空引用检查是我额外添加的东西。 – 2012-04-26 16:15:32

0

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

相关问题