Release Banner

Release Announcement: Bold Reports 10.1.11 has arrived! Explore the latest features here.

Created with Sketch.
Add Dynamic Report Parameters to ASP.NET Core Applications
Add Dynamic Report Parameters to ASP.NET Core Applications

Add Dynamic Report Parameters to ASP.NET Core Applications

Customized reports are essential for effective data analysis, as static reports can be overwhelming or lack essential details. Integrating report parameters in the Report Viewer control enables dynamic filtering, allowing users to refine data, for example, by selecting specific years. In this blog, we’ll walk you through how to add parameters to a sales report in an ASP.NET Core application, making it easy for users to customize their report views based on selected values.

What Are Report Parameters?

Report parameters allow users to filter reports to show only the data they need. They specify what information to view, making reports more relevant. This feature enhances focus and efficiency in data analysis.

When to Use Report Parameters?

  • Customized Reporting: When you need to allow users to tailor reports to their specific needs, such as selecting particular date ranges, regions, or product categories. For example, a sales manager might want to filter sales data by region or date to focus on the performance of a specific market.
  • Performance Optimization: Parameters let you reduce the amount of data to make reports load faster. For example, when a business tool filters data by date or region, it cuts down on unnecessary information, improving report speed.

How to Add Report Parameters to an ASP.NET Core Report Viewer Application

Before following along, ensure that you have an ASP.NET Core application set up with an integrated Report Viewer. For detailed guidance on setting up an ASP.NET Core application with a Report Viewer, please refer to this blog.

In this blog, we’re going to use a sales report example. We’ll show how to filter sales data by year using report parameters. For a full guide on creating sales reports, check out this blog.

Implementing Controller Action for Report Parameters

To implement controller actions for report parameters, follow these steps:

  1. Open the Controllers folder of your project.
  2. Open the HomeController.cs file.
  3. Update the controller to accept report parameters for the sales report. Here’s a basic example:
[HttpPost]
public IActionResult ReportViewer (string year)
{
List<BoldReports.Models.ReportViewer.ReportParameter> parameters = new List<BoldReports.Models.ReportViewer.ReportParameter>();
parameters.Add(new BoldReports.Models.ReportViewer.ReportParameter()
{
   Name = "Year",
   Values = new List<string>(){ year }
});
ViewBag.parameters = parameters;
ViewBag.parameterSettings = new BoldReports.Models.ReportViewer.ParameterSettings();
ViewBag.parameterSettings.HideParameterBlock = true;
return View();
}

This action method takes a year as a parameter, fetches the corresponding sales data, and passes it to the view.

Setting Up the Client Side

On the client side, you need to create an interface that allows users to input their parameters and view the report. Here’s how to set it up:

  1. Navigate to the Views -> Home folder and open the Index.cshtml file.
  2. Insert the following code snippet to create a form for user input:
 @{
    ViewData["Title"] = "Home Page";
  }
 @{
    Html.BeginForm("ReportViewer", "Home", FormMethod.Post, new { target = "_blank" });
    {
        <div class="Common">
            <div class="tablediv">
                <div class="rowdiv">
                    <br />
                    <label id="design" style="margin-left: 15px">
                       <h3> View Sales Report Based on Year</h3>
                       <br />
                    </label>
                </div>
                <div class="rowdiv">
                    <div class="celldiv" style="margin-left:15px">
                        <label>
                            <strong> Year :</strong>
                        </label>
                        <input id="year" type="text" name="year" style="margin-left: 15px" />
                        <br />
                        <br />
                        <input class="buttonStyle" type="submit" name="button" value="View Report" style="width:150px;" />
                    </div>
                </div>
            </div>
        </div>
        Html.EndForm();
    }
}

This form allows users to enter a year and submit it to the SalesReport action in HomeController. When the View Report button is clicked, the report will be generated based on the specified year.

Run the Application

Finally, run your application using the command dotnet run. Enter the year value and click View Report.

Enter Parameter value
Enter Parameter value

The report will automatically open on a new page, displaying data based on your input. The following screenshot displays sales details for 2020.

Sales Report details for 2020
Sales Report details for 2020

The following screenshot illustrates the sales details for 2021.

Sales Report details for 2021
Sales Report details for 2021

Conclusion

Using report parameters to filter reports by date range, category, or geographic region in an ASP.NET Core application is a powerful way to enhance data analysis. By following the steps outlined in this guide, you can create dynamic parameters in the Report Designer that help your business make informed decisions. Remember, the key to effective reporting is understanding the data that matters most to you!

If you have any questions, please post them in the comments section. You can also contact us through our contact page, or if you already have an account, you can log in to submit your question.

Bold Reports® by syncfusion offers a 30-day free trial with no credit card information required. We invite you to start one and experience Bold Reports for yourself. Be sure to let us know what you think!

Tags:

Share this blog

Leave a Reply