Creating local report using MS Reporting Services

Microsoft reporting services provide the way to represent data in the form of reports in easy manner. It’s a powerful tool, we can generate complex reports using it.   We can create reports by creating Server Reports Project in visual studio  and publish them to server. We can also create local report for ASP.NET using the same mechanism. Here i want to show you how can we achieve this.

Before going further check if your system got Reporting Services components installed. Create a Server Reports project (here i am using VS2005) by following the steps in this link. In this example i am assuming that your report consumes data returned from Stored Procedure and your SP takes some parameters. The report files created by this project will have .rdl extension and these files are XML files so you can easily change them using any notepad.

Now copy your final report file(.rdl) to ASP.NET application and change the file extension to .rdlc. Now open the report file using any text editor like notepad then remove the parameters tags if your reports requires any parameters.

How to remove parameter tags:

<ReportParameters></ReportParameters> and <QueryParameters></QueryParameters> section of the XML represents the parameters required by your report. So delete these tags and content inside these tags and save the file. Now your report is ready to use in your ASP.NET application.

Add report viewer control to your webpage. Below code shows, code required to use SSRS reports as local report.

Default.aspx:

<rsweb:ReportViewer ID="rptViewer" runat="server" Width="100%" ShowBackButton="false" ShowExportControls="true"
ShowFindControls="true" ShowPageNavigationControls="true" ShowRefreshButton="false" ShowPrintButton="false"
ShowParameterPrompts="false" ShowZoomControl="false" Visible="false">
<LocalReport>

</LocalReport>
</rsweb:ReportViewer>

Default.aspx.cs:

DataTable dt = GetReportData(param1);
String path = &quot;~/Reports/YourReport.rdlc&quot;;
BindDataToReport(dt, path);
private void BindDataToReport(DataTable reportDataTable,String reportPath)
{

    if (reportDataTable != null &amp;&amp; reportDataTable.Rows.Count &gt; 0)
    {
        rptViewer.Visible = true;
        rptViewer.LocalReport.ReportPath = string.Empty;
        ReportDataSource rptDataSource = new ReportDataSource(&quot;ReportDataSource&quot;, reportDataTable);
        rptViewer.LocalReport.DataSources.Clear();
        rptViewer.Reset();
        rptViewer.LocalReport.ReportPath = Server.MapPath(reportPath);
        rptViewer.LocalReport.DataSources.Add(rptDataSource);
        rptViewer.LocalReport.Refresh();
    }
    else
    {

       //else part here
    }
}

Note:

In the above example i created report using VS2005 and used it as local report in the project created by .NET3.5(VS2008) because the local reports in VS2010 uses SSRS 2008 schema while VS2008 uses SSRS 2005 schema. SSRS 2008 introduces tablix data region concept which is the combination of matrix,table and list. This is not available in SSRS 2005. So take care while using reports as local report.

I hope this post helpful to you. Happy coding Happy

Advertisements