Click here to Skip to main content
15,901,035 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I have created PDF at run time in class using report viewer and save these PDF on FTP server. I am not using any window form. All code in windows service application. I have installed this service on server. After some records processed found below error and don't know why. Then I stopped service and restart the service with same issue. Please help.

System.ComponentModel.Win32Exception (0x80004005): Error creating window handle.
   at System.Windows.Forms.NativeWindow.CreateHandle(CreateParams cp)
   at System.Windows.Forms.Control.CreateHandle()
   at System.Windows.Forms.Control.get_Handle()
   at System.Windows.Forms.Control.PointToScreen(Point p)
   at System.Windows.Forms.ToolStripItem.TranslatePoint(Point fromPoint, ToolStripPointType fromPointType, ToolStripPointType toPointType)
   at System.Windows.Forms.ToolStripDropDownItem.DropDownDirectionToDropDownBounds(ToolStripDropDownDirection dropDownDirection, Rectangle dropDownBounds)
   at System.Windows.Forms.ToolStripDropDownItem.GetDropDownBounds(ToolStripDropDownDirection dropDownDirection)
   at System.Windows.Forms.ToolStripDropDownItem.get_DropDownLocation()
   at System.Windows.Forms.ToolStripDropDown.GetDropDownBounds(Rectangle suggestedBounds)
   at System.Windows.Forms.ToolStripDropDown.SetBoundsCore(Int32 x, Int32 y, Int32 width, Int32 height, BoundsSpecified specified)
   at System.Windows.Forms.Control.SetBounds(Int32 x, Int32 y, Int32 width, Int32 height, BoundsSpecified specified)
   at System.Windows.Forms.Control.set_Size(Size value)
   at System.Windows.Forms.ToolStripDropDown.AdjustSize()
   at System.Windows.Forms.ToolStripOverflow.OnLayout(LayoutEventArgs e)
   at System.Windows.Forms.Control.PerformLayout(LayoutEventArgs args)
   at System.Windows.Forms.Control.PerformLayout()
   at System.Windows.Forms.Control.ResumeLayout(Boolean performLayout)
   at System.Windows.Forms.Control.ResumeLayout()
   at System.Windows.Forms.ToolStrip.OnLayout(LayoutEventArgs e)
   at System.Windows.Forms.Control.PerformLayout(LayoutEventArgs args)
   at System.Windows.Forms.Control.PerformLayout()
   at System.Windows.Forms.Control.ResumeLayout(Boolean performLayout)
   at System.Windows.Forms.Control.SetBoundsCore(Int32 x, Int32 y, Int32 width, Int32 height, BoundsSpecified specified)
   at System.Windows.Forms.TextBoxBase.SetBoundsCore(Int32 x, Int32 y, Int32 width, Int32 height, BoundsSpecified specified)
   at System.Windows.Forms.Control.System.Windows.Forms.Layout.IArrangedElement.SetBounds(Rectangle bounds, BoundsSpecified specified)
   at System.Windows.Forms.ToolStripControlHost.OnBoundsChanged()
   at System.Windows.Forms.ToolStripItem.SetBounds(Rectangle bounds)
   at System.Windows.Forms.ToolStripItem.set_Width(Int32 value)
   at Microsoft.Reporting.WinForms.ReportToolBar..ctor()
   at Microsoft.Reporting.WinForms.ReportViewer.InitializeComponent()
   at Microsoft.Reporting.WinForms.ReportViewer..ctor()
Posted
Updated 23-Sep-16 9:39am
v3
Comments
Richard MacCutchan 28-Mar-15 11:21am    
What is the service doing?
Mahesh.Dhiman27 30-Mar-15 3:05am    
Hi,

My service fetching data from sqlserver database and create pdf using report viewer and then save that pdf on ftp server. I am not using any window form.

Steps:
1) craeting report viewer object in method like ReportViewer rptViewer = new ReportViewer();
2) Fetching data from database and fill dataset.
3) Set all report viewer properties.
4) Create ftp request.
5) write report viewer bytes data on the ftp server using ftp stream.
6) rptViewer.LocalReport.Refresh(); rptViewer.RefreshReport();
7) Clear and Dispose dataset object.
8) clear and Dispose report viewer object

After that same process for next record.

Thanks
Mahesh Dhiman
Sergey Alexandrovich Kryukov 28-Mar-15 20:21pm    
What's the use of looking at the exception stack without seeing the code throwing that exception?
—SA
Mahesh.Dhiman27 30-Mar-15 3:12am    
Hi,

Here my code, please check.

public static void GenerateNAVPDF(string strBoxID, string strWebOrder, string strDocketNo, string ftpIP, string folderName)
{
ReportViewer rptViewer = new ReportViewer();
try
{

logger.Debug("Generate PDF start");
Warning[] warnings;
string[] streamIds;
string mimeType = string.Empty;
string encoding = string.Empty;
string extension = string.Empty;
string devInfo = "<deviceinfo><outputformat>PDF" +
"<PageWidth>5.83in</PageWidth>" +
"<PageHeight>8.27in</PageHeight>" +
"<margintop>0in" +
"<marginleft>0in" +
"<marginright>0in" +
"<marginbottom>0in" +
"";

ProcessInvoiceDataBL objInvoiceBL = new ProcessInvoiceDataBL();
DataSet dsInvoice = null;

dsInvoice = objInvoiceBL.GetPostedInvoiceDetail(strBoxID);
logger.Debug("Fetch Invoice Data Successfully.");
if (dsInvoice != null && dsInvoice.Tables.Count > 1)
{
if (dsInvoice.Tables[0].Rows.Count > 0)
{
if (Convert.ToString(dsInvoice.Tables[0].Rows[0]["giftorder"]) == "1")
{
logger.Debug("This is a Gift Order,Please use Nav for Printing this Invoice. Shipment label is being printed.");
}
else
{
logger.Debug("Invoice Pdf start");
BarcodeLib.Barcode b = new BarcodeLib.Barcode();
b.Alignment = BarcodeLib.AlignmentPositions.CENTER;
b.LabelPosition = BarcodeLib.LabelPositions.TOPCENTER;
BarcodeLib.TYPE type = BarcodeLib.TYPE.CODE128;
b.IncludeLabel = false;
b.LabelPosition = BarcodeLib.LabelPositions.TOPCENTER;
System.Drawing.Image img = b.Encode(type, Convert.ToString(dsInvoice.Tables[0].Rows[0]["DocketNo"]), 400, 100);
dsInvoice.Tables[0].Columns.Add("ImgData", typeof(byte[]));
dsInvoice.Tables[0].Rows[0]["ImgData"] = imageToByteArray(img);

rptViewer.ProcessingMode = ProcessingMode.Local;

rptViewer.LocalReport.DataSources.Clear();

rptViewer.LocalReport.ReportPath = @"C:\UploadNAVPDFToFTPLog\Invoice.rdlc";

rptViewer.LocalReport.DataSources.Clear();
rptViewer.LocalReport.DataSources.Add(new ReportDataSource("dsInvoice_Header", dsInvoice.Tables[0]));
rptViewer.LocalReport.DataSources.Add(new ReportDataSource("dsInvoice_Lines", dsInvoice.Tables[1]));
logger.Debug("RPT Datasource Assiged, Render Start");
byte[] bytes = rptViewer.LocalReport.Render("PDF", devInfo, out mimeType, out encoding, out extension, out streamIds, out warnings);
logger.Debug("RPT Render Ends");

string file_name = strWebOrder + "_" + strDocketNo + ".pdf"; //save the file




System.Net.FtpWebRequest ftpRequest = null;


logger.Debug("FTP Request Start");
ftpRequest

1 solution

I've experienced something like this before. This is a memory leak of sorts, and revolves around the way .NET deals with "Unmanaged resources".

Your service is creating too many unmanaged Windows Handles and not deleting them in a timely fashion, causing it to hit a hard limit of 10,000 handles.

Look at the Process tab of the Windows Task Manager, click on the view menu, Select Columns. Add User Objects to the list. Most programs will show a few in use, but the offending process will (in time) show thousands.

You need to make sure that every object which you create derived from IDisposable is also Disposed when it is no longer needed. Also, any event handlers to those objects need to be disconnected prior to disposing of them.

If the process is fairly linear, I suggest looking at the using statement. In the following example, Font is derived from IDisposable. The using statement ensures that Dispose is called at the end of the block.

C#
using (Font font1 = new Font("Arial", 10.0f))
{
    byte charset = font1.GdiCharSet;
}


On top of this, you may need to force garbage collection as well to ensure that all of the objects are deleted.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900