Skip to main content

Register Predefined Data Sources (ASP.NET MVC)

  • 3 minutes to read

This document describes how to register data sources in the Web Report Designer. These predefined data sources are displayed when the user invokes the Data Source Wizard:

Web Report Designer Data Source Wizard Choose Existing Data Source

To do this, create the data sources and add them to the ReportDesignerSettings.DataSources collection.

@using DevExpress.DataAccess.ConnectionParameters
@using DevExpress.DataAccess.Sql
@using DevExpress.DataAccess.Json

@Html.DevExpress().ReportDesigner(settings => {
    settings.Name = "ReportDesigner";

    // Create a SQL data source with specified connection parameters.
    MsSqlConnectionParameters connectionParameters =
            new MsSqlConnectionParameters("localhost", "Northwind", null, null, MsSqlAuthorizationType.Windows);
    DevExpress.DataAccess.Sql.SqlDataSource ds = new DevExpress.DataAccess.Sql.SqlDataSource(connectionParameters);

    // Create a SelectQuery to access all columns of the Products data table.
    SelectQuery query = SelectQueryFluentBuilder.AddTable("Products").SelectAllColumns().Build("Products");
    ds.Queries.Add(query);
    ds.RebuildResultSchema();

    // Create a JSON data source.
    JsonDataSource jsonDataSource = new JsonDataSource();

    // Specify the data source location.
    jsonDataSource.JsonSource = new UriJsonSource(new Uri("https://raw.githubusercontent.com/DevExpress-Examples/DataSources/master/JSON/customers.json"));

    // Retrieve data from the JSON data source to the Report Designer's Field List.
    jsonDataSource.Fill();

    // Create an Excel data source.
    DevExpress.DataAccess.Excel.ExcelDataSource excelDS = new DevExpress.DataAccess.Excel.ExcelDataSource();
    // Specify the data source location.
    excelDS.FileName = Server.MapPath("~/App_Data/Categories.xlsx");
    excelDS.Name = "CustomExcelDataSource";
    // Create a schema for the Excel data source.
    DevExpress.DataAccess.Excel.ExcelWorksheetSettings excelWorksheetSettings1 = new DevExpress.DataAccess.Excel.ExcelWorksheetSettings() { CellRange = null, WorksheetName = "Sheet" };
    DevExpress.DataAccess.Excel.ExcelSourceOptions excelSourceOptions1 = new DevExpress.DataAccess.Excel.ExcelSourceOptions(excelWorksheetSettings1) { SkipEmptyRows = true, SkipHiddenColumns = true, SkipHiddenRows = true, UseFirstRowAsHeader = true };
    excelDS.SourceOptions = excelSourceOptions1;
    DevExpress.DataAccess.Excel.FieldInfo fieldInfo1 = new DevExpress.DataAccess.Excel.FieldInfo() { Name = "CategoryID", Type = typeof(double) };
    DevExpress.DataAccess.Excel.FieldInfo fieldInfo2 = new DevExpress.DataAccess.Excel.FieldInfo() { Name = "CategoryName", Type = typeof(string) };
    DevExpress.DataAccess.Excel.FieldInfo fieldInfo3 = new DevExpress.DataAccess.Excel.FieldInfo() { Name = "Description", Type = typeof(string) };
    excelDS.Schema.AddRange(new DevExpress.DataAccess.Excel.FieldInfo[] {
                        fieldInfo1,
                        fieldInfo2,
                        fieldInfo3
            });
    // Retrieve data from the JSON data source.
    excelDS.Fill();

    // Add the created data sources to the list of default data sources. 
    settings.DataSources.Add("Northwind", ds);
    settings.DataSources.Add("JsonDataSource", jsonDataSource);
    settings.DataSources.Add(excelDS.Name, excelDS);
}).Bind(new XtraReport()).GetHtml()

Note

The JSON Data Source uses the open source Newtonsoft.Json library to supply JSON data at runtime. Install the Newtonsoft.Json package if your application does not reference this library.

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

For more information on Reporting data sources, review the following help topic: Bind Reports to Data.

See Also