Example T271722
Visible to All Users

Reporting for Web Forms - How to Register Data Sources for Use in the Web Report Designer

This example demonstrates how to create data sources at runtime and add them to the list of the data sources available in the Web Report Designer.

The following data source types are included in this example:

The project uses the Northwind database at the local SQL server.

The JSON data source uses the open source Newtonsoft.Json library to retrieve JSON data at runtime.

Files to Review

Documentation

More Examples

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Example Code

WebApplication1/Default.aspx
ASPx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1.Default" %> <%@ Register assembly="DevExpress.XtraReports.v24.2.Web.WebForms, Version=24.2.3.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" namespace="DevExpress.XtraReports.Web" tagprefix="dx" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <dx:ASPxReportDesigner ID="ASPxReportDesigner1" runat="server" Height="800px"> </dx:ASPxReportDesigner> </div> </form> </body> </html>
WebApplication1/Default.aspx.cs(vb)
C#
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 }

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.