Click here to Skip to main content
15,889,909 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I prepared a report by passing dataset and parameters to a RDLC report. Now I can send it to printer for printing. As like I want to send it to A RDLC report viwer to preview. I don not want to pass dataset to report viwer. Please look into the line I commented in the code bellow. Anyone can help?

1. I want to remove hardcoe from this line-
report.ReportPath = @"F:\Projects\SaleSlip.rdlc"
//
C#
How will it work after deploying my project to other drvie?


2. I want to load a report this way.
reportViewer.LocalReport = report;//I want to load report to reportviwer.
C#
But do not know whether it is possible or not.


What I have tried:

report = new LocalReport();
report.ReportPath = @"F:\Projects\SaleSlip.rdlc"
report.DataSources.Add(new ReportDataSource("SaleSlipData", dtAddItems));
report.DataSources.Add(new ReportDataSource("SaleSlipPaymentData", dtPaymentCollection));


switch (strOption)
{
case "Print":
SendToPrinter(report);
break;

case "Preview":
SaleSlipViewer frmSaleSlipViwer = new SaleSlipViewer();
ReportViewer reportViewer = frmSaleSlipViwer.rpvSaleSlipVeiwer;
reportViewer.Clear();
reportViewer.LocalReport = report;//I want to load report to reportviwer. But how?
reportViewer.Refresh();
break;
C#


case "ExporttoPDF":
exportSaleSlipToPDF();
break;

}
Posted
Updated 12-Mar-16 9:58am
v2

1. I want to remove hardcoe from this line-
report.ReportPath = @"F:\Projects\SaleSlip.rdlc"
How will it work after deploying my project to other drvie?

You could copy your report files to the application path.
You can achieve this when you build your project:

a. Locale the "Copy to Output Directory" property of the report file.
b. Change it to "Copy always".

Then, use a relative url:
C#
report.ReportPath = "reportName.rdlc";


2. I want to load a report this way.
reportViewer.LocalReport = report;//I want to load report to reportviwer

The LocalReport property of the ReportViewer is readonly.
You could do this:
C#
// report = new LocalReport();
SaleSlipViewer frmSaleSlipViwer = new SaleSlipViewer();
ReportViewer reportViewer = frmSaleSlipViwer.rpvSaleSlipVeiwer;
LocalReport report = reportViewer.LocalReport;
report.ReportPath = @"F:\Projects\SaleSlip.rdlc"
report.DataSources.Add(new ReportDataSource("SaleSlipData", dtAddItems));
report.DataSources.Add(new ReportDataSource("SaleSlipPaymentData", dtPaymentCollection));


switch (strOption)
{
case "Print":
SendToPrinter(report);
break;

case "Preview":
// SaleSlipViewer frmSaleSlipViwer = new SaleSlipViewer();
// ReportViewer reportViewer = frmSaleSlipViwer.rpvSaleSlipVeiwer;
reportViewer.Clear();
// reportViewer.LocalReport = report;//I want to load report to reportviwer. But how?
// reportViewer.Refresh();
reportViewer.RefreshReport();
break;
 
Share this answer
 
Comments
Member 12360367 12-Mar-16 2:35am    
Thank you very much Isaac Salcedo. I agreed with first solution, but I have query about the second. How to assign the report to report viewer? Here is clear and refresh property, but which report it should load?
reportViewer.Clear();
reportViewer.RefreshReport();

Thank you for your kind response.
Isaac Salcedo 12-Mar-16 11:54am    
I forgot to comment this instruction: reportViewer.Clear();.
I have a question:
- Do you want to load different report files using the same reportViewer?
Member 12360367 12-Mar-16 13:17pm    
No I want to load only one report. I have prepared a RDLC report as follow-
report = new LocalReport();
report.ReportPath = @"F:\Projects\SaleSlip.rdlc"
report.DataSources.Add(new ReportDataSource("SaleSlipData", dtAddItems));
report.DataSources.Add(new ReportDataSource("SaleSlipPaymentData", dtPaymentCollection));

I can preview this report sending all data to report viewer as follow-

SaleSlipViewer frmSaleSlipViwer = new SaleSlipViewer();
ReportViewer reportViewer = frmSaleSlipViwer.rpvSaleSlipVeiwer;
reportViewer.Clear();

//set processing to local mode
reportViewer.LocalReport.EnableExternalImages = true;
reportViewer.ProcessingMode = Microsoft.Reporting.WinForms.ProcessingMode.Local;
reportViewer.LocalReport.ReportPath ="SaleSlip.rdlc";

reportViewer.LocalReport.DataSources.Add(new ReportDataSource("SaleSlipData", dtAddItems));
reportViewer.LocalReport.DataSources.Add(new ReportDataSource("SaleSlipPaymentData", dtPaymentCollection));

//refresh viewer with above settings
reportViewer.RefreshReport();
frmSaleSlipViwer.ShowDialog();

But I do not want to pass data to report viewer again, I just want to load that report which I already prepared to use various purposes like export to PDF, Excell, Print or Preview according to parameter. I want to preview that report which I prepared for print. Hope you get me.
You could create a method for prepare the report, and another method for generate files like pdf or excel.

C#
// Prepare the report
private void prepareReport(LocalReport report, string reportFilePath, DataTable data)
{
   report.ReportPath = reportFilePath;
   report.DataSources.Add(new ReportDataSource("SaleSlipData", data));
}
// Generate a file
private void GenerateFile(string fileType, LocalReport report, string savePath)
{
   // The FileStream class is in the System.IO namespace.
   /* 
    * The savePath include the file name with the proper file extension.
    * If file is a pdf -> filename.pdf
    */
   byte[] bytes = report.Render(fileType);
   FileStream fs = new FileStream(savePath, FileMode.Create);
   fs.Write(bytes, 0, bytes.Length);
   fs.Close();
}

// Here the code for a button.
private void btnButton1_Click(object sender, EventArgs e)
{
   SaleSlipViewer frmSaleSlipViwer = new SaleSlipViewer();
   ReportViewer reportViewer = frmSaleSlipViwer.rpvSaleSlipVeiwer;
   
   /* 
    * The processing mode of a report viewer is Local by default, 
    * this line is not necessary. 
    */
   reportViewer.ProcessingMode = ProcessingMode.Local;

   prepareReport(reportViewer.LocalReport, "SaleSlip.rdlc", dtAddItems);
   
   switch (strOption)
   {
      case "Preview":
        reportViewer.RefreshReport();
        break;
      case "ExporttoPDF":
        GenerateFile("PDF", reportViewer.LocalReport, "Reports/report.pdf");
        break;
      case "ExporttoExcel":
        GenerateFile("Excel", reportViewer.LocalReport, "Reports/report.xls");
        break;
      // Add more cases as needed.
   }
   
}


The LocalReport Render method support file types are PDF, Excel, Word, and image.
See the documentation for more information (here)
 
Share this answer
 
Comments
Member 12360367 13-Mar-16 0:57am    
Nice coding Salcedo. Thank you very much.
But what about when I want an option to switch for print directly without preview.
case "Print":
Isaac Salcedo 13-Mar-16 3:43am    
I never tried that, but I hope the example below may help you.

1. Create a RenderingCompleteEventHandler that shows the PrintDialog of the ReportViewer.

private void printReport(object sender, RenderingCompleteEventArgs e)
{
reportViewer.PrintDialog();
}

2. In the case "Print" refresh the report and add the RenderingCompleteEventHandler to the RenderingComplete event of the reportViewer.

case "Print":
reportViewer.RefreshReport();
reportViewer.RenderingComplete += new RenderingCompleteEventHandler(printReport);
break;

If you want to print the report without rendering or refreshing the reportViewer (send the report directly to the printer) this article may help: https://blogs.msdn.microsoft.com/brianhartman/2009/02/27/manually-printing-a-report/
Member 12360367 14-Mar-16 4:06am    
Thanks. It will pop up a print dialogue, but I have the method to streaming the report.rdlc(not to the report viewer) and send the streaming to the printer. It will print the report directly without pop up the print dialogue. I want this. But there is no option to print a report form report viewer without pop up print dialogue or without preview. So that I have to have two methods. One for passing data table and parameters to report viewer for export or preview and another for passing data table and parameters to the report.rdlc(not to the report viewer) for printing. I expect there should be a way to do all in one method.
Isaac Salcedo 14-Mar-16 6:27am    
I create a method that takes the reportViewer LocalReport, the path of report.rdlc, a DataTable and an option (preview, print, exportToPDF, exportToExcel).

private void processReport(ReportViewer reportViewer, string reportPath, DataTable data, string option)
{
prepareReport(reportViewer.LocalReport, reportPath, data);

switch (option)
{
case "preview":
reportViewer.RefreshReport();
case "print":
sendToPrinter(reportViewer.LocalReport);
break;
case "exportToPDF":
generateFile("PDF", reportViewer.LocalReport, "Reports/report.pdf");
break;
case "exportToExcel":
generateFile("Excel", reportViewer.LocalReport, "Reports/report.xls");
break;
default:
MessageBox.Show("The option is not valid.");
break;
}
}

Code for preview the report:
processReport(reportViewer.LocalReport, "report.rdlc", data, "preview");
Code for print the report:
processReport(reportViewer.LocalReport, "report.rdlc", data, "print");
Code for export the report:
processReport(reportViewer.LocalReport, "report.rdlc", data, "exportToPDF");
processReport(reportViewer.LocalReport, "report.rdlc", data, "exportToExcel");

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900