xtrareports-17557-web-reporting-asp-net-webforms-reporting-end-user-report-designer-in-asp-net-web-forms-reporting-bind-to-data-register-data-sources.md
This document describes how to make data sources available in the Web Report Designer. These predefined data sources are displayed when the user invokes the Data Source Wizard:
To do this, create the data sources at runtime and add them to the ASPxReportDesigner.DataSources collection.
using System;
using System.Collections.Generic;
namespace WebApplication1 {
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DevExpress.DataAccess.Sql.SqlDataSource sqlDataSource = GenerateSqlDataSource();
DevExpress.DataAccess.ObjectBinding.ObjectDataSource objDataSource = GenerateObjectDataSource();
DevExpress.DataAccess.EntityFramework.EFDataSource efDataSource = GenerateEFDataSource();
DevExpress.DataAccess.Excel.ExcelDataSource excelDataSource = GenerateExcelDataSource();
DevExpress.DataAccess.Json.JsonDataSource jsonDataSource = GenerateJsonDataSource();
ASPxReportDesigner1.DataSources.Add(sqlDataSource.Name, sqlDataSource);
ASPxReportDesigner1.DataSources.Add(objDataSource.Name, objDataSource);
ASPxReportDesigner1.DataSources.Add(efDataSource.Name, efDataSource);
ASPxReportDesigner1.DataSources.Add(excelDataSource.Name, excelDataSource);
ASPxReportDesigner1.DataSources.Add(jsonDataSource.Name, jsonDataSource);
ASPxReportDesigner1.OpenReport("XtraReportTest");
}
private DevExpress.DataAccess.Json.JsonDataSource GenerateJsonDataSource()
{
DevExpress.DataAccess.Json.JsonDataSource jsonDataSource = new DevExpress.DataAccess.Json.JsonDataSource();
jsonDataSource.Name = "CustomJsonDataSource";
var uri = new System.Uri("~/App_Data/nwind.json", System.UriKind.Relative);
jsonDataSource.JsonSource = new DevExpress.DataAccess.Json.UriJsonSource(uri);
jsonDataSource.Fill();
return jsonDataSource;
}
private DevExpress.DataAccess.Excel.ExcelDataSource GenerateExcelDataSource()
{
DevExpress.DataAccess.Excel.ExcelDataSource excelDS = new DevExpress.DataAccess.Excel.ExcelDataSource();
excelDS.FileName = Server.MapPath("App_Data/Categories.xlsx");
excelDS.Name = "CustomExcelDataSource";
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
});
excelDS.RebuildResultSchema();
return excelDS;
}
private DevExpress.DataAccess.EntityFramework.EFDataSource GenerateEFDataSource()
{
DevExpress.DataAccess.EntityFramework.EFDataSource efds = new DevExpress.DataAccess.EntityFramework.EFDataSource();
efds.Name = "CustomEntityFrameworkDataSource";
efds.ConnectionParameters = new DevExpress.DataAccess.EntityFramework.EFConnectionParameters();
efds.ConnectionParameters.ConnectionStringName = "NorthwindEntitiesConnString";
efds.ConnectionParameters.Source = typeof(Models.NorthwindEntities);
return efds;
}
private DevExpress.DataAccess.Sql.SqlDataSource GenerateSqlDataSource()
{
DevExpress.DataAccess.Sql.SqlDataSource ds =
new DevExpress.DataAccess.Sql.SqlDataSource("localhost_Northwind_Connection");
ds.Name = "CustomSqlDataSource";
// Create an SQL query to access the Products table.
DevExpress.DataAccess.Sql.CustomSqlQuery query = new DevExpress.DataAccess.Sql.CustomSqlQuery();
query.Name = "customQuery1";
query.Sql = "SELECT * FROM Products";
ds.Queries.Add(query);
ds.RebuildResultSchema();
return ds;
}
private DevExpress.DataAccess.ObjectBinding.ObjectDataSource GenerateObjectDataSource()
{
DevExpress.DataAccess.ObjectBinding.ObjectDataSource objds = new DevExpress.DataAccess.ObjectBinding.ObjectDataSource();
objds.BeginInit();
objds.Name = "CustomObjectDataSource";
objds.DataSource = typeof(ItemList);
objds.Constructor = new DevExpress.DataAccess.ObjectBinding.ObjectConstructorInfo();
objds.EndInit();
return objds;
}
}
#region Object Data Source
public class ItemList : List<Item>
{
public ItemList()
{
for (int i = 0; i < 10; i++)
{
Add(new Item() { Name = i.ToString() });
}
}
}
public class Item
{
public string Name { get; set; }
}
#endregion
}
Imports DevExpress.DataAccess.ConnectionParameters
Imports System
Imports System.Collections.Generic
Namespace WebApplication1
Partial Public Class [Default]
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
Dim sqlDataSource As DevExpress.DataAccess.Sql.SqlDataSource = GenerateSqlDataSource()
Dim objDataSource As DevExpress.DataAccess.ObjectBinding.ObjectDataSource = GenerateObjectDataSource()
Dim efDataSource As DevExpress.DataAccess.EntityFramework.EFDataSource = GenerateEFDataSource()
Dim excelDataSource As DevExpress.DataAccess.Excel.ExcelDataSource = GenerateExcelDataSource()
Dim jsonDataSource As DevExpress.DataAccess.Json.JsonDataSource = GenerateJsonDataSource()
ASPxReportDesigner1.DataSources.Add(sqlDataSource.Name, sqlDataSource)
ASPxReportDesigner1.DataSources.Add(objDataSource.Name, objDataSource)
ASPxReportDesigner1.DataSources.Add(efDataSource.Name, efDataSource)
ASPxReportDesigner1.DataSources.Add(excelDataSource.Name, excelDataSource)
ASPxReportDesigner1.DataSources.Add(jsonDataSource.Name, jsonDataSource)
ASPxReportDesigner1.OpenReport("XtraReportTest")
End Sub
Private Function GenerateJsonDataSource() As DevExpress.DataAccess.Json.JsonDataSource
Dim jsonDataSource As New DevExpress.DataAccess.Json.JsonDataSource()
jsonDataSource.Name = "CustomJsonDataSource"
Dim sourceUri As New Uri("~/App_Data/nwind.json", System.UriKind.Relative)
jsonDataSource.JsonSource = New DevExpress.DataAccess.Json.UriJsonSource(sourceUri)
jsonDataSource.Fill()
Return jsonDataSource
End Function
Private Function GenerateExcelDataSource() As DevExpress.DataAccess.Excel.ExcelDataSource
Dim excelDS As New DevExpress.DataAccess.Excel.ExcelDataSource()
excelDS.FileName = Server.MapPath("App_Data/Categories.xlsx")
excelDS.Name = "CustomExcelDataSource"
Dim excelWorksheetSettings1 As New DevExpress.DataAccess.Excel.ExcelWorksheetSettings() With {.CellRange = Nothing, .WorksheetName = "Sheet"}
Dim excelSourceOptions1 As New DevExpress.DataAccess.Excel.ExcelSourceOptions(excelWorksheetSettings1) With {.SkipEmptyRows = True, .SkipHiddenColumns = True, .SkipHiddenRows = True, .UseFirstRowAsHeader = True}
excelDS.SourceOptions = excelSourceOptions1
Dim fieldInfo1 As New DevExpress.DataAccess.Excel.FieldInfo() With {.Name = "CategoryID", .Type = GetType(Double)}
Dim fieldInfo2 As New DevExpress.DataAccess.Excel.FieldInfo() With {.Name = "CategoryName", .Type = GetType(String)}
Dim fieldInfo3 As New DevExpress.DataAccess.Excel.FieldInfo() With {.Name = "Description", .Type = GetType(String)}
excelDS.Schema.AddRange(New DevExpress.DataAccess.Excel.FieldInfo() { fieldInfo1, fieldInfo2, fieldInfo3 })
excelDS.RebuildResultSchema()
Return excelDS
End Function
Private Function GenerateEFDataSource() As DevExpress.DataAccess.EntityFramework.EFDataSource
Dim efds As New DevExpress.DataAccess.EntityFramework.EFDataSource()
efds.Name = "CustomEntityFrameworkDataSource"
efds.ConnectionParameters = New DevExpress.DataAccess.EntityFramework.EFConnectionParameters()
efds.ConnectionParameters.ConnectionStringName = "NorthwindEntitiesConnString"
efds.ConnectionParameters.Source = GetType(Models.NorthwindEntities)
Return efds
End Function
Private Function GenerateSqlDataSource() As DevExpress.DataAccess.Sql.SqlDataSource
Dim ds As New DevExpress.DataAccess.Sql.SqlDataSource("localhost_Northwind_Connection")
ds.Name = "CustomSqlDataSource"
Dim query As New DevExpress.DataAccess.Sql.CustomSqlQuery()
query.Name = "customQuery1"
query.Sql = "SELECT * FROM Products"
ds.Queries.Add(query)
ds.RebuildResultSchema()
Return ds
End Function
Private Function GenerateObjectDataSource() As DevExpress.DataAccess.ObjectBinding.ObjectDataSource
Dim objds As New DevExpress.DataAccess.ObjectBinding.ObjectDataSource()
objds.BeginInit()
objds.Name = "CustomObjectDataSource"
objds.DataSource = GetType(ItemList)
objds.Constructor = New DevExpress.DataAccess.ObjectBinding.ObjectConstructorInfo()
objds.EndInit()
Return objds
End Function
End Class
#Region "Object Data Source"
Public Class ItemList
Inherits List(Of Item)
Public Sub New()
For i As Integer = 0 To 9
Add(New Item() With {.Name = i.ToString()})
Next i
End Sub
End Class
Public Class Item
Public Property Name() As String
End Class
#End Region
End Namespace
Important
The JSON Data Source uses System.Text.Json to supply JSON data at runtime. .NET projects do not require manual installation of the System.Text.Json package, as it is already included with the .NET environment.
Set the DevExpress.DataAccess.Native.Json.JsonLoaderHelper.ProcessingLibrary property to NewtonsoftJson to use the Newtonsoft.Json library instead. Then, install the Newtonsoft.Json NuGet package.
In the End-User Designer, when a user adds a data source to a report, a clone of the data source object is assigned to the report’s DataSource property. This is required to properly serialize the data source settings along with the report layout data when the report is saved.
See Also
Bind a Report to an MDB Database (Runtime Sample)
Bind a Report to a Microsoft SQL Server Database at Runtime
Bind a Report to JSON Data (Runtime Sample)
Register SQL Data Connections in ASP.NET Web Forms Reporting