Skip to content

Pass DataTable from .NET to Report

Peter Gill edited this page Jul 1, 2026 · 13 revisions

Passing Data to a Report from .NET

The DataSet.SetData method lets you supply data to a report dataset directly from code instead of (or in addition to) a database query. This is useful when you already have data in memory — from a service call, LINQ query, computed result, or any other source.

All four overloads are async and must be called after Report() / Parse() but before Rebuild() / RunRender().

Using the WinForms viewer

RdlEngineConfig.RdlEngineConfigInit();

var rdlView = new Majorsilence.Reporting.RdlViewer.RdlViewer();
await rdlView.SetSourceFile(new Uri(@"C:\reports\report.rdl"));

var rpt = await rdlView.Report();
await rpt.DataSets["DataSetNameInYourReport"].SetData(yourData);

await rdlView.Rebuild();

Replace yourData with any of the four source types below. The dataset name must match the one defined in the report — see Finding the dataset name below.


Source type 1 — DataTable

var table = new DataTable();
table.Columns.Add("CustomerId", typeof(int));
table.Columns.Add("CustomerName", typeof(string));
table.Rows.Add(1, "Acme Corp");
table.Rows.Add(2, "Globex");

await rpt.DataSets["Customers"].SetData(table);

Column names in the DataTable must match the field names used in the report.


Source type 2 — IEnumerable<T> (LINQ, lists, arrays)

Pass any C# collection or LINQ query directly. The engine reflects on the public properties of T to build the field set.

public record CustomerRow(int CustomerId, string CustomerName, decimal Balance);

var customers = new List<CustomerRow>
{
    new(1, "Acme Corp",  12500.00m),
    new(2, "Globex",      8300.50m),
};

await rpt.DataSets["Customers"].SetData(customers);

Or from a LINQ query:

var rows = dbContext.Customers
    .Where(c => c.IsActive)
    .Select(c => new CustomerRow(c.Id, c.Name, c.Balance))
    .ToList();

await rpt.DataSets["Customers"].SetData(rows);

Important: Public property names of T must match the field names defined in the report dataset. If the report uses {CustomerName}, the class needs public string CustomerName { get; set; }.


Source type 3 — IDataReader

Pass an open IDataReader when you want to stream rows directly from a database command without first loading everything into memory.

using var conn = new Microsoft.Data.SqlClient.SqlConnection(connectionString);
await conn.OpenAsync();

using var cmd = conn.CreateCommand();
cmd.CommandText = "SELECT CustomerId, CustomerName, Balance FROM Customers WHERE IsActive = 1";

using var reader = await cmd.ExecuteReaderAsync();
await rpt.DataSets["Customers"].SetData(reader);
// reader is consumed by SetData — do not read from it afterwards

Source type 4 — XmlDocument

Pass an XmlDocument when your data is already in XML form.

var xml = new XmlDocument();
xml.Load(@"C:\data\customers.xml");

await rpt.DataSets["Customers"].SetData(xml);

The XML structure must match what the report's XML data provider expects. This overload is most useful with reports that already use the XML data provider.


No-GUI (server-side) usage

The same overloads work when rendering without a viewer:

using Majorsilence.Reporting.Rdl;

RdlEngineConfig.RdlEngineConfigInit();

var rdlp = new RDLParser(await File.ReadAllTextAsync("report.rdl"))
{
    Folder = @"C:\reports"
};
using var report = await rdlp.Parse();

// Supply data — use any of the four overloads
await report.DataSets["Customers"].SetData(customers);

// Parameters still go to RunGetData; pass null if none
await report.RunGetData();

using var ms = new MemoryStreamGen();
await report.RunRender(ms, OutputPresentationType.PDF);
var pdf = ((MemoryStream)ms.GetStream()).ToArray();

Finding the dataset name in the designer

The dataset name in code must exactly match the name defined in the report.

The Data Set Name

In the designer: Data → Data Sets lists all dataset names for the current report.


Version 4 (Legacy — .NET Framework / net48)

using fyiReporting.RDL;
using fyiReporting.RdlViewer;

var rdlView = new fyiReporting.RdlViewer.RdlViewer();
rdlView.SourceFile = new Uri(@"\path\to\your\report.rdl");
rdlView.Report.DataSets["DataSetNameInYourReport"].SetData(YourDataTable);
rdlView.Rebuild();

Clone this wiki locally