0
  Public Shared Function GetVolsForMU(ByVal MU As String) As DataTableReader

     Dim dataSet As New DataSet

     Dim responseData As String

     Try
        Dim request As HttpWebRequest
        Dim response As HttpWebResponse
        Dim reader As StreamReader
        request = HttpWebRequest.Create("https://myapi/" & MU)
        request.Method = "GET"
        request.ContentType = "application/json"
        request.Accept = "application/json"
        request.Headers.Add("Authorization", "Bearer " & ConfigurationManager.AppSettings("BearerToken"))
        request.ReadWriteTimeout = 11000
        response = request.GetResponse

         reader = New StreamReader(response.GetResponseStream)
         responseData = reader.ReadToEnd
     Catch ex As Exception
        My.Utility.LogManager.LogError("VolunteerDAL", "GetVolsForMU", ex)
        My.Utility.LogManager.LogInfo("VolunteerDAL", "GetVolsForMU MU was: " & MU)
        Throw New My.Exceptions.APIException("Error Calling API.")
     End Try

     If responseData Is Nothing Then
        Return Nothing
     End If

      Dim volunteers = JsonConvert.DeserializeObject(Of DataTable)(responseData)

     Dim tableReader As DataTableReader = dataSet.CreateDataReader(volunteers)

     Return tableReader
  End Function

My volunteers variable is returning as = {}

responseData = "[{""userId"":""93e4098c-db55-3245-accb-40fe48cd0330"",""firstName"":""Test"",""lastName"":""Tester"",""middleName"":""Super"",""preferredName"":""Test"",""displayName"":""Test"",""pmNumber"":""89332220"",""eMail"":""test@gmail.com"",""username"":""testAPI"",""startDate"":null,""lastLogin"":""2020-08-12T16:25:38.173"",""inactive"":null,""assignmentId"":""334dadbb-bf34-4b92-1783-08da7a5bd456"",""roleId"":""B6CD23CA-7EBF-40FC-99AB-6F91999ABEF5"",""jobName"":""Volunteer"",""jobCodeGroupName"":""Volunteer"",""mU"":""2430"",""expirationDate"":""2023-08-31T00:00:00"",""active"":true}]"

The DataTableReader is coming up empty based off of volunteers not being deserialized correctly. Thanks for any help!


Here's what I ended up with to get it to work.

    Public Shared Function ConvertToDataTable(Of T)(ByVal list As IList(Of T)) As DataTable
        Dim table As New DataTable()
        Dim fields()  = GetType(T).GetProperties()
        For Each field As PropertyInfo In fields
            table.Columns.Add(field.Name, If(Nullable.GetUnderlyingType(field.PropertyType),  field.PropertyType))
        Next
        For Each item As T In list
            Dim row As DataRow = table.NewRow()
            For Each field As PropertyInfo In fields
                row(field.Name) = If(field.GetValue(item) Is Nothing, DbNull.Value, field.GetValue(item))
            Next
            table.Rows.Add(row)
        Next
        Return table
    End Function

Public Shared Function GetVolsForMU(ByVal MU As String) As DataTableReader

 Dim dataSet As New DataSet

 Dim responseData As String

 Try
    Dim request As HttpWebRequest
    Dim response As HttpWebResponse
    Dim reader As StreamReader
    request = HttpWebRequest.Create("https://myapi/" & MU)
    request.Method = "GET"
    request.ContentType = "application/json"
    request.Accept = "application/json"
    request.Headers.Add("Authorization", "Bearer " & ConfigurationManager.AppSettings("BearerToken"))
    request.ReadWriteTimeout = 11000
    response = request.GetResponse

     reader = New StreamReader(response.GetResponseStream)
     responseData = reader.ReadToEnd
 Catch ex As Exception
    My.Utility.LogManager.LogError("VolunteerDAL", "GetVolsForMU", ex)
    My.Utility.LogManager.LogInfo("VolunteerDAL", "GetVolsForMU MU was: " & MU)
    Throw New My.Exceptions.APIException("Error Calling API.")
 End Try

 If responseData Is Nothing Then
    Return Nothing
 End If

        Dim listOfUserAssignments = JsonConvert.DeserializeObject(Of List(Of UserAssignment))(responseData)

        Dim volunteers = ConvertToDataTable(listOfUserAssignments)

             Dim tableReader As DataTableReader = dataSet.CreateDataReader(volunteers)

        Return tableReader

End Function

The code above is what ended up working for me.

  • You have to check responseData one more time. – Serge Aug 11 '22 at 20:41
  • Hey @Serge where/what would I need to check it one more time? – Jonathan Barnett Aug 11 '22 at 21:19
  • Side note. _responseData Is Nothing_ is a little questionable? I admit to always messing this myself and sure I'll get a lesson here but a string can not be nothing – Hursey Aug 11 '22 at 23:28
  • Does this help? https://stackoverflow.com/a/11982180/504351 – Jon Roberts Aug 12 '22 at 10:43
  • Hi @JonathanBarnett, The conversion is perfect, directly using the string you passed as a return. What is the Newtonsoft.Json version you used? Does this code "volunteers.Rows().Count()" generate object null error? – Anderson Constantino Aug 12 '22 at 10:47
  • @AndersonConstantino interesting question, it does show that it has a row count of 1. My knowledge of VB is pretty limited, but it seems really strange that volunteers is showing up as empty but has a row count? Does it need to be converted to something else? Newtsonsoft.Json version 13.0.1 – Jonathan Barnett Aug 16 '22 at 14:59
  • @JonathanBarnett Then see that the conversion is correct, your object is not empty, as I had imagined. I imagine that the problem occurs here "dataSet.CreateDataReader", because it is not able to map the DataTable's data with its DataSet. Maybe if you post the DataSet structure we will be able to find the problem. – Anderson Constantino Aug 16 '22 at 17:20
  • @AndersonConstantino thank you again for the help! I ended up Deserializing the json into a list of Volunteer Objects and then found a code snippet that turned that list into a DataTable. – Jonathan Barnett Aug 18 '22 at 14:32

0 Answers0