Dynamic Reporting.ObjectDataSource with DataAccess

Thread is closed for posting
4 posts, 0 answers
  1. Jeffrey
    Jeffrey avatar
    12 posts
    Member since:
    Mar 2014

    Posted 01 Dec 2015 Link to this post

    I am building a reporting engine.  The goal is to have a database only driven reporting solution.  I only have one piece remaining to make it work right.

     So I am using reflection to create the report itself off of the database report table and all parameters are dynamically handled on the UI.  The trick is getting the data for the report.  All reports will have a stored procedure to compile the data needed.  Currently I have a report ObjectDataSource being loaded like this:

     public static Telerik.Reporting.ObjectDataSource GetReportDataSource(Dal.Report report, Dictionary<string,object> parameterValues)
                Telerik.Reporting.ObjectDataSource dataSource = new Telerik.Reporting.ObjectDataSource();
                dataSource.DataSource = typeof(ReportLogic);
                dataSource.DataMember = string.Format("GetData{0}", report.Name.Replace(".", ""));
                foreach(KeyValuePair<string,object> parameter in parameterValues)
                    dataSource.Parameters.Add(new Telerik.Reporting.ObjectDataSourceParameter(parameter.Key, parameter.Value.GetType(), parameter.Value));
                return dataSource;

    and each report then has to have a method like this:

     public static IEnumerable<StoredProcedureDomainMethodReturnShapeHere> GetData<ReportNameHere>(<StoredProcedureParametersHere>)
                using (DataContext dataContext = new DataContext())
                    return dataContext.StoredProcedureName(StoredProcedureParameters);

     So the goal is to only have ONE GetData method instead of one per report.  I am not sure if I should use a different object than the ObjectDataSource or how I can make the Stored Procedure call to a dynamically named SP with params.

     Trying to avoid using SQLDataAdapter and Datasets... hoping there is a way to do this with DataAccess dynamically.


  2. Stef
    Stef avatar
    3610 posts

    Posted 02 Dec 2015 Link to this post

    Hi Jeffrey,

    The reporting engine tries to load the data schema (available fields) from the data source component's configuration. Thus the approach to inject the type at a later point is not applicable at design-time.

    My recommendation is to design reports with sample data having the same schema as the actual data. Then at run-time you can make an injection to get the required data, and then bind the retrieved data objects to the report.

    For example:
    //approach one is to modify the report instance
    var report = new MyReport();
    report.DataSource = SetReportDataHere();//there is no need to use a data source component if data is already instantiated
    //set the data source of a nested data item
    var table= report.Items.Find("table1",true)[0] as Telerik.Reporting.Table;
    table.DataSource = SetDataItemDataHere();
    //display the modified report
    var IRS = new InstanceReportSource();
    IRS.ReportDocument = report;

    In case you are using the Reporting HTML5 Viewer and REST service, the above code can be used in a custom resolver's Resolve method.

    Let us know if you need any further help.

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. Kasun
    Kasun avatar
    22 posts
    Member since:
    Oct 2016

    Posted 07 Nov 2016 in reply to Stef Link to this post

    Hi Stef, 

    Its me again. Now I'm trying to access Report Datasource in Resolve method. Basically what i need to do is, when I load the report xml definition in my custom resolve method, I need to change the connectionString. But the problem is when I Deserialize the string to a Report object, the datasource is  null. Below is my code.

    var reportData = GetReportTemplateFromUIManagement(int.Parse(reportId));   //This gets the string xml          
                   if (!string.IsNullOrWhiteSpace(reportData))
                       var settings = new XmlReaderSettings()
                           IgnoreWhitespace = true
                       using (XmlReader reader = XmlReader.Create(new StringReader(reportData), settings))
                           var xmlSerializer = new ReportXmlSerializer();
                           var reportObject = (Report)xmlSerializer.Deserialize(reader);                         
                           var paramns = new InstanceReportSource { ReportDocument = reportObject }.ReportDocument.ReportParameters;
                           return new InstanceReportSource { ReportDocument = reportObject };

    but when i do reportObject.Datasource its always null.

    Can we get the datasource details in the resolve method? 


  4. Stef
    Stef avatar
    3610 posts

    Posted 09 Nov 2016 Link to this post

    Hello Kasun,

    If the report visualizes data via nested data items like Table, Graph, you need to access the items and get their DataSource properties. Please consider the example here.

    More complex example is available in Changing the connection string dynamically according to runtime data.

    Telerik by Progress
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top