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

Setting SqlDataSource parameter value programmatically at runtime

2 Answers 837 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Nathan
Top achievements
Rank 1
Nathan asked on 16 Feb 2015, 07:00 PM
I have set up a SQLDataSource parameter for a report and can set its value at design time. I need to set the parameter value from a UI element at runtime. This is the code that I have (setting the TypeReportSource dynamically at runtime works fine, and I can query the report's parameter's collection without issue. However, once I update the parameter value, the report still uses the design-time value.

// Specifying the assembly qualified name of the Report class for the TypeName of the report source<br>
ViewerReportSource = new TypeReportSource {TypeName = selectedReport.ReportType.AssemblyQualifiedName};<
br><br>
// create instance of report
<
br>var report = (Report)Activator.CreateInstance(selectedReport.ReportType);
<
br><br>
// Set filters (datasource params)<
br>
var ds = report.DataSource as SqlDataSource;<
br>
ds.Parameters["@UserID"].Value = 999;


the value '999' is not passed into the query (I am using SQL Profiler to see the value passed in by the reporting framework). I see the value I set at design time.

2 Answers, 1 is accepted

Sort by
0
Nasko
Telerik team
answered on 17 Feb 2015, 01:56 PM
Hello Nathan,

You are using a TypeReportSource with the report Type Name to load the report in the report viewer:
ViewerReportSource = new TypeReportSource {TypeName = selectedReport.ReportType.AssemblyQualifiedName};

There is no report object at this time and even if you create one and modify it, the changes won't affect the currently assigned to the report viewer TypeReportSource. If you'd like to use the modified report object you will need to do so via an InstanceReportSource and then set ViewerReportSource to that InstanceReportSource object.

For a better understanding of the different types of report sources, please refer to the Report Sources help article.

In case switching to an InstanceReportSource report source type is not an option in your application you will need to provide the value for the SqlDataSource parameter via a Report Parameter:

Parameter:
UserID 

Value:
= Parameters.UserIdReportParameter

Then in your application pass a value to the UserIdReportParameter via the TypeReportSource.Parameters collection:
ViewerReportSource.Parameters.Add("UserIdReportParameter", 999);


Regards,
Nasko
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
kaczmar2
Top achievements
Rank 1
answered on 17 Feb 2015, 05:48 PM
Thank you. Using an instance report source worked perfectly:

// create instance of report
var report = (Report)Activator.CreateInstance(selectedReport.ReportType);
 
// Assigning the Report object to the InstanceReportSource
ViewerReportSource = new InstanceReportSource { ReportDocument = report };
 
// Set filters
var ds = report.DataSource as SqlDataSource;
ds.Parameters["@UserID"].Value = 999;

I did not clearly read the differences between report sources earlier. The help article clarified things.



Tags
General Discussions
Asked by
Nathan
Top achievements
Rank 1
Answers by
Nasko
Telerik team
kaczmar2
Top achievements
Rank 1
Share this question
or