Reputation: 354
I am having trouble getting a datatable from a WCF service using Async/Await and loading it into a datagridview. This is my first time doing this, I feel like I am missing something basic. This is the code I have so far:
Private p_oDataService As New SQLService.DataServiceClient
Async Function GetReportDataTable() As Task(Of DataTable)
Try
p_oDataService = New SQLService.DataServiceClient
Dim tDatatable As Task(Of DataTable) = p_oDataService.GetValidationReportsAsync()
Dim dt As DataTable = Await tDatatable
Return dt
Catch ex As Exception
Throw ex
End Try
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
Dim tDT As Task(Of DataTable) = GetReportDataTable()
Dim dt As DataTable = GetReportDataTable.Result
DataGridView1.AutoGenerateColumns = True
DataGridView1.DataSource = dt
Catch ex As Exception
Throw ex
End Try
End Sub
Any help is appreciated!
Upvotes: 2
Views: 13379
Reputation: 246998
You are mixing blocking and async calls. When you call GetReportDataTable.Result
it will deadlock.
Event handlers allow you to await async tasks as well so update the event handler...
Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
Dim dt As DataTable = Await GetReportDataTable()
DataGridView1.AutoGenerateColumns = True
DataGridView1.DataSource = dt
Catch ex As Exception
Throw ex
End Try
End Sub
Upvotes: 6