Creating Reports in ASP.Net with Entity Framework Creating Reports in ASP.Net with Entity Framework asp.net asp.net

Creating Reports in ASP.Net with Entity Framework


Below is a quick sample of how i set the report datasource in one of my .NET winForms applications.

public  void getMyReportData()    {        using (myEntityDataModel v = new myEntityDataModel())        {            var reportQuery = (from r in v.myTable                                   select new                                   {                                       l.ID,                                       l.LeaveApplicationDate,                                       l.EmployeeNumber,                                       l.EmployeeName,                                       l.StartDate,                                       l.EndDate,                                       l.Supervisor,                                       l.Department,                                       l.Col1,                                       l.Col2,                                       .......,                                       .......,                                       l.Address                                   }).ToList();            reportViewer1.LocalReport.DataSources.Clear();            ReportDataSource datasource = new ReportDataSource("nameOfReportDataset", reportQuery);            reportViewer1.LocalReport.DataSources.Add(datasource);            Stream rpt = loadEmbededReportDefinition("Report1.rdlc");            reportViewer1.LocalReport.LoadReportDefinition(rpt);            reportViewer1.RefreshReport();            //Another way of setting the reportViewer report source            string exeFolder = Path.GetDirectoryName(Application.ExecutablePath);            string reportPath = Path.Combine(exeFolder, @"rdlcReports\Report1.rdlc");            reportViewer1.LocalReport.ReportPath = reportPath;            reportParameter p = new ReportParameter("DeptID", deptID.ToString());            reportViewer1.LocalReport.SetParameters(new[] { p });        }    }    public static Stream loadEmbededReportDefinition(string reportName)        {            Assembly _assembly = Assembly.GetExecutingAssembly();            Stream _reportStream = _assembly.GetManifestResourceStream("ProjectNamespace.rdlcReportsFolder." + reportName);            return _reportStream;        }


My approach has always been to use RDLC files with object data sources and run them in 'local' mode. These data sources are ... my entities! This way, I'm using all of the same business logic, string formatting, culture awareness, etc. that I use for my web apps. There are a some quirks, but I've been able to live with them:

  • RDLC files don't like to live in web projects. We create a separate dummy winform project and add the RDLC files there.
  • I don't show reports in a viewer. I let the user download a PDF, Word, or Excel file and choose to save or open in the native viewer. This saves a bunch of headaches, but can put some folks off, depending on requirements. For mobile devices, it's pretty nice.
  • Since you are not using SSRS, you don't get the nice subscription feature. You are going to build that, if required. In many ways, though, I prefer this.

However, the benefits are really nice:

  • I'm using all of the same business logic goodness that I've already written for my views.
  • I have a custom ReportActionResult and DownloadReport controller method that allows me to essentially run any report via a single URL. This can be VERY handy. It sure makes a custom subscription component easier.
  • Report development seems to go pretty quick, now that I only need to adjust entity partial classes to tweak a little something here or there. Also - If I need to shape the data just a bit differently, I have LINQ.


We too use SSRS as "local" reports. We create Views in SQL server, then create that Object in our application along with the other EF Domain Models, and query that object using our DbContext. We use an ASPX page and use the code behind (Page_Load) to get the data passed to the report.

Here is an example of how we query it in the Page_Load Event:

        var person = MyDbContext            .Query<ReportModel>()            .Where(x => x.PersonId == personId)            .Where(x => x.Year == year)            .Select(x =>            {                PersonId = x.PersonId,                Year = x.Year,                Name = x.Name            });        var datasource = new ReportDataSource("DataSet1", person.ToList());        if (!Page.IsPostBack)        {            myReport.Visible = true;            myReport.ProcessingMode = ProcessingMode.Local;            myReport.LocalReport.ReportPath = @"Areas\Person\Reports\PersonReport.rdlc";        }        myReport.LocalReport.DataSources.Clear();        myReport.LocalReport.DataSources.Add(datasource);        myReport.LocalReport.Refresh();