1

I have a function (on vb.net) to get a data from a 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

And then the function to call the GetDataSchedule() Function:

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

End Sub

But then when I executed the code, it results on get the error message: Unable to cast object of type 'System.Data.DataView' to type 'System.Data.IDataReader'.

When I just execute the GetDataSchedule() Function, it return value, but when I make it separately to call the function, then it got error. Am I missing something? Need your help. Thanks...

mrjimoy_05
  • 3,452
  • 9
  • 58
  • 95

2 Answers2

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.

Shai Cohen
  • 6,074
  • 4
  • 31
  • 54
  • Hi, thanks.. It doesn't work. I still got the message, and I think the problem is not get back a nullreference, but I'm not sure enough.. – mrjimoy_05 Apr 26 '12 at 02:12
  • Sorry if I wasn't clear. I think the solution to your problem is the Copy() method above. The null reference check was something extra I added. – Shai Cohen Apr 26 '12 at 16:15
0

The Load function accepts only DataReaders as arguments (eg: SqlDataReader), but you already have a datatable. It should be enough to have DT = xTable.

mslliviu
  • 1,098
  • 2
  • 12
  • 30