Error using Crystal Reports for .net
I wrote a program that generates PDFs from a Crystal Report, then emails those PDFs to someone. The program works great on my development machine, but when I copy the bin\Release directory to the Windows 2000 Server (machine where I'd like to run it), it starts to run and then generates this error and stack trace:
The type initializer for 'CrystalDecisions.CrystalReports.Engine.ReportDocument' threw an exception.
at CrystalDecisions.CrystalReports.Engine.ReportDocument..ctor()
at DailyJobCostSummaryEmail.Program.crptToPDF(String reportFile, String jobNum, String outputLocation) in M:\Projects\DailyJobCostSummaryEmail\DailyJobCostSummaryEmail\Program.cs:line 79
at DailyJobCostSummaryEmail.Program.Main(String[] args) in M:\Projects\DailyJobCostSummaryEmail\DailyJobCostSummaryEmail\Program.cs:line 46
ERROR OCCURS EVERYWHERE EXCEPT WHEN RUNNING FROM VISUAL STUDIO.
.Net 2.0 is installed on that machine, and I've since installed CRRedist2005_x86.msi with no effect. I even get the "Send Error Report to Microsoft" dialog, even though I'm using try/catch to print the exception to a file. Even when the catch block executed, my program will not close properly.
static void Main(string[] args)
{
try
{
String dir = @"JobCostReports";
DataTable jobs = new DataTable();
using (SqlConnection conn = new SqlConnection(connString))
{
String sql = "JC_GetJobsClosedYesterday";
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
da.Fill(jobs);
}
List<String> files = new List<String>();
foreach (DataRow row in jobs.Rows)
{
files.Add(crptToPDF(@"JobCost.rpt", row["JobNumber"].ToString().TrimEnd(), dir));
}
Utilities.sendEmail("[toEmail]",
"[FromEmail]",
"Job Cost Summaries for Yesterday",
"Attached are Job cost summaries for the " + files.Count + " jobs closed yesterday.",
files.ToArray());
Console.WriteLine("Email sent.");
}
catch (Exception e)
{
using (StreamWriter writer = new StreamWriter("errors.log", true))
{
writer.AutoFlush = true;
Console.WriteLine();
writer.WriteLine(e.Message);
writer.WriteLine(e.StackTrace);
}
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
开发者_如何学Go Console.Read();
}
finally { }
}
public static String crptToPDF(String reportFile, String jobNum, String outputLocation)
{
using (ReportDocument rpt = new ReportDocument())
{
rpt.Load(reportFile);
rpt.SetParameterValue("@vJobNumber", jobNum);
String output = outputLocation + @"\" + jobNum + "_JobCostSummary.pdf";
rpt.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, output);
return output;
}
}
Am I not including all the proper references and using statements? I've tried many combinations, but no effect.
This could also be an issue with the fact that the CR assemblies shipped with visual studio are a different version to the full product.
If you are having trouble with the runtimes you download from business objects try the runtime here:
https://wiki.sdn.sap.com/wiki/pages/viewpage.action?pageId=56787567
This seems to be related. Have you tried the steps outlined there?
Check that the debug and release solution configurations in Visual Studio have the same dependencies and settings and that all dependency paths are relative; when things work in VS but not outside I usually find that it is one of those tow problems.
A quick and dirty trick. Hoping it might work.
AFAI can understand, might be the refereneces are missing on the production or the version mistmatch. So to over come this, From your solution, Go to thr references directory -> Select the reference-> Right click on the reference -> Select property -> then choose Copy Local - True. Now build your app and copy the new bin directory and pages to production and check it its running or not. If stillnot running, then Again goto the property of the reference, Choose the path. If its in GAC, then search google for copying dll's from GAC. Or you can Map your Windows folder to same machine and access then navigate through the directory and copy the dlls, but for this you need admin previleges. Now create a new dir inside the solution, name it CRDist, paste all dlls inside the folder and refer from there , at the production end, open Command Prompt and register the dll using regsvr32 -i dll path.
ALSO IN YOUR PDF EXPORTING MECHANISM,
I can see you are loading report file but you are not using any data source. Are you setting the datasource in designtime or what is going behind the sceenes.
Hope this will help
To register managed Assembly. Use this
C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322>RegAsm.exe /tlb dllName.dll
Microsoft (R) .NET Framework Assembly Registration Utility 1.1.4322.573
Copyright (C) Microsoft Corporation 1998-2002. All rights reserved.
Here v1.1.4322 will the framework you are developing. Choose the framework
accordingly. Or goto Start->AllPrograms -> MicrosoftVisual Studio 2005/8/10->
Visual Studio Tools-> Visual Studio Command Prompt.
then run RegAsm.exe /tlb dllName.dll
The server is misconfigured I am afraid. Reinstall the runtimes on it from SAP (NOT VS) which you can find by googling. When you say you want to run it on the server, are you running it as a website or is it the case that you are dropping the RPT on a network share or what? If it is the website, then it is almost without a doubt a misconfigured Crystal install on the server - not your report as such.
I do not think, but do not know for certain, that the runtime installation is going to ask you for a product key at any time, since you are just using the free viewer etc. If you are getting a request for a product key then you are not installing the pure runtimes, I fear.
精彩评论