0

What I'm trying to accomplish: - Run Crystal Report straight to PDF (bypassing the viewer) - It requires a database login. - It takes one parameter. It shows as '@rpt_args' in CR 11 application. - This Crystal report calls a Store procedure for it's result set.

Partial solution

My code:

        ReportClass rptH = new ReportClass();         
        //rptH.FileName = Server.MapPath("whkinvc.rpt");      
        rptH.FileName = "D:\\whkinvc.rpt";      
        rptH.Load();        
        rptH.SetDatabaseLogon("FAKEUSER", "FAKEPASSWORD", "FAKESERVER", "FAKEDB");
        rptH.SetParameterValue("@rpt_args", atm.WorkingModel.Header.TicketNumber);         
        Stream stream = rptH.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);         
        return File(stream, "application/pdf");

I'm currently getting a database login failure. Do I have my code setup correctly to perform the action I'm looking for? Any possible reason for the database login failure?

Thanks,

** Update **

I got it working through VS with the following code (The database login error was of result of a typo):

        ReportClass rptH = new ReportClass();
        rptH.FileName = Server.MapPath(Url.Content("~/app_data/whkinvc.rpt"));
        rptH.Load();

        rptH.SetDatabaseLogon(...);

        ParameterValues xyz = new ParameterValues();
        ParameterDiscreteValue pdv = new ParameterDiscreteValue();
        pdv.Value = atm.WorkingModel.Header.TicketNumber;

        xyz.Add(pdv);
        rptH.DataDefinition.ParameterFields["@rpt_args"].ApplyCurrentValues(xyz);
        Stream stream = rptH.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);         
        return File(stream, "application/pdf"); 

I would like the output to go to a new tab or window. Currently it just replaces the current window.

Community
  • 1
  • 1
Elim99
  • 663
  • 3
  • 17
  • 34
  • Trying to do the same with CR 10.5. The following works on my dev box, but not on server: `ReportDocument doc = new ReportDocument(); – erict Nov 03 '11 at 13:53
  • `ReportDocument doc = new ReportDocument(); doc.Load(Server.MapPath("~/app_data/invoice.rpt")); doc.SetDatabaseLogon(...); ParameterValues xyz = new ParameterValues(); xyz.Add(new ParameterDiscreteValue().Value = id); doc.DataDefinition.ParameterFields["Id"].ApplyCurrentValues(xyz); doc.ExportToDisk(ExportFormatType.PortableDocFormat, Server.MapPath("~/app_data/invoice.pdf"));' I assume the fact that I have CR installed on dev, but not on server??? – erict Nov 03 '11 at 14:11
  • Hi erict. I imagine you might have to wrap your paths with "URL.Content" for it to find the correct path. Ex. Url.Content("~/app_data/whkinvc.rpt") – Elim99 Nov 03 '11 at 14:35
  • I believe my previous comment is only a ASP.NET MVC solution. – Elim99 Nov 03 '11 at 14:41

2 Answers2

2

I got it working through VS with the following code (The database login error was of result of a typo):

ReportClass rptH = new ReportClass();         
rptH.FileName = Server.MapPath(Url.Content("~/app_data/whkinvc.rpt"));         
rptH.Load();          
rptH.SetDatabaseLogon(...);          
ParameterValues xyz = new ParameterValues();         
ParameterDiscreteValue pdv = new ParameterDiscreteValue();         
pdv.Value = atm.WorkingModel.Header.TicketNumber;          
xyz.Add(pdv);         
rptH.DataDefinition.ParameterFields["@rpt_args"].ApplyCurrentValues(xyz);         
Stream stream = rptH.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);                  
return File(stream, "application/pdf");  
Damith
  • 62,401
  • 13
  • 102
  • 153
Elim99
  • 663
  • 3
  • 17
  • 34
0

In the past I've had to set the connection string for every table (or stored proc or whatever) in the report, and also in every table in every sub report.

For example:

(Suppose rd is an instance of ReportDocument, and cn is a preprepared instance of ConnectionInfo)

void SetupConnection(ReportDocument rd, ConnectionInfo cn)
{

    foreach (Table t in rd.database.Tables)
    {
        TableLoginInfo li = t.LogOnInfo;
        li.ConnectionInfo = cn;
        t.ApplyLogOnInfo(li);
        t.Location = t.Location;
    }
    if (!rd.IsSubReport) 
    {
         foreach (ReportDocument sr in rd.SubReports)
             SetupConnection(sr,cn);
    }
}

I realise some of this makes little sense, (eg t.Location = t.Location). Its a while since I used it, but I believe that these were necessary for it to work properly.

Jon Egerton
  • 40,401
  • 11
  • 97
  • 129