0

I'm new to the world of Crystal Reports, and I've found a variety of articles on creating a new Crystal Report in VS 2010, but nothing about taking an existing report and using it to generate a PDF that can be exported by a user.

This is the article I found I was trying to use as a start: Crystal Reports in ASP.NET MVC

I ran SQL Profiler when running the original report in our ERP system, and tried to use this query as my DataSource, but each time I get to the point where I am loading my report file, it throws an exception:

    Exception Details: System.Runtime.InteropServices.COMException: Database logon failed.

If I debug through the code, the HasRecords

    '((CrystalDecisions.CrystalReports.Engine.ReportDocument)(rptH)).HasRecords' threw an exception of type 'CrystalDecisions.CrystalReports.Engine.LogOnException'

I've also tried adding the SetDatabaseLogin method, with a username and password with DBO access to the database, but got the same results.

    rptH.SetDatabaseLogon("username", "password");

So to make a long story longer, I'm wondering if anyone knows of a good article, or can give me some foolproof steps to get the report working in my code, where I can have a PDF returned (even if I hardcode the SQL first... I just want to see it working).

Thanks!

Community
  • 1
  • 1
someguy0005
  • 125
  • 2
  • 12

1 Answers1

0

Here's an overly simplified version of how we do it where I work...

protected void btnGetReport_Click(object sender, EventArgs e)
{
   System.Guid desiredGuid = System.Guid.NewGuid();
   desiredGuid = createReport();
   //open report in browser
   Response.Redirect(@"SecureReports\" + desiredGuid + ".pdf");
}

private System.Guid createReport()
{
    System.Guid desiredGuid = System.Guid.NewGuid();
    string workpath = "";
    ReportDocument reportDocument = new ReportDocument();

    workpath = @"c:\Reports\cr_myReport.rpt";

    reportDocument.FileName = workpath;
    reportDocument.SetDatabaseLogon("user", "pass", "SERVER\\sql", "database");
    reportDocument.SetParameterValue("@param", strParam);
//export CR to a pdf on the server
    reportDocument.ExportToDisk(ExportFormatType.PortableDocFormat, @"c:\SecureReports\" + desiredGuid + ".pdf");
//send location of pdf back to calling function
        return desiredGuid;
    }
dave k
  • 1,329
  • 4
  • 22
  • 44
  • Thanks Dave. I did get past my original error, it turns out I needed (for whatever crazy reason), to recreate all connections in the report to use OLE DB, rather than whatever else they were. If anyone in the future needs to fix a "Database Login" error connecting to a report in C#, (steps for Crystal Reports 2011) 1) try opening the report 2) Click Database > Set Datasource Location 3) Create a new OLE DB connection to your database 4) Update references to every single table in that database to use your new connection. – someguy0005 Aug 03 '12 at 21:12