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

Dynamic Reporting.ObjectDataSource with DataAccess

3 Answers 208 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Jeffrey
Top achievements
Rank 1
Jeffrey asked on 01 Dec 2015, 05:55 PM

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.

 

3 Answers, 1 is accepted

Sort by
0
Stef
Telerik team
answered on 02 Dec 2015, 04:58 PM
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;
 
reportViewer1.ReportSource=IRS;

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.

Regards,
Stef
Telerik
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
0
Kasun
Top achievements
Rank 1
answered on 07 Nov 2016, 07:36 PM

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? 

 

0
Stef
Telerik team
answered on 09 Nov 2016, 02:10 PM
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.

Regards,
Stef
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
Tags
General Discussions
Asked by
Jeffrey
Top achievements
Rank 1
Answers by
Stef
Telerik team
Kasun
Top achievements
Rank 1
Share this question
or