Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,

I want to create a scheduled job that exports a crystal report to pdf. I'm using Quartz NuGet as a scheduler. Here's my code:

C#
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using Quartz;
using System;
using System.IO;
using System.Net;
using System.Web;
 
namespace TestSchedule.App_code
{
    public class LettersJob:IJob
    {
        public static string text;
 
        public void Execute(IJobExecutionContext context)
        {
            int appno = 2;
            int regNum = 1;
 
            PrintLetter(appno, regNum);
        }
 
        public void PrintLetter(int appno,int regNum)
        {
 
            text = text + "A";
            System.IO.File.WriteAllText("C:\\temp\\WriteText.txt", text);
 
            ReportDocument cryRpt = new ReportDocument();
            Random random = new Random();
            int randomNumber = random.Next(0, 100000);
 
            try
            {
                if (regNum == 1)
                    cryRpt.Load(System.Web.HttpContext.Current.Server.MapPath("~/ApproveLetterA.rpt"));
                else
                {
                    cryRpt.Load(System.Web.HttpContext.Current.Server.MapPath("~/ApproveLetterB.rpt"));
                }
 
                cryRpt.SetParameterValue("@appno", appno);             
 
                ExportOptions CrExportOptions;
                DiskFileDestinationOptions CrDiskFileDestinationOptions = new DiskFileDestinationOptions();
                PdfRtfWordFormatOptions CrFormatTypeOptions = new PdfRtfWordFormatOptions();
                CrDiskFileDestinationOptions.DiskFileName = "C:\\temp\\CertNo" + appno + randomNumber + ".pdf";
                CrExportOptions = cryRpt.ExportOptions;
                  {
                    CrExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
                    CrExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
                    CrExportOptions.DestinationOptions = CrDiskFileDestinationOptions;
                    CrExportOptions.FormatOptions = CrFormatTypeOptions;
                  }
                    cryRpt.Export();                
            }
            catch (Exception ex) { Console.Write(ex.Message); }
        }

C#
using Quartz;
using Quartz.Impl;
using System;
 
namespace TestSchedule.App_code
{
    public class JobScheduler
    {
        private IScheduler scheduler;
 
        public void Start()
        {
            scheduler = StdSchedulerFactory.GetDefaultScheduler();
            scheduler.Start();
 
            IJobDetail job = JobBuilder.Create<lettersjob>().Build();
 
            ITrigger trigger = TriggerBuilder.Create()
                       .WithIdentity("trigger1", "group1")
                       .StartNow()
                       .WithSimpleSchedule(x => x
                       .WithIntervalInSeconds(10)
                       .RepeatForever())
                       .Build();
 
            scheduler.ScheduleJob(job, trigger);
        }
    }
}

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Optimization;
using System.Web.Routing;
using System.Web.Security;
using System.Web.SessionState;
using TestSchedule.App_code;
 
namespace TestSchedule
{
    public class Global : HttpApplication
    {
        JobScheduler jobScheduler;
 
        void Application_Start(object sender, EventArgs e)
        {
            // Code that runs on application startup
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
 
            jobScheduler = new JobScheduler();
            jobScheduler.Start();
        }
    }
}


The problem is that I cannot get the rpt exported to pdf when PrintLetter runs on scheduler.
Any ideas what am I doing wrong? Is there another way to implement the scheduler?

Thank you in advance.

What I have tried:

1. I run the PrintLetter on Default.aspx Load and I get the exported file as I supposed to.
2. I added a write to file command on PrintLetter and run the scheduler, and the text is written on file as it supposed to.
3. I run the PrintLetter through the scheduler I only get the text written in the text file.
4. I try using Windows Service based on this example (Repeat Task every N interval using Windows Service in C# and VB.Net[^]) but I get the same results as above, write to file works but exporting the rpt to pdf does not work.
Posted
Updated 2-Dec-16 0:37am
v2

1 solution

I just found the error, it seems that the following line does not work.
C#
cryRpt.Load(System.Web.HttpContext.Current.Server.MapPath("~/ApproveLetterA.rpt));

I changed it to full path and worked.
C#
cryRpt.Load(@"C:\ApproveLetterA.rpt");

not very convenient ... but at least it works.
 
Share this answer
 

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