“Database Logon Failed” error after upgrading from

2019-08-30 04:21发布

问题:

I have a app that was written in VS 2008 for .net 2, upgraded to VS 2010 and built for .net 4. In this app is a Crystal Report. There should be absolutely NO database interaction done by the report. I manually set the various ITextObject fields from one of the objects in my project.

Example:

            to = (CrystalDecisions.CrystalReports.Engine.TextObject)crystalPrint.Section2.ReportObjects["tbName"];
            to.Text = visitor.first + " " + visitor.last;

            to = (CrystalDecisions.CrystalReports.Engine.TextObject)crystalPrint.Section2.ReportObjects["tbCompany"];
            to.Text = visitor.company;

where visitor is the class that I'm pulling data from.

Again, there should be absolutely NO database interaction done with this report, and yet, it somehow thinks that it should be hitting a database.

Here's the relevant stack trace: CrystalDecisions.ReportAppServer.DataSetConversion

EDIT: It's blowing up when I call .PrintToPrinter(1, false, 1, 0);

*EDIT / UPDATE * I poked into the app.config file, and I found this little block

<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>

With that in the app, my report prints. With that commented out, it blows up.....

EDIT 3:

More source code:

 cryBadge crystalPrint = new cryBadge();
 crystalPrint.ReportOptions.EnableSaveDataWithReport = false;

 //NOTE: pi is an internal object used to find printer information.
 crystalPrint.PrintOptions.PrinterName = pi.PrinterName;
 numSize = pi.FindPaperSizeIndex(height, wid);
 to = (CrystalDecisions.CrystalReports.Engine.TextObject)crystalPrint.Section2.ReportObjects["tbName"];
            to.Text = visitor.first + " " + visitor.last;

 to = (CrystalDecisions.CrystalReports.Engine.TextObject)crystalPrint.Section2.ReportObjects["tbCompany"];
 to.Text = visitor.company;
 crystalPrint.PrintOptions.PaperOrientation = CrystalDecisions.Shared.PaperOrientation.Landscape;
 crystalPrint.PrintToPrinter(1, false, 1, 0);

回答1:

Alright. Figured it out.

I had to enter in this block of code into the app.config file:

<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>

Then make sure the config file stays with the .exe For example, if the app called "TestApp1", there should be a TestApp1.exe.config file in the output folder. That .config file needs to stay with the application.



回答2:

For example if I wanted to print a report using Crystal Reports I would do something like the following below

based on your example

.PrintToPrinter(1, false, 1, 0);

you may want to try refactoring your code here is an example

ReportDocument rd = new ReportDocument();
// Insert code to run the report here

// This gets the user's default printer to print to.
PrintDialog prt = new PrintDialog();
rd.PrintOptions.PrinterName = prt.PrinterSettings.PrinterName; 
// This does the printing.
rd.PrintToPrinter(copies, true, 1, 1000); 

A working example that you can try as well below..

//Note untested
PrintDialog pDialog = new PrintDialog();

Nullable<Boolean> print = pDialog.ShowDialog();
if (print == true)
{
    string value1 = "This is a Test";
    string value2= "Happy Halloween";
    ReportDocument rd = new ReportDocument();
    rd.Load("ReportFile.rpt");
    rd.SetParameter("Parameter1", value1);
    rd.SetParameter("Parameter2", value2);
    rd.PrintOptions.PrinterName = pd.PrinterSettings.PrinterName;
    rd.PrintToPrinter(1, false,0,0);
}