Web Reporting

ASP.NET WebForms

The simplest way to add reporting to an ASP.NET application is to add a "Generic Handler (.ashx)" to your project. If you are unfamiliar with .ashx files, they are basic HTTP handlers like .aspx, but they do not have the overhead of building a Page object and a control heirarchy. They are for serving any kind of content over the web, including HTML, binary data like PDF, images, etc. They are accessed via URL just like a regular .aspx, and can be passed querystrings the same way.

WebReport will handle all serving of the report for you. It will determine what type of report to send based on the ?ReportType querystring.

  • PDF: http://myserver.com/Reports/ProductsReport.ashx?ReportType=PDF
  • Excel: http://myserver.com/Reports/ProductsReport.ashx?ReportType=Excel

Configuration

Check out the Configuring DoddleReport documentation to map the ReportType parameter to specific Report Writers.


When you create your ashx you can replace all the code inside as follows, you just need to implement a few basic abstract members.

public class ProductsReport : WebReport
{
    public override string FileName
    {
        get { return "ProductsReport"; }
    }

    public override IReportSource ReportSource
    {
        get
        {
            // LINQ query
            var query = from p in db.Products
                        select p;

            return query.ToReportSource();
        }
    }

    public override void CustomizeReport(Report report)
    {
        // Text Fields
        report.TextFields.Title = "Products Report";
        report.TextFields.Footer = "Copyright 2009 © The Doddle Project";
        report.TextFields.Header = string.Format("Report Generated: {0}", DateTime.Now);

        // Data Fields
        report.DataFields["ProductID"].Hidden = true;
        report.DataFields["CategoryID"].Hidden = true;
        report.DataFields["SupplierID"].Hidden = true;
    }
}

ASP.NET MVC

And for my MVC friends I have provided a ReportResult to use as an Action Result that will serve up the report.

The ReportResult will actually read the extension of the request on the controller and return the correct type of report.

  • PDF - http://myserver.com/reports/ProductsReport.pdf
  • Excel - http://myserver.com/reports/ProductsReport.xls
  • HTML - http://myserver.com/reports/ProductsReport.html
  • Delimited - http://myserver.com/reports/ProductsReport.txt

Configuration

Check out the Configuring DoddleReport documentation to map the URL extension with a specific Report Writer.

IMPORTANT ROUTING STEP

DoddleReport can automatically determine which IReportWriter to use based on the file extension of the route. To enable this, you must do the following:

// Add using DoddleReport.Web;
// put this line at the _top_ of your route configuration
routes.MapReportingRoute();

Next simply create your report like you normally would and return it inside an instance of ReportResult. DoddleReport will handle the rest.

public ReportResult ProductReport()
{
    // Get the data for the report (any IEnumerable will work)
    var query = ProductRepository.GetAll();
    var totalProducts = query.Count;
    var totalOrders = query.Sum(p => p.OrderCount);


    // Create the report and turn our query into a ReportSource
    var report = new Report(query.ToReportSource());


    // Customize the Text Fields
    report.TextFields.Title = "Products Report";
    report.TextFields.SubTitle = "This is a sample report showing how Doddle Report works";
    report.TextFields.Footer = "Copyright 2011 © The Doddle Project";
    report.TextFields.Header = string.Format(@"
        Report Generated: {0}
        Total Products: {1}
        Total Orders: {2}
        Total Sales: {3:c}", DateTime.Now, totalProducts, totalOrders, totalProducts * totalOrders);


    // Render hints allow you to pass additional hints to the reports as they are being rendered
    report.RenderHints.BooleanCheckboxes = true;


    // Customize the data fields
    report.DataFields["Id"].Hidden = true;
    report.DataFields["Price"].DataFormatString = "{0:c}";
    report.DataFields["LastPurchase"].DataFormatString = "{0:d}";

    // Return the ReportResult
    // the type of report that is rendered will be determined by the extension in the URL (.pdf, .xls, .html, etc)
    return new ReportResult(report);
}

Last edited Aug 1, 2012 at 4:33 AM by mhidinger, version 10

Comments

GrandizerCoen Dec 12, 2011 at 4:29 PM 
You need to watch out here. If you want a PDF and read above it says to do the following:

http://myserver.com/Reports/ProductsReport.ashx?ReportType=PDF

You will then get the following error:

Unable to locate a ReportWriter Configuration with the format 'PDF'. Has this format been registered in web.config?

At least in the version I downloaded, 1.2, the NuGet packages updated the Web.config so I would need to replace PDF with iTextSharpPdf or AbcPdf. Doing the following fixed my issue:

http://myserver.com/Reports/ProductsReport.ashx?ReportType=iTextSharpPdf