This article explains how to use a ReportViewer control in a C# Windows Application that displays a local SSRS report whose data source is a web service method.
I ran in to a need to use SQL Server 2005 Reporting Services' Report Viewer control in a C# Windows Form project. That sounds easy enough. The catch was the report had to run without the support of the SSRS server (local mode) and the report data had to be returned from a web service method that returned its result in an array. I scoured SQL Books Online and the internet for a quick explanation but surprisingly found nothing that quite met my needs.
Using Reporting Services 2005 allows you to use a Microsoft supplied report viewer control in Visual Studio 2005. This control is embeddable in .NET 2.0 Windows and web forms. A great resource for more information on this control can be found at http://www.gotreportviewer.com/.
Reporting Services 2005 introduces the concept of local reports. These are reports that are processed on the client without a connection to the SSRS report server. They make use the same report definition language (RDL) server reports use but are embedded in your project. Local SSRS reports have the file extension rdlc, where the 'c' stands for client.
The steps in this article assume you have Visual Studio 2005 and SQL Server 2005 with Reporting Services 2005 installed on your development environment.
private
this.reportViewer1.LocalReport.ReportPath = "Report1.rdlc";
//add a datasource to the report viewer// note: this must be a data tablereportViewer1.LocalReport.DataSources.Add(GetMyData());//this will refresh the report in the viewer with the above settingsthis.reportViewer1.RefreshReport();}
using
namespace
public DataSet ToDataSet(){DataSet ds = new DataSet();XmlSerializer xmlSerializer = new XmlSerializer(_objectArray.GetType());StringWriter writer = new StringWriter();xmlSerializer.Serialize(writer, _objectArray);StringReader reader = new StringReader(writer.ToString());ds.ReadXml(reader);return ds;}}}
///
//create a new array for the data that is returnedReportSummaryMsg[] dataList;
//assign the array to the results of the web service methoddataList = rs.MyWebServiceMethod();
//instantiate the ObjectArray which will convert the array returned from the web service// to a DataSet
ObjectArray objectArray = new ObjectArray(dataList);//create a new DataSetDataSet ds = new DataSet();//use the ObjectArray to convert the array to a DataSet// (this is needed because the ReportDataSource must be a Table)ds = objectArray.ToDataSet();//return the ReportDataSource// The string value must be the same name as the report's datasource (go to Report -> DataSources to see this name)// The table is from the the DataSet.
return new ReportDataSource("LocalReportSample_mySampleWebService_ReportSummaryMsg", ds.Tables[0]);
}
Compile this and you're good to go! You will see your report appear in the ReportViewer control.
Reporting with Reports Viewer in Visual Studio 2005