ActiveReports 18 .NET Edition
Developers / Working with Reports / Page/RDLX Report / Bind a Page/RDLX Report to Data / Bind Page/RDLX Report to CSV, JSON and XML Data
In This Topic
    Bind Page/RDLX Report to CSV, JSON and XML Data
    In This Topic

    You can use common data formats such as CSV, JSON, XML to bind your Page/RDLX report to data. You can use a provider with the LocateDataSource event to connect to unbound data sources at runtime. The reporting engine calls the LocateDataSource event when it needs data. To do this, you need to add a handler for this event and set the data for the report in it.

    When using the LocateDataSource event as a data binding method, the general approach is as follows:

    Copy Code
    string path = ""; // Your path to the report
    PageReport report = new PageReport(new FileInfo(path));
    report.Document.LocateDataSource += (sender, args) => { // Data binding  };
    

    The event handler receives an argument of the LocateDataSourceEventArgs type with data, related to this event. The following LocateDataSourceEventArgs properties provide information, specific to this event.

    CSV Data Source

    To connect to the CSV data source, use the following code:

    Copy Code
    private PageReport LoadReport()
    {           
        string path = ""; // Your path to the report
        PageReport report = new PageReport(new FileInfo(path));
        report.Document.LocateDataSource += new LocateDataSourceEventHandler(OnLocateDataSource);
       
        return report;
    }
    private void OnLocateDataSource(object sender, LocateDataSourceEventArgs args)
    {
        string data = ""; // Your csv string
        args.Data = data;
    }
    
    Note: We assume that the report has defined data fields and datasets, which can be done in various ways, e.g., in the Web or Windows Forms Designer.

    To interact with the CSV data source, you can specify a connection string that includes the following parameters:

    You can also use the LocateDataSource event to add new data. Data is added as a new data source in the OnLocateDataSource event handler. To control this, you previously subscribed to this event. The reporting engine raises the LocateDataSource event when it needs data.

    The LocateDataSource event handler sets the data in the required format as you can see in the sample below. 

    Copy Code
    private void OnLocateDataSource(object sender, LocateDataSourceEventArgs args)
    {
        string data = ""; // Your csv string
        args.Data = data;
    }
    

    JSON Data Source

    To demonstrate how to bind to JSON data, let's pass a string in the target format as a data source. To do this, you need to subscribe to the LocateDataSource event.

    Copy Code
    private void OnLocateDataSource(object sender, LocateDataSourceEventArgs args)
    {
        string data = ""; // Your json  string
        args.Data = data;
    }
    private PageReport LoadReport()
    {
        string path = ""; // Your path to the report
        PageReport report = new PageReport(new FileInfo(path));
        report.Document.LocateDataSource += OnLocateDataSource;
       
        return report;
    }
    
    Note: We assume that the report has defined data fields and datasets, which can be done in various ways, e.g., in the Web or Windows Forms Designer.

    Data in the JSON format is assigned when the application calls the LocateDataSource event. Data is added as a new data source in the OnLocateDataSource event handler. To control this, you previously subscribed to this event. The reporting engine raises the LocateDataSource event when it needs data. To do this, we create the OnLocateDataSource event handler.

    XML Data Source

    The example below shows how to bind a report to the XML data source by subscribing to the LocateDataSource event and assigning a handler to it.

    Copy Code
    private void OnLocateDataSource(object sender, LocateDataSourceEventArgs args)
    {
        XmlTextReader xmlReader = new XmlTextReader(""); // Your path to the xml file
        args.Data = xmlReader;
    }
    private PageReport LoadReport()
    {
        string path = ""; // Your path to the report file
        PageReport report = new PageReport(new FileInfo(path));
        report.Document.LocateDataSource += OnLocateDataSource;
       
        DataSource dataSource = report.Report.DataSources[0]; // Your data source
        if(dataSource.ConnectionProperties.DataProvider == "XML")
        {
            dataSource.ConnectionProperties.ConnectString = "xmldoc="; // Your connection string
        }
       
        return report;
    }
    

    In the event handler, an XML file is assigned as a data source by using one of the allowed binding methods. For an XML file, these methods include XmlReader, XmlDocument, IXPathNavigable.

    The reporting mechanism calls the LocateDataSource event, which is used to set the data into the report. To do this, you create the OnLocateDataSource event handler and pass in it the data that will be transmitted to the report.

    Note: We assume that the report has defined data fields and datasets, which can be done in various ways, e.g., in the Web or Windows Forms Designer.

    See Also