Crystal Reports crash in second printing - c#

I was made to print a report in C#. But I found the error. For the first print run normally. But for the second time, my program is immediately stopped.
The window error is something like that :
has encountered a problem and needs to close. we are sorry for the inconvenience
The code is :
ReportDocument rptDocument = new ReportDocument();
PrinterSettings settings = new PrinterSettings();
public void cetak() {
rptDocument.Load(Application.StartupPath + "/report/fakturpenjualan.rpt");
rptDocument.SetParameterValue("idpenjualan", idp);
rptDocument.PrintOptions.PrinterName = settings.PrinterName;
rptDocument.PrintToPrinter(1, true, 0, 0);
rptDocument.Close();
}
What should I do?

Be sure and DISPOSE the object after closing it and force a garbage collection. The runtime engine has issues if the report object is not completely destroyed.

Related

Crystal report too slow from Visual Studio 2019 and Winforms

I have a form that prints some reports created with Crystal Report (13.030) and Visual Studio 2019. The printing of each report takes about 5/8 seconds. I have tried all the suggestions found on google, but have not solved anything. Since the reports are about 10m, the customer has to wait a long time before being able to continue.
I have read that it is a known problem, but I don't know how to fix it. I've been thinking about doing the prints in a BackgroundWorker, but I'm not sure if that's a good idea.
I checked the No printer in report box. This is one of my reports, which are all very similar. Am I wrong something?
ReportDocument myReport = new ReportDocument();
myReport.Load("myreport.rpt");
PrintLayoutSettings PrintLayout = new PrintLayoutSettings();
PrinterSettings printerSettings = new PrinterSettings();
printerSettings.PrinterName = "PDF Creator"; // Any;
PageSettings pSettings = new PageSettings(printerSettings);
myReport.SetParameterValue("var1", "123");
myReport.SetParameterValue("var2", "ABC");
myReport.SetParameterValue("var3", "DEF");
myReport.PrintToPrinter(printerSettings, pSettings, false, PrintLayout);
myReport.Close();
I moved the prints to as many BackgroundWorkers, obviously everything is very fast, even if I don't know if it's the best solution.

java.lang.illegalstateexception: 'current page not finished!'

after all my trials to create a pdf with a table that contains Arabic text using itext7 failed, I decided to move to the normal way, where I use view.draw and save the result in a pdf. but this also didn't work. here's the code:
Android.Graphics.Pdf.PdfDocument pdf_new = new Android.Graphics.Pdf.PdfDocument();
Android.Graphics.Pdf.PdfDocument.PageInfo pageInfo_new = new Android.Graphics.Pdf.PdfDocument.PageInfo.Builder(6000, 6000, 0).Create();
Android.Graphics.Pdf.PdfDocument.Page page_new = pdf_new.StartPage(pageInfo_new);
ListView listView = new ListView(this.Context);
accounts_info_homeadapter hmm = new accounts_info_homeadapter(this, pdflist);
listView.Adapter = hmm;
listView.SetBackgroundColor(Android.Graphics.Color.Aqua);
listView.Draw(page_new.Canvas);
// lst.Draw(page.Canvas);
//TextView txt = new TextView(this.Context);
//txt.Text = "hi";
//txt.Draw(page.Canvas);
var path = global::Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
file_Path = Path.Combine(path.ToString(), "Client.pdf");
new_stream = new FileStream(file_Path, FileMode.Create);
new_stream.Flush();
pdf_new.WriteTo(new_stream);
new_stream.Close();
pdf_new.Close();
I got the exception: java.lang.illegalstateexception: 'current page not finished!' though I tried this a week ago on a trial list and it was working. now i'm getting this. I thought it might be the size of the pdf so I started increasing it but in vain. I tried changing my variables' names, I thought maybe because I tried to use them with my itext before, but still nothing. I deleted the fragment I 'm working on and then recreated it, also nothing. anyone know what the problem is. thanks in advance.
You missed one line. You forgot to close the page after editing it.
pdf_new.finishPage(page_new)
You must finish the page by calling finishPage(page) before closing the page otherwise it will throw an exception.
From close() docs reference:
Do not call this method if the page returned by startPage(android.graphics.pdf.PdfDocument.PageInfo) is not finished by calling finishPage(android.graphics.pdf.PdfDocument.Page).
In your case it should be:
pdf_new.finishPage(page_new);
pdf_new.close();
Source: close() method docs reference
After inserting your desired text in the canvas make sure to place this line of code.
This helped me to remove my error
canvas.drawText("Financial Statement for 2021",40,50,myPaint);
myPdfDocument.finishPage(myPage1); //this one

Printing multiple report using Crystal Report crash applcation

I have a WinForms Application (.net 4.0, x86) with Crystal Reports (13.0.15). I want to print multiple documents using Crystal Reports (without seeing them on Report Viewer - directly on printer).
foreach (var document in documents )
{
ReportDocument report=GenerateReport(document.id);
report.PrintToPrinter(printerSettings, pageSettings, false);
}
Everytime it prints 48 reports and on 49th print I've got exception System.ComponentModel.Win32Exception The handle is invalid Void OnStartPrint(System.Drawing.Printing.PrintDocument, System.Drawing.Printing.PrintEventArgs)
I tried another set of documents but I still have exception on 49 print.
I tried to change PrintJobLimit to over 9000 but it doesn't help.
Next I tried to dispose report after print:
foreach (var document in documents )
{
using (ReportDocument report=GenerateReport(document.id))
{
report.PrintToPrinter(printerSettings, pageSettings, false);
}
}
But now program crashes before print 28th report and I cannot catch exception (program stop working). Only in Event Viewer is information about Application Error (Faulting module name: ntdll.dll).
I tried different things (and combination of them):
report.Close();
report.Dispose();
report=null;
even GC.Collect() after print but it still doesn't work.
Anybody has a solution?
It seems like when report.PrintToPrinter returned, the underlying operation sometimes went on. You need to keep reportDocument alive for a few more time, instead of let it go out of scope and GC collection kick in.
My solution is to put them into a delayed queue and Close them later, something like:
foreach (var document in documents )
{
ReportDocument report = GenerateReport(document.id);
report.PrintToPrinter(printerSettings, pageSettings, false);
Task.Run(async () => {
// keep report alive for extra 60 secs
await Task.Delay(60 * 1000);
report.Close();
report.Dispose();
})
}

In coded ui how do you correctly retrieve a browser window that contains an embedded Adobe PDF reader in browser

I'am running across this issue when I'm debugging or running my coded UI automation project, where i get the exception labeled "{"COM object that has been separated from its underlying RCW cannot be used." System.Exception {System.Runtime.InteropServices.InvalidComObjectException}" everytime i come from a browser window that contains a pdf reader embedded in it. This happens every time I retrieve the window and try to click back. It barfs when i perform the back method on it. I've tried different things but none has worked including the playback wait.
var hereIsmypdf = ReturnPDFDoc();
public BrowserWindow ReturnPDFDoc()
{
Playback.Wait(1000);
var myPdFdoc = GlobalVariables.Browser;
return myPdFdoc;
}
hereIsmypdf.Back();
The only way i was able to get around this issue was not to use the BrowserWindow class. I ended up using the WinWindow class and just getting the tab of the window from it. The BrowserWindow class seemed to trigger the exception "COM object that has been separated from its underlying RCW cannot be used." System.Exception {System.Runtime.InteropServices.InvalidComObjectException}" everytime i tried to retrieve it. I hope this helps someone one or maybe someone has a better way to handle this issue.
For the people that voted my question down, i really did try to figure it out. Sorry i wasnt clear about what i was asking the community or couldn't properly articulate what this pain was. I'm sure someone probably is going through the same pain i did and having a hard time articulating whats going on.
Here is my code on what i ended up doing
public WinTabPage ReturnPDFDoc()
{
WinWindow Wnd = new WinWindow();
Wnd.SearchProperties[BrowserWindow.PropertyNames.ClassName] = "IEFrame";
WinTabList tabRoWlist = new WinTabList(Wnd);
tabRoWlist.SearchProperties[WinTabPage.PropertyNames.Name] = "Tab Row";
WinTabPage myTab = new WinTabPage(tabRoWlist);
myTab.SearchConfigurations.Add(SearchConfiguration.AlwaysSearch);
myTab.SearchProperties[WinTabPage.PropertyNames.Name] = "something";
//UITestControlCollection windows = newWin.FindMatchingControls();
return myTab;
}

Opening Access report via .net working, but not working

I am trying to open an access report through .net. I can open it in normal mode using Access.AcView.acViewNormal, but it's not what I want to do because that prompts the user to save the first and then it can be viewed. I want the report to pop up and it seems .acViewPreview or .acViewReport is what i should use, but it's not popping up.
It does open up a process and seems to be opening, but i can't see the report. Perhaps it's closing really fast. The following is the code I have currently
private void buttonResults_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Access.Application oAccess = new Microsoft.Office.Interop.Access.Application();
oAccess.OpenCurrentDatabase("D:\\path.mdb",true);
try
{
oAccess.DoCmd.OpenReport("rptChartData", Microsoft.Office.Interop.Access.AcView.acViewPreview);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
What am i doing wrong?? Thanks in advance...
I don't know Dot Net. I put this code in an Excel module and it created an Access instance and displayed my report. The key seemed to be oAccess.Visible = True Without setting Visible = True, Access opened (and remained open) with the report but was just not visible.
Dim strDbFullPath As String
Dim oAccess As Access.Application
strDbFullPath = "C:\Access\webforums\whiteboard2003.mdb"
Set oAccess = New Access.Application
oAccess.Visible = True
oAccess.OpenCurrentDatabase strDbFullPath, True
oAccess.DoCmd.OpenReport "rptFoo", acViewPreview
So I think this may not be a Dot Net issue, but standard behavior for an Access application instance.

Categories