I would like to know how can I generate report from the code and apply values for filters programmatically.
Below is the code for reading report from database. Assume I want to show customer data and apply filter only show customer where id = 3 (Parameter).
I want to acheive below items:
1. Run report with filters
2. Save generated report as (PDF, Excel, etc)
Code: Select all
public StiReport? LoadReportFromDatabase(string reportName)
{
// Query the database for an existing report
using (var connection = new SqlConnection(DatabaseConnection.ConnectionString))
{
connection.Open();
var command = new SqlCommand("SELECT ReportData FROM Reports WHERE ReportName = @ReportName", connection);
command.Parameters.AddWithValue("@ReportName", reportName);
var reportData = command.ExecuteScalar() as string;
if (!string.IsNullOrEmpty(reportData))
{
try
{
var report = new StiReport();
report.LoadPackedReportFromString(reportData);
//LoadSubMenuDataSource(report);
return report;
}
catch (Exception ex)
{
_logger.LogError($"Error loading report: {ex.Message}");
}
}
}
// Report not found, return null
return null;
}