This is a migrated thread and some comments may be shown as answers.

NeedDataSource does not work with a generated dataset?

1 Answer 312 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Mika
Top achievements
Rank 2
Mika asked on 24 Mar 2009, 02:19 PM
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.ObjectByVal 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 NothingAndAlso (connSettings.ConnectionString <> NothingThen 
        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.ObjectByVal 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 NothingAndAlso (connSettings.ConnectionString <> NothingThen 
        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 

1 Answer, 1 is accepted

Sort by
0
Accepted
Ivan
Telerik team
answered on 25 Mar 2009, 11:54 AM
Hi Mika,

We have spotted a small mistake in the first code snippet, you have provided, that might be the source of your problem. Try replacing the last line of code, which says:

report.DataSource = Me.SubReport1DataSetTableAdapter1 

with this one:

report.DataSource = Me.SubReport1DataSet.SubReport1DataSetTable 

We hope this helps resolving your issue. If that is not the case, please drop us a line.

Regards,

the Telerik team

Check out Telerik Trainer , the state of the art learning tool for Telerik products.
Tags
General Discussions
Asked by
Mika
Top achievements
Rank 2
Answers by
Ivan
Telerik team
Share this question
or