Skip to main content

Register Predefined Data Sources (ASP.NET Core)

  • 2 minutes to read

This document describes how to create predefined data sources for the End-User Report Designer in ASP.NET Core Applications. These predefined data sources are displayed when the user invokes the Data Source Wizard:

Web Report Designer Data Source Wizard Choose Existing Data Source

  1. Add a new Report Designer Model (the ReportDesignerModelWithDataSources.cs file) to the Models folder with the following code:

    using DevExpress.XtraReports.UI;
    using System.Collections.Generic;
    
    public class ReportDesignerModelWithDataSources {
        public XtraReport Report { get; set; }
        public Dictionary<string, object> DataSources { get; set; }
    }
    
  2. Open the HomeController.cs file and add the default action that creates a new blank report and data sources. The NWindConnectionString in the appsettings.json file specifies a connection to the sample Northwind database:

    {
        "ConnectionStrings": {
            "NWindConnectionString": "XpoProvider=SQLite;Data Source=|DataDirectory|/Data/nwind.db",
            "ReportsDataConnectionString": "Filename=Data/reportsData.db"
        }
    }
    
    using DevExpress.DataAccess.Json;
    using DevExpress.DataAccess.Sql;
    using DevExpress.XtraReports.UI;
    using Microsoft.AspNetCore.Mvc;
    
    public class HomeController : Controller {
        public IActionResult Index() {
            // Create a SQL data source.
            SqlDataSource dataSource = new SqlDataSource("NWindConnectionString");
            SelectQuery query = SelectQueryFluentBuilder.AddTable("Products").SelectAllColumnsFromTable().Build("Products");
            dataSource.Queries.Add(query);
            dataSource.RebuildResultSchema();
    
            // Create a JSON data source.
            JsonDataSource jsonDataSource = new JsonDataSource();
            jsonDataSource.JsonSource = new UriJsonSource(
                new System.Uri("https://raw.githubusercontent.com/DevExpress-Examples/DataSources/master/JSON/customers.json"));
            jsonDataSource.Fill();
    
            var model = new ReportDesignerModelWithDataSources {
                // Open your report here.
                Report = new XtraReport()
            };
            model.DataSources = new System.Collections.Generic.Dictionary<string, object> {
                { "Northwind", dataSource },
                { "JsonDataSource", jsonDataSource }
            };
            return View(model);
        }
    
  3. In the default view file (Index.cshtml), modify your code to obtain data sources from the model and add them to the Report Designer’s DataSources collection:

    @model ReportDesignerModelWithDataSources
    
    @{
        var designerRender = Html.DevExpress().ReportDesigner("reportDesigner")
            .Height("80%")
            .Bind(Model.Report)
            .DataSources(configureDS => { foreach (var ds in Model.DataSources) { configureDS.Add(ds.Key, ds.Value); } });
        @designerRender.RenderHtml()
    }
    
    @section Scripts {
        // ...
    }
    
  4. Install the Newtonsoft.Json NuGet package if you added JSON data sources.

When an end user adds one of the available data sources to a report, the data source object is cloned and its copy is assigned to the XtraReportBase.DataSource property. This is required to serialize the data source settings with the report layout data when the report is saved.

See Also