Thursday, April 15, 2010

ReportViewer Control in Visual Studio 2008

ReportViewer Control in Visual Studio 2008

Introduction

ReportViewer is a freely redistributable control that enables embedding reports in applications developed using the .NET Framework. Reports are designed with drag-and-drop simplicity using Report Designer included in Visual Studio 2008 (Standard editon and above.)


The ReportViewer control offers the following benefits:

  • Processes data efficiently. The reporting engine built into ReportViewer can perform operations such as filtering, sorting, grouping and aggregation.
  • Supports a variety of ways in which to present data. You can present data as lists, tables, charts and matrices (also known as crosstabs.)
  • Adds visual appeal. You can specify fonts, colors, border styles, background images etc to make your report visually appealing.
  • Enables interactivity in reports. You can have collapsible sections, document map, bookmarks, interactive sorting etc in your report.
  • Supports conditional formatting. You can embed expressions in the report to change display style dynamically based on data values.
  • Supports printing and print preview.
  • Supports export to Excel, Word and PDF formats. (Word export in Visual Studio 2010 and up.)

The control can process and render reports independently using a built-in engine ('local mode') or it can display reports that are processed and rendered on a Report Server ('remote mode').

There is a WinForms and a WebForms version of the control.

Wednesday, April 14, 2010

Passing Parameters to Server Report

ReportViewer1.ShowParameterPrompts = false;
ReportViewer1.ShowCredentialPrompts = false;

ReportViewer1.ProcessingMode = ProcessingMode.Remote;
ReportViewer1.ServerReport.ReportServerUrl = new Uri("http://venkateswarlu/ReportServer");
IReportServerCredentials irsc = new CustomReportCredentials("username","password","domain");
ReportViewer1.ServerReport.ReportServerCredentials = irsc;

//ReportViewer1.ServerReport.ReportPath = "/AdventureWorks/CHCompanySales";
ReportViewer1.ServerReport.ReportPath = "/AdventureWorks/CHSalesOrderDetail";

ReportParameter[] rptParams = new ReportParameter[1];
rptParams[0] = new ReportParameter("SalesOrderNumber", "SO50750");

ReportViewer1.ServerReport.SetParameters(rptParams);
ReportViewer1.ServerReport.Refresh();

ReportViewerCredentials Class

using System.Security.Principal;
using System.Net;

///
/// Summary description for ReportViewerCredentials
///
public class CustomReportCredentials : Microsoft.Reporting.WebForms.IReportServerCredentials
{
// local variable for network credential.
private string _UserName;
private string _PassWord;
private string _DomainName;
public CustomReportCredentials(string UserName, string PassWord, string DomainName)
{
_UserName = UserName;
_PassWord = PassWord;
_DomainName = DomainName;
}
public WindowsIdentity ImpersonationUser
{
get
{
return null; // not use ImpersonationUser
}
}
public ICredentials NetworkCredentials
{
get
{
// use NetworkCredentials
return new NetworkCredential(_UserName, _PassWord, _DomainName);
}
}
public bool GetFormsCredentials(out Cookie authCookie, out string user, out string password, out string authority)
{
// not use FormsCredentials unless you have implements a custom autentication.
authCookie = null;
user = password = authority = null;
return false;
}
}