RE: control object is in a read only state

Tech-Archive recommends: Fix windows errors by optimizing your registry



Walter,

Not that article. I first saw the async code in Fritz Onion's Essential
ASP.Net 2.0 book. The article in the March 2007 edition of MSDN magazine
prompted me to try it.


The line that errors is...
ReportViewer1.LocalReport.DataSources.Add(New
ReportDataSource("DataTable1", dsReport.Tables(0)))
I've tried it in both the EndGetReportData and the Page_PreRenderComplete
handlers but it didn't help.

I wondered if there is an issue because the callbacks are running on a
different thread than the initial page loading thread.


The web page has Async="True"
The code behind has...

'class level privates...
Private _reportCon As SqlConnection
Private _reportCmd As SqlCommand

Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.PreRender

'rdlStream As System.IO.MemoryStream = <removed for brevity>

ReportViewer1.Reset()
ReportViewer1.LocalReport.ReportPath = ""
ReportViewer1.LocalReport.LoadReportDefinition(rdlStream)
ReportViewer1.LocalReport.DataSources.Clear()

Dim task1 As PageAsyncTask = New PageAsyncTask( _
New BeginEventHandler(AddressOf BeginGetReportData), _
New EndEventHandler(AddressOf EndGetReportData), _
New EndEventHandler(AddressOf GetReportDataTimeout), _
Nothing, False)

Page.RegisterAsyncTask(task1)

End Sub

Function BeginGetReportData(ByVal src As Object, ByVal e As EventArgs, ByVal
cb As AsyncCallback, ByVal state As Object) As IAsyncResult

Dim sPrjConStr As String
sPrjConStr =
ConfigurationManager.ConnectionStrings("PrjConStr").ConnectionString +
";async=true"

Dim conReport As SqlConnection
conReport = New SqlConnection(sPrjConStr)
conReport.Open()

_reportCon = conReport
_reportCmd = getCmd_sp_Prj_centres(conReport)

Return _reportCmd.BeginExecuteReader(cb, state)

End Function

Sub EndGetReportData(ByVal ar As IAsyncResult)

Try
Dim dsReport As DataSet = New DataSet()
Dim drReport As SqlDataReader = _reportCmd.EndExecuteReader(ar)

'new data directly from reader to dataset
dsReport.Tables.Add("DataTable1")
dsReport.Tables(0).Load(drReport)

'close reader and connection
drReport.Close()
_reportCon.Close()

'--------------------------------------------------
' data sources THIS IS THE LINE THAT ERRORS
ReportViewer1.LocalReport.DataSources.Add(New
ReportDataSource("DataTable1", dsReport.Tables(0)))

Finally
If _reportCon.State = ConnectionState.Open Then
_reportCon.Close()
End If
End Try

End Sub


Sub GetReportDataTimeout(ByVal ar As IAsyncResult)
'operation timed out, so just clean up by closing connection
If _reportCon.State = ConnectionState.Open Then
_reportCon.Close()
End If

Me.lblReportError.Text = "Query timed out..."
End Sub

Protected Sub Page_PreRenderComplete(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.PreRenderComplete
'All async tasks have completed
End Sub

Thanks,
Andrew

.



Relevant Pages