Hello,
I tried to pass a parameter to a subreport with a NeedDataSource event handler.
The subreport uses a generated DataSet, say SubReportDataSet.xsd, and there is an SQL parameter @Parameter in the SelectCommand.
When I tried the below error example code, I could only get the message rendered to the subreport:
"The expression contains object '####' that is not defined in the current context. "
,where the object '####' refers to a first data expression in the report surface, like =Fields.FirstField
I checked that the NeedDataSource runs and It fills the dataset correctly. I tried to put the event handler to the subreport or the main report, with no difference in the result.
Eventually, I fixed the report by creating a new SqlAdapter (working example below).
I just wonder if this is a bug? I use Q1 2009 (3.0.9.311).
Strangely, a generated adapter works fine as a CrossTab datasource. Adapter connection string and parameters are set up in a NeedDataSource event. I have a CrossTab embedded in an othewise empty subreport and I fill the DataSet just like in the error example. Except that I don't have to dynamically set a datasource anywhere.
Regards,
-mika-
UPDATED: The answer was right, the DataSource in the error example should be set:
report.DataSource = Me.SubReport1DataSet.SubReport1DataSetTable
Error example:
Private Sub SubReport1_NeedDataSource(ByVal sender As System.Object, ByVal e As System.EventArgs) _ |
Handles MyBase.NeedDataSource |
'Take the Telerik.Reporting.Processing.Report instance |
Dim report As Telerik.Reporting.Processing.Report = CType(sender, Telerik.Reporting.Processing.Report) |
' Read connection string from app.config / web.config |
Dim connSettings As ConnectionStringSettings = ConfigurationManager.ConnectionStrings("SiteSqlServer") |
If (connSettings IsNot Nothing) AndAlso (connSettings.ConnectionString <> Nothing) Then |
Me.SubReport1DataSetTableAdapter1.Connection.ConnectionString = connSettings.ConnectionString |
'ReportParameter value to SQL |
Dim param As Integer = report.Parameters("Parameter") |
Me.SubReport1DataSetTableAdapter1.Fill(Me.SubReport1DataSet.SubReport1DataSetTable, param) |
' Set report data source |
report.DataSource = Me.SubReport1DataSetTableAdapter1 |
End If |
End Sub |
Working example:
Private Sub SubReport1_NeedDataSource(ByVal sender As System.Object, ByVal e As System.EventArgs) _ |
Handles MyBase.NeedDataSource |
'Take the Telerik.Reporting.Processing.Report instance |
Dim report As Telerik.Reporting.Processing.Report = CType(sender, Telerik.Reporting.Processing.Report) |
' Read connection string from app.config / web.config |
Dim connSettings As ConnectionStringSettings = ConfigurationManager.ConnectionStrings("SiteSqlServer") |
If (connSettings IsNot Nothing) AndAlso (connSettings.ConnectionString <> Nothing) Then |
Dim conn As SqlConnection = New SqlConnection(connSettings.ConnectionString) |
Dim commandText As String = "SELECT ... WHERE id = @Parameter" |
Dim cmd As New SqlCommand(commandText, conn) |
'ReportParameter value to SQL |
Dim param As Integer = report.Parameters("Parameter") |
cmd.Parameters.Add("@Parameter", SqlDbType.VarChar, 10).Value = param |
Dim adapter As New SqlDataAdapter(cmd) |
Dim ds As New DataSet() |
adapter.Fill(ds) |
report.DataSource = adapter |
End If |
End Sub |