2012-04-25 60 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應該足夠了。

相關問題