How to run report locally & remotely using Microsoft Report Viewer

/// <summary>
/// Run report remotely either by passing Network Credential or User Id & Password for authentication
/// purpose
/// </summary>
private void RunRemoteSourceReport(NetworkCredential nc,  string userId, string password, string domain, string caption, string reportServerUrl, string reportPath, List<ReportParameter> rptParams)
{
  if (caption.Length > 0)
  {
    if(OnSetReportTitle != null)
      OnSetReportTitle(caption);
  }
  rptView.ProcessingMode = ProcessingMode.Remote;
  rptView.ShowCredentialPrompts = false;
  rptView.ShowParameterPrompts = false;
  ServerReport svrRpt = rptView.ServerReport;
  svrRpt.ReportServerUrl = new Uri(reportServerUrl);
  svrRpt.ReportPath = reportPath;
  if (nc != null)
  {
    svrRpt.ReportServerCredentials.NetworkCredentials = nc;
  }
  else if (userId.Length > 0)
  {
    svrRpt.ReportServerCredentials.NetworkCredentials = new NetworkCredential(userId, password,
    domain);
  }
  svrRpt.SetParameters(rptParams);
  rptView.RefreshReport();
}

/// <summary>
/// Run report locally with single Data Source such as DataTable
/// </summary>
public void RunSingleSourceReport(string caption, string rptPath, string rptSrcName, DataTable rptSrcData, List<ReportParameter> rptParams)
{
  if (caption.Length > 0)
  {
    if (OnSetReportTitle != null)
      OnSetReportTitle(caption);
  }
  rptView.Reset();
  rptView.ProcessingMode = ProcessingMode.Local;
  rptView.LocalReport.ReportPath = rptPath;
  rptView.LocalReport.DataSources.Clear();
  if (rptParams != null && rptParams.Count > 0)
    rptView.LocalReport.SetParameters(rptParams);
  rptView.LocalReport.DataSources.Add(new ReportDataSource(rptSrcName, rptSrcData));
  rptView.Update();
  rptView.RefreshReport();
}

/// <summary>
/// Run report locally with multiple Data Sources
/// </summary>
public void RunMultipleSourceReport(string caption, string rptPath, List<ReportDataSource> rptSources, List<ReportParameter> rptParams)
{
  if (caption.Length > 0)
  {
    if (OnSetReportTitle != null)
      OnSetReportTitle(caption);
  }
  rptView.Reset();
  rptView.ProcessingMode = ProcessingMode.Local;
  rptView.LocalReport.ReportPath = rptPath;
  rptView.LocalReport.DataSources.Clear();
  if (rptParams != null && rptParams.Count > 0)
    rptView.LocalReport.SetParameters(rptParams);
  if (rptSources != null && rptSources.Count > 0)
  {
    foreach (ReportDataSource rptSource in rptSources)
    {
      rptView.LocalReport.DataSources.Add(rptSource);
    }
    rptView.Update();
    rptView.RefreshReport();

  }
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s