Monday, June 2, 2008

How to show a report of reporting services from .net applications

After deploying your reports to report server, you need to access these reports and show them in your custom .net applications. This post will help you to do this task. Please see the clip that shows you how to deploy your report and how to access them.

Let's say you have a web application that needs to show reports. Firstly, you need add the ReportViewer control from toolbox (which is in data tab) to your page. Then you need to write some code to load report inside the ReportViewer probably you write this code either in page load or in a button click. Take a look to this code:

ReportViewer1.ServerReport.ReportServerCredentials = new ReportCredentials("emady", "mypassword", "hhi");

ReportViewer1.ServerReport.ReportServerUrl = new Uri("http://falcon/Reportserver");

ReportViewer1.ServerReport.ReportPath = "/Report Project State/Report1";

ReportViewer1.ProcessingMode = ProcessingMode.Remote;

ReportViewer1.ServerReport.Refresh();


The first line is trying to pass the credentials to server. Second line is the Reportserver address. Third line specifies the Report address which has Report folder address and the report itself. Forth line sets the processing mode to Remote because you are accessing the report from other application. Finally you need to call Refresh to show the report.

The very tricky part about above code is that I used a Custom class Called ReportCredentials to apply my credentials to the Report server. The reality is that you need a valid credential to access report server so you need to pass this credential by this class which is inherited from IReportServerCredentials. Take a look at the source of this class:

public class ReportCredentials : Microsoft.Reporting.WebForms.IReportServerCredentials

{

string _userName, _password, _domain;

public ReportCredentials(string userName, string password, string domain)

{

_userName = userName;

_password = password;

_domain = domain;

}

public System.Security.Principal.WindowsIdentity ImpersonationUser

{

get

{

return null;

}

}

public System.Net.ICredentials NetworkCredentials

{

get

{

return new System.Net.NetworkCredential(_userName, _password, _domain);

}

}

public bool GetFormsCredentials(out System.Net.Cookie authCoki, out string userName, out string password, out string authority)

{

userName = _userName;

password = _password;

authority = _domain;

authCoki = new System.Net.Cookie(".ASPXAUTH", ".ASPXAUTH", "/", "hhi");

return true;

}

}


2 comments:

Anonymous said...

Hello Emad,

This is a great post - very helpful. Thanks so much.

Gavin

Anonymous said...

Wow great site, love all the tutorials. I was trying to figure out how to load remote reports from our Reporting Server and found this site. Also viewing all your tutorials on "delegates." Informative and thorough, best of all free... Great work!

-Vash