Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to generate a Crystal Report such that on generation it exports to a folder on my application server. Additionally i want the report to also have a dynamic name. Currently the generated report is taking the name of the ActionResult as it s name and is being downloaded to Downloads folder.

I have amended the source but i have an exception whilst specifying these parameters "Cannot implicitly convert type void to 'System.I0.Stream'".

This is how im generating the Report after adding the parameters to the stream instance. The exception is also occuring on this line:

C#
public ActionResult AuditTrail()
      {

          SqlConnection con = new SqlConnection(Helpers.DatabaseConnect);
          DataTable dt = new DataTable();
          try
          {

              DateTime Today = DateTime.Today;
              var startdate = new DateTime(Today.Year, Today.Month, 1);
              var enddate = startdate.AddMonths(1).AddDays(-1);

              con.Open();
              SqlCommand cmd = new SqlCommand("SELECT Action,CreatedBy,CreatedOn,Entity,Username,Checker FROM Trail ", con);
              SqlDataAdapter adp = new SqlDataAdapter(cmd);
              adp.Fill(dt);

          }
          catch (Exception ex)
          {
              ViewBag.ErrorMessage = Helpers.Messages.GENERAL_ERROR;
              return View();
          }

          DateTime Todayy = DateTime.Today;
          var startdatte = new DateTime(Todayy.Year, Todayy.Month, 1);
          var enddatte = startdatte.AddMonths(1).AddDays(-1);

          string filename = "Trail";
          string OutputFileName = filename.ToString();

          //string fileName = System.IO.Path.GetTempPath() + Guid.NewGuid().ToString() + ".pdf";

          ReportClass rptH = new ReportClass();
          rptH.FileName = Server.MapPath(("~/Reports/") + "AuditTrail.rpt");
          rptH.Load();
          rptH.SetDataSource(dt);

          Stream stream = rptH.ExportToDisk(ExportFormatType.PortableDocFormat, @"C:\Users\myuser\source\repos\MyProject\MySolution\GeneratedReports\" + OutputFileName + "_" + enddatte + ".pdf");

          return File(stream, "application/pdf");
      }


What am i missing?

What I have tried:

I have attempted using CrystalDecisions.Shared.DiskFileDestinationOptions but im not sure how it can be used with Stream.
Posted
Updated 7-Jan-20 2:26am

If you look at the documentation at ReportDocument.ExportToDisk Method (CrystalDecisions.CrystalReports.Engine) | Microsoft Docs[^], you can clearly see that ExportToDisk does not return a stream object.
 
Share this answer
 
v2
Comments
Maciej Los 6-Jan-20 10:29am    
Reading the documentation is the best way to resolve issue!
Richard MacCutchan 6-Jan-20 11:11am    
Strange that so many 'developers' never think of doing it. :(
Tshumore 6-Jan-20 10:44am    
Thanks guys, but is there something amiss in how im passing the parameters to stream
Richard MacCutchan 6-Jan-20 11:10am    
Yes, please read my message again. The call to ExportToDisk does not return a stream object, so you cannot use it in the File constructor in the next line.
This solved my issue-- I can generate, preview(in new browser window) and save to a specified server directory using a dynamic name

C#
public ActionResult AuditTrail()
       {
           string batch = string.Empty;
           batch = System.Web.HttpContext.Current.Session["BatchId"].ToString();

           SqlConnection con = new SqlConnection(Helpers.DatabaseConnect);
           DataTable dt = new DataTable();
           try
           {

               DateTime Today = DateTime.Today;
               var startdate = new DateTime(Today.Year, Today.Month, 1);
               var enddate = startdate.AddMonths(1).AddDays(-1);

               con.Open();
               SqlCommand cmd = new SqlCommand("SELECT Action,CreatedBy,CreatedOn,Entity,Username,Checker FROM Trail ", con);
               SqlDataAdapter adp = new SqlDataAdapter(cmd);
               adp.Fill(dt);

           }
           catch (Exception ex)
           {
               ViewBag.ErrorMessage = Helpers.Messages.GENERAL_ERROR;
               return View();
           }

           string OutputFileName = batch;

           //string fileName = System.IO.Path.GetTempPath() + Guid.NewGuid().ToString() + ".pdf";

           ReportClass rptH = new ReportClass();
           rptH.FileName = Server.MapPath(("~/Reports/") + "AuditTrail.rpt");
           rptH.Load();
           rptH.SetDataSource(dt);
           string filePath = Server.MapPath("~/GeneratedReports/") + OutputFileName + ".pdf";
           rptH.ExportToDisk(ExportFormatType.PortableDocFormat, filePath);
           Stream stream = rptH.ExportToStream(ExportFormatType.PortableDocFormat);

           return File(stream, "application/pdf");
       }


However im getting error 'The given path's format is not supported' when i try naming the file as i would want . ie using the BatchId and the current month-end date :

C#
string batch = string.Empty;
          batch = System.Web.HttpContext.Current.Session["BatchId"].ToString();
          DateTime Todayy = DateTime.Today;
          var startdatte = new DateTime(Todayy.Year, Todayy.Month, 1);
          var enddatte = startdatte.AddMonths(1).AddDays(-1);
          var monthend = enddatte.ToString();
          string OutputFileName = batch + " " + monthend;
          string filePath = Server.MapPath("~/GeneratedFolder/") + OutputFileName + ".pdf";
          rptH.ExportToDisk(ExportFormatType.PortableDocFormat, filePath);
 
Share this answer
 
Comments
phil.o 7-Jan-20 9:14am    
Probably because the date-part in the filename contains characters (i.e., /) which are invalid in a filename.
Put a breakpoint on the line where you declare the monthend variable and you will be able to spot what it contains; then match it to the list of forbidden characters for the filesystem you are trying to write to, and modify your code accordingly.

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