Repository navigation
Pass DataTable from .NET to Report
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().
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.
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.
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; }.
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 afterwardsPass 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.
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();The dataset name in code must exactly match the name defined in the report.

In the designer: Data → Data Sets lists all dataset names for the current report.
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();