I'm trying to output a report as pdf and everything seems fine except I'm seeing only one page in the final output. The datasrouce is showing a count 2 but the final report shows only 1 page.
This is what I have in code:
if (reportType == ReportType.AllJobs)
{
dataSource = BRInfoGateway.GetAllJobs(); //This shows a count of 2 during debug
reportName = "./Reports/AllJobs.rdlc";
}
else
{
dataSource = BRInfoGateway.GetJob(jobContext);
reportName = "./Reports/SpecificJob.rdlc";
}
var report = new LocalReport();
report.ReportPath = reportName;
report.DataSources.Add(new ReportDataSource("BRInfo", dataSource));
Warning[] warnings;
string[] streamids;
string mimeType;
string encoding;
string extension;
return report.Render("PDF", null, out mimeType, out encoding, out extension,
out streamids, out warnings);
Then in my test I'm just saving the byte[]. Pretty normal stuf...
var reportData = Reports.ReportsGateway.GetReport(Reports.ReportType.AllJobs, null);
string filename = "AllJobs.pdf";
if(File.Exists(filename)) File.Delete(filename);
using (FileStream fs = File.Create(filename))
{
fs.Write(reportData, 0, reportData.Length);
}
Any thoughts. I'm not sure if I have to do something specific in the report template!
I got it to work by using a new report instead of the wizard...no idea why !
Related
Dears
Im using Microsoft reportviewer in my asp.net core project to generate a pdf report .
I want to set an external path from my files into the header section as a logo ,
I'm tying to do that not by report designer , i need to do that at runtime by c# code.
im using this code to generate report
public static byte[] GenerateReport(string reportName, string reportHeader, List<ReportDataSource> DataSets, LanguageEnum language, bool setLanguage = true, string returnExtension = "Pdf", string reportParams = "")
{
ReportViewer EMSReportViewer = new ReportViewer();
EMSReportViewer.LocalReport.EnableExternalImages = true;
SetReport(EMSReportViewer, reportName, language, setLanguage, reportParams);
EMSReportViewer.LocalReport.DisplayName = getLabel(reportHeader, language);
EMSReportViewer.LocalReport.DataSources.Clear();
foreach (ReportDataSource dataset in DataSets)
EMSReportViewer.LocalReport.DataSources.Add(dataset);
Warning[] warnings;
string[] streamids;
string mimeType;
string encoding;
string filenameExtension;
byte[] bytes = EMSReportViewer.LocalReport.Render(returnExtension, null, out mimeType, out encoding, out filenameExtension, out streamids, out warnings);
return bytes;
}
can you help me please?
If you want to pass a external images then you need to pass it as a ReportParameter.
Example:
public static byte[] GenerateReport(string reportName, string reportHeader,
List<ReportDataSource> DataSets, LanguageEnum language, bool setLanguage =
true, string returnExtension = "Pdf", string reportParams = "")
{
ReportViewer EMSReportViewer = new ReportViewer();
EMSReportViewer.LocalReport.EnableExternalImages = true;
//ReportParameter
var parameter = new ReportParameter("ImagePath","-Your Image Path--");
EMSReportViewer.LocalReport.SetParameters(parameter);
SetReport(EMSReportViewer, reportName, language, setLanguage, reportParams);
EMSReportViewer.LocalReport.DisplayName = getLabel(reportHeader, language);
EMSReportViewer.LocalReport.DataSources.Clear();
foreach (ReportDataSource dataset in DataSets)
EMSReportViewer.LocalReport.DataSources.Add(dataset);
Warning[] warnings;
string[] streamids;
string mimeType;
string encoding;
string filenameExtension;
byte[] bytes = EMSReportViewer.LocalReport.Render(returnExtension, null, out mimeType, out encoding, out filenameExtension, out streamids, out warnings);
return bytes;
}
I'm doing a basic MVC apps and i'm trying to export the value of my SQL query to an Excel.
When i open the Excel file the 2 fields that are part of the JOIN are empty and i dont undertand why since when i click on Preview data on my DataSet i see the correct result.
But in my Excel file i dont see the TitleDescription and the TeamDescription.
My SQL Query:
Select Emp.EmployeeID, Emp.FirstName, Emp.LastName, Ti.TitleDescription, Te.TeamDescription,
Emp.Phone FROM Employees Emp
JOIN Titles Ti ON Emp.TitleID=Ti.TitleID
JOIN Teams Te ON Emp.TeamID=Te.TeamID
My Method:
public ActionResult Reports(string ReportType)
{
LocalReport localreport = new LocalReport();
localreport.ReportPath = Server.MapPath("~/Reports/EmployeeReport.rdlc");
ReportDataSource reportDataSource = new ReportDataSource();
reportDataSource.Name = "EmployeeReportDataSet";
reportDataSource.Value = db.Employees.ToList();
localreport.DataSources.Add(reportDataSource);
string mimeType;
string encoding;
string fileNameExtension = "XLSX";
string[] streams;
Warning[] warnings;
byte[] renderedByte;
renderedByte = localreport.Render("EXCELOPENXML", null, out mimeType, out encoding, out fileNameExtension, out streams, out warnings);
Response.AddHeader("content-disposition", "attachment; filename=employee_report." + fileNameExtension);
return File(renderedByte, fileNameExtension);
}
I believe my problem is comming from reportDataSource.Value = db.Employees.ToList(); i should use DataTable1 instead of Employees but i'm not sure how.
I would really appreciate if someone can help me out.
Thanks,
I found what was the problem,
First i used a DataSet TableAdapter instead of the a DataSet Query and i used the method that was generated by default which is the GetData() method. I put it in the DataSource.Value=
Here's my corrected code:
public ActionResult Reports(string ReportType)
{
EmployeeReportDataSet employeeReportDataSet = new EmployeeReportDataSet();
EmployeeReportDataSetTableAdapters.TempReportTableAdapter employeereportTableAdapter =
new EmployeeReportDataSetTableAdapters.EmployeesReportsTableAdapter();
employeereportTableAdapter.Fill(employeeReportDataSet.TempReport);
LocalReport localreport = new LocalReport();
localreport.ReportPath = Server.MapPath("~/Reports/EmployeeReport.rdlc");
ReportDataSource reportDataSource = new ReportDataSource();
reportDataSource.Name = "EmployeeReportDataSet";
reportDataSource.Value = employeereportTableAdapter.GetData();
localreport.DataSources.Add(reportDataSource);
string mimeType;
string encoding;
string fileNameExtension = "XLSX";
string[] streams;
Warning[] warnings;
byte[] renderedByte;
renderedByte = localreport.Render("EXCELOPENXML", null, out mimeType, out encoding, out fileNameExtension, out streams, out warnings);
Response.AddHeader("content-disposition", "attachment; filename=employee_report." + fileNameExtension);
return File(renderedByte, fileNameExtension);
}
i want to export rdl into pdf and reports have query and dataset everything sets on report. i am using this code but it's give me an error
ReportViewer reportViewer = new ReportViewer();
reportViewer.ProcessingMode = ProcessingMode.Local;
reportViewer.SizeToReportContent = true;
reportViewer.Width = Unit.Percentage(900);
reportViewer.Height = Unit.Percentage(900);
string filePath = HttpContext.Current.Server.MapPath(#"~/Reports/Reports.rdl");
reportViewer.LocalReport.ReportPath = HttpContext.Current.Server.MapPath(#"~/Reports/Reports.rdl");
Warning[] warnings;
string[] streamids;
string mimeType;
string encoding;
string filenameExtension;
byte[] bytes = reportViewer.LocalReport.Render("PDF", null, out mimeType, out encoding, out filenameExtension, out streamids, out warnings);
using (FileStream fs = new FileStream(filePath, FileMode.Create))
{
fs.Write(bytes, 0, bytes.Length);
}
please help me how to fix this..?
I am facing problem in last two days. I am trying to view rdlc report as pdf without reportviewer. I export rdlc to pdf using the following code...
public string Export(LocalReport rpt, string filePath)
{
string ack = "";
try
{
Warning[] warnings;
string[] streamids;
string mimeType;
string encoding;
string extension;
byte[] bytes = rpt.Render("PDF", null, out mimeType, out encoding, out extension, out streamids, out warnings);
using (FileStream stream = File.OpenWrite(filePath))
{
stream.Write(bytes, 0, bytes.Length);
}
return ack;
}
catch(Exception ex)
{
ack = ex.InnerException.Message;
return ack;
}
}
The pdf file exported to User->AppData->Temp
string filePath = System.IO.Path.GetTempFileName();
string ack = Export(rpt, Server.MapPath("~/Reports/Mymun_Lab/ComparisonStudy.rdlc"));
System.Diagnostics.Process.Start(filePath);
I want to render this pdf file to the client PC.
This Code is work fine on my local machine. But when i publish this to IIS Server and run for try to get the exported pdf file to client PC not success. How can i solve this issue. Can anyone help me.
Thanks In Advance...
Finally, i found one solution about view RDLC report as pdf in asp.net MVC. The solution is following....
public FileResult ViewReport()
{
string RptPath = Server.MapPath("~/Reports/Mymun_Lab/ComparisonStudy.rdlc");
Microsoft.Reporting.WebForms.LocalReport rpt = new Microsoft.Reporting.WebForms.LocalReport();
/* Bind Here Report Data Set */
rpt.ReportPath = RptPath;
string filePath = System.IO.Path.GetTempFileName();
Export(rpt, filePath);
//CLOSE REPORT OBJECT
rpt.Dispose();
return File(filePath, "application/pdf");
}
I use the following method for generate pdf from RDLC....
public string Export(LocalReport rpt, string filePath)
{
string ack = "";
try
{
Warning[] warnings;
string[] streamids;
string mimeType;
string encoding;
string extension;
byte[] bytes = rpt.Render("PDF", null, out mimeType, out encoding, out extension, out streamids, out warnings);
using (FileStream stream = File.OpenWrite(filePath))
{
stream.Write(bytes, 0, bytes.Length);
}
return ack;
}
catch(Exception ex)
{
ack = ex.InnerException.Message;
return ack;
}
}
[HttpPost]
public async Task<FileResult> DownloadReport(string id, string firstName, string lastName)
{
var result = await accountRepository.GetUserLogInDetailsByID(id);
//string RptPath = Server.MapPath("~/RDLC/rptUserLogInDetails.rdlc");
string RptPath = Server.MapPath("~/RDLC/rptUserLogInDetails.rdlc");
ReportViewer rv = new ReportViewer();
ReportDataSource rds = new ReportDataSource();
rds.Name = "DataSet1";
rds.Value = result;
string companyName = WebConfigurationManager.AppSettings["CompanyName"];
ReportParameter[] parameters = new ReportParameter[2];
parameters[0] = new ReportParameter("username", firstName + " " + lastName);
parameters[1] = new ReportParameter("companyName", companyName);
//ReportViewer rv = new Microsoft.Reporting.WebForms.ReportViewer();
rv.ProcessingMode = ProcessingMode.Local;
rv.LocalReport.ReportPath = RptPath;
// Add the new report datasource to the report.
rv.LocalReport.DataSources.Add(rds);
rv.LocalReport.EnableHyperlinks = true;
rv.LocalReport.SetParameters(parameters);
rv.LocalReport.Refresh();
byte[] streamBytes = null;
string mimeType = "";
string encoding = "";
string filenameExtension = "";
string[] streamids = null;
Warning[] warnings = null;
streamBytes = rv.LocalReport.Render("PDF", null, out mimeType, out encoding, out filenameExtension, out streamids, out warnings);
string fileName = "LogInDetails-" + firstName + lastName + ".pdf";
// This will download the pdf file
//return File(streamBytes, mimeType, fileName);
//This will open directly the pdf file
return File(streamBytes, "application/pdf");
}
I have a method that renders a report as a PDF so that it can be attached in an email. The code is as follows:
private string generate_pdf_report(int ponum)
{
this.DataTable1TableAdapter.Fill(this.reportdataset.DataTable1, ponum);
Warning[] warnings;
string[] streamids;
string mimeType;
string encoding;
string filenameExtension;
string filename = "Purchase Requisition " + ponum.ToString() + ".pdf";
byte[] bytes = reportViewer1.LocalReport.Render(
"PDF", null, out mimeType, out encoding, out filenameExtension,
out streamids, out warnings);
try
{
using (FileStream fs = new FileStream(filename, FileMode.Create))
{
fs.Write(bytes, 0, bytes.Length);
}
}
catch(Exception ex)
{
throw ex;
}
return filename;
}
This works fine the first time, but if the user generates another report and sends an email, the report attached to the email is the first one generated, not the current one.
For example, the user opens the software, creates purchase order #3, and the PDF is generated and emailed correctly. The user then creates a new purchase order without exiting the software, #4. After #4 is submitted, the PDF attached to the email is from #3, like the ReportViewer did not clear out the previous data.
Even if the ReportViewer interface is used to generate a new report, it does not affect what is attached to the email. I have tried clearing the ReportViewer between renders to no avail.
What am I missing here?
I was able to correct the problem by resetting the ReportDataSource each iteration of the function:
this.DataTable1TableAdapter.Fill(this.reportdataset.DataTable1, ponum);
DataTable dt = new DataTable();
dt = DataTable1TableAdapter.GetData(ponum);
Microsoft.Reporting.WinForms.ReportDataSource rprtDTSource = new Microsoft.Reporting.WinForms.ReportDataSource(dt.TableName, dt);
reportViewer1.LocalReport.DataSources.Add(rprtDTSource);
Warning[] warnings;
string[] streamids;
string mimeType;
string encoding;
string filenameExtension;
string filename = "Purchase Requisition " + ponum.ToString() + ".pdf";
byte[] bytes = reportViewer1.LocalReport.Render(
"PDF", null, out mimeType, out encoding, out filenameExtension,
out streamids, out warnings);
try
{
using (FileStream fs = new FileStream(filename, FileMode.Create))
{
fs.Write(bytes, 0, bytes.Length);
fs.Flush();
}
}
catch (Exception ex)
{
throw ex;
}
return filename;