Setting SqlDataSource parameter value programmatically at runtime

3 posts, 0 answers
  1. Nathan
    Nathan avatar
    2 posts
    Member since:
    Oct 2012

    Posted 16 Feb 2015 Link to this post

    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. Nasko
    Admin
    Nasko avatar
    1051 posts

    Posted 17 Feb 2015 Link to this post

    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.

     
  3. kaczmar2
    kaczmar2 avatar
    8 posts
    Member since:
    Nov 2010

    Posted 17 Feb 2015 in reply to Nasko Link to this post

    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.



Back to Top