Tuesday, May 19, 2009

Sql Server Reporting Service : Rendering Report From C# Code & Save The Report

One of the requirement for our last project was after generating the report the report link would be emailed to client. The client will follow the link and download the report in PDF format. So we decided that after generating the report we will save the report in the file system in PDF format.To achieve the goal we have gone through the following steps.

1.Add web reference to the url http://MyServer/ReportServer/ReportExecution2005.asmx
2.Generate a report snapshot which I described here.
3.Call the following function to get stream in your preferred format.

public byte[] GetRenderStream(string historyID, string format)
{
ReportExecutionService reportExecutionService = new ReportExecutionService();
CredentialCache credentials = new CredentialCache();
NetworkCredential serviceCredential = new
NetworkCredential(Settings.Default.ReportUserName, Settings.Default.ReportPassword);
credentials.Add(new Uri(reportExecutionService.Url), "Basic", serviceCredential);

reportExecutionService.Credentials = serviceCredential;

byte[] resultStream = null;
string reportPath = Settings.Default.ReportPath;
string format = format; // PDF,Excel etc;

string encoding;
string mimeType;
string extension;
ReportExecutionService.Warning[] warnings = null;
string[] streamIDs = null;

ExecutionInfo execInfo = new ExecutionInfo();
ExecutionHeader execHeader = new ExecutionHeader();

try
{
reportExecutionService.ExecutionHeaderValue = execHeader;
execInfo = reportExecutionService.LoadReport(reportPath, historyID);
String SessionId = reportExecutionService.ExecutionHeaderValue.ExecutionID;
resultStream = reportExecutionService.Render(format, null, out extension, out encoding, out mimeType, out warnings, out streamIDs);

return resultStream;
}
catch (Exception ex)
{
throw ex;
}
}


4. Save the returned stream as a file.

string snapshotID = CreateAndGetNewSnapShotID(int year); // this method is described
//here
string format = "PDF";
string fullFileName = @"C:\Reports\Report.pdf";
byte[] responseStream = _reportServiceProxy.GetRenderStream();

try{
FileStream stream = File.OpenWrite(fullFileName);
stream.Write(responseStream, 0, responseStream.Length);
stream.Close();
}
catch(Exception ex)
{
//catch and log exception;
}

Sunday, May 10, 2009

Creating Report Snapshot Using SQL Server 2005 Reporting Service In C#

While working in the project "BI Site For Car Dealers" we need to generate report from our application instead of depending on reporting server scheduler.Here i will describe the steps
that need to be followed to generate report snapshot from c# code.


1. Add an web reference to the url http://MyServer/ReportServer/ReportService2005.asmx.
2. Now call the following function to generate report snapshot.

public string CreateAndGetNewSnapShotID(int year)
{
ReportingService2005 reportingService = new ReportingService2005();

CredentialCache credentials = new CredentialCache();
NetworkCredential serviceCredential = new
NetworkCredential(Settings.Default.ReportUserName, Settings.Default.ReportPassword);
credentials.Add(new Uri(reportingService.Url), "Basic", serviceCredential);

reportingService.Credentials = serviceCredential;

string report = "/myReport";

ReportParameter[] reportParameters;

// I have used only 1 parameter year. It is possible to use multiple parameter as filter.
reportParameters = new ReportParameter[1];
reportParameters[0] = new ReportParameter();
reportParameters[0].Name = "Year";
reportParameters[0].DefaultValues = new string[] { year.ToString() };

try
{
reportingService.SetReportParameters(report, reportParameters);
Warning[] warnings;
return reportingService.CreateReportHistorySnapshot(report,out warnings);
}
catch (Exception ex)
{
throw new Exception(string.Format("Error:Failed To Create New Subscription ErrorMessage:{0}", ex.Message));
}
}


Few things to note here:
1. User name and password is the windows creadential where the reporting server is hosted.
2. To pass the authentication process successfully it is required to configure the host which I described in an earlier post.