Monday 4 January 2010

ReportViewer in WebPart (SharePoint)

Using ReportViewer control inside SharePoint Webpart is bit tricky and challenging.
Below are the steps to make it happen.

1. web.config (of SharePoint) changes (Extra space has been included after > and < to make it appear in blog)

add below line to < httpHandlers > section

< add verb="*" path="Reserved.ReportViewerWebControl.axd" type = "Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" / >


and below line to < assemblies >

< add assembly="Microsoft.ReportViewer.WebForms, Version=9.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" / >

2. Add Report (.rdlc) and DataSet (.xsd) files to Solution


Sample Code

------------------------------------------------------------
using System;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.Reporting.WebForms;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;

namespace ReportWebpart
{
public class ReportsWP : WebPart
{
Label lblTime;
ReportViewer ReportViewer1;
TextBox tb1;
Button btn;

protected override void CreateChildControls()
{
base.CreateChildControls();
try
{
lblTime = new Label();
lblTime.Text = "Enter Plant No.";

tb1 = new TextBox();

btn = new Button();
btn.Text = "Refresh Report";
btn.Click += new EventHandler(btn_Click);

ReportViewer1 = new ReportViewer();
ReportViewer1.Width = Unit.Percentage(80);
ReportViewer1.EnableViewState = true;
ReportViewer1.LocalReport.ReportPath = @".\Report1.rdlc"; // Copy the rdlc file to root of IIS
ReportViewer1.ProcessingMode = ProcessingMode.Local;
ReportViewer1.ID = "rprtViewer1";

this.Controls.Add(lblTime);
this.Controls.Add(tb1);
this.Controls.Add(btn);
this.Controls.Add(ReportViewer1);
}
catch (Exception ex)
{
string k = ex.Message;
}
}

void btn_Click(object sender, EventArgs e)
{
DataTable newDT = GetTable();
ReportViewer1.LocalReport.DataSources.Clear();
ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet1_Employee", newDT));//Name of Dataset (.xsd file)+ '_' + table name
ReportViewer1.ID = "rprtViewer1";
ReportViewer1.DocumentMapCollapsed = true;
ReportViewer1.LocalReport.Refresh();
}

private DataTable GetTable()
{
//FRAME THE QUERY AS NEEDED. THE FIELDS RETURN SHOULD MATCH WITH REPORT'S DESIGN TIME FIELDS
}
}
}
----------------------------------------------------------

NOTE: If you are getting following error while accessing the WebPart page containing ReportViewer

Microsoft.SharePoint.Portal.Analytics.UI.ReportViewerMessages, Microsoft.SharePoint.Portal, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c does not implement IReportViewerMessages or could not be found


Try either of following to fix it

1In web.config of SharePoint, comment the following line under
1. <appsettings >
< ! -- < add key="ReportViewerMessages" value="Microsoft.SharePoint.Portal.Analytics...... / > -- >

or

2.  Add  following to < appsettings >
< remove key="ReportViewerMessages" >


------------------------------------------------------