Tups001
asked on
How to save SSRS report to pdf from win application
Hey guys,
I'm trying to save reports from reporting services to my local drive from an application rather than the report manager.
The problem I'm getting is actually creating the PDF and writing my data to that file.
I'm a SQL and SSRS developer but I'm new to C# and winapps. I've been asked to host the reports within an app, hense the query.
The below code all works great until the creation of the pdf - which should be the easiest part! :)
It creates a Pdf, writes the data - apparently - but when trying to open the file it fails.
In fact even if I create a file, and encode it, it won't create a pdf.
Any help is greatly appreciated.
I'm trying to save reports from reporting services to my local drive from an application rather than the report manager.
The problem I'm getting is actually creating the PDF and writing my data to that file.
I'm a SQL and SSRS developer but I'm new to C# and winapps. I've been asked to host the reports within an app, hense the query.
The below code all works great until the creation of the pdf - which should be the easiest part! :)
It creates a Pdf, writes the data - apparently - but when trying to open the file it fails.
In fact even if I create a file, and encode it, it won't create a pdf.
Any help is greatly appreciated.
public void SaveTo(DataTable TransactionDT)
{
ReportService2005.ReportingService2005 rs;
ReportExecution2005.ReportExecutionService rsExec;
// Create a new proxy to the web service
rs = new ReportService2005.ReportingService2005();
rsExec = new ReportExecution2005.ReportExecutionService();
// Authenticate to the Web service using Windows credentials
rs.Credentials = System.Net.CredentialCache.DefaultCredentials;
rsExec.Credentials = System.Net.CredentialCache.DefaultCredentials;
rs.Url = "http://localhost/reportserver/reportservice2005.asmx";
rsExec.Url = "http://localhost/reportserver/reportexecution2005.asmx";
// Prepare Render arguments
string historyID = null;
string deviceInfo = null;
string format = "PDF";
Byte[] bytPDF;
string encoding = String.Empty;
string mimeType = String.Empty;
string extension = String.Empty;
ReportExecution2005.Warning[] warnings = null;
string[] streamIDs = null;
// Define variables needed for GetParameters() method
// Get the report name
string _reportName = "/Faxes/Transactions";
string _historyID = null;
bool _forRendering = false;
ReportService2005.ParameterValue[] _values = null;
ReportService2005.DataSourceCredentials[] _credentials = null;
ReportService2005.ReportParameter[] _parameters = null;
foreach (DataRow dr in TransactionDT.Rows)
{
// Load the selected report.
ReportExecution2005.ExecutionInfo ei = rsExec.LoadReport(_reportName, historyID);
// Prepare report parameter.
// Set the parameters for the report needed.
ReportExecution2005.ParameterValue[] parameters = new ReportExecution2005.ParameterValue[1];
// Place to include the parameter.
parameters[0] = new ReportExecution2005.ParameterValue();
parameters[0].Label = "TransactionID";
parameters[0].Name = "TransactionID";
parameters[0].Value = dr["ID"].ToString();
rsExec.SetExecutionParameters(parameters, "en-us");
//rsExec.Render(format.ToString(), deviceInfo,out extension, out encoding,out mimeType, out warnings, out streamIDs);
bytPDF = rsExec.Render(format, deviceInfo,
out extension, out encoding,
out mimeType, out warnings, out streamIDs);
string FileLocation = @"C:\fax_TransactionID_" + dr["ID"].ToString() + ".pdf";
//Write file specified location
StreamWriter WriteFile = new StreamWriter(FileLocation.ToString());
WriteFile.Write(bytPDF);
WriteFile.Close();
//this test returns same error//
FileStream fs = new FileStream(@"C:\test.pdf", FileMode.Create, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.GetEncoding("gb2312"));
sw.WriteLine("Hello World");
sw.Close();
}
bytPDF = null;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.