I have a error when I try to export Report Viewer. The Try{}Catch{] don´t get the Exceptions and the forum Micorsoft C# didn´t answred my post.
The problem occur in the forms MDI childs of a StrataFrame Caller.
The error is:
The runtime has encountered a fatal error. The address of the error was at 0x61a4f96c, on thread 0x1e08. The error code is 0xc0000005. This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code. Common sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack.
I don´t have to do now, Bellow the code:
using System;
using System.IO;
using System.Data;
using System.Text;
using System.Drawing.Imaging;
using System.Drawing.Printing;
using System.Collections.Generic;
using System.Windows.Forms;
using Microsoft.Reporting.WinForms;
namespace Relatorios.Reports.Danfe
{
public class DanfeDireto : IDisposable
{
private int m_currentPageIndex;
private IList<Stream> m_streams;
private int _nfeId;
private string _impName;
private int _qtdeCopias;
/* //Comenta construtor
public DanfeDireto(int nfeId, string ImpName, int QtdeCopias)
{
_nfeId = nfeId;
_impName = ImpName;
_qtdeCopias = QtdeCopias;
}*/
private Stream CreateStream(string name, string fileNameExtension, Encoding encoding,
string mimeType, bool willSeek)
{
Stream stream = new FileStream(name + "." + fileNameExtension, FileMode.Create);
m_streams.Add(stream);
return stream;
}
private void Export(LocalReport report)
{
try
{
string deviceInfo =
"<DeviceInfo>" +
" <OutputFormat>EMF</OutputFormat>" +
" <PageWidth>8.5in</PageWidth>" +
" <PageHeight>11.62in</PageHeight>" +
" <MarginTop>0.0in</MarginTop>" +
" <MarginLeft>0.0in</MarginLeft>" +
" <MarginRight>0.0in</MarginRight>" +
" <MarginBottom>0.0in</MarginBottom>" +
"</DeviceInfo>";
Warning[] warnings;
m_streams = new List<Stream>();
report.Render("Image", deviceInfo, CreateStream, out warnings);
foreach (Stream stream in m_streams)
stream.Position = 0;
}
catch (Exception ex)
{
MessageBox.Show(ex.InnerException.ToString());
}
}
private void PrintPage(object sender, PrintPageEventArgs ev)
{
Metafile pageImage = new Metafile(m_streams[m_currentPageIndex]);
ev.Graphics.DrawImage(pageImage, ev.PageBounds);
m_currentPageIndex++;
ev.HasMorePages = (m_currentPageIndex < m_streams.Count);
}
/*
private string ObterImpressoraPadrao()
{
string impressoraPadrao = string.Empty;
string strQuery = "SELECT * FROM Win32_Printer";
ObjectQuery objectQuery = new ObjectQuery(strQuery);
ManagementObjectSearcher query = new ManagementObjectSearcher(objectQuery);
ManagementObjectCollection queryCollection = query.Get();
foreach (ManagementObject managementObject in queryCollection)
{
PropertyDataCollection propertyDataCollection = managementObject.Properties;
if ((bool)managementObject["Default"]) // DEFAULT PRINTER
{
impressoraPadrao = managementObject["Name"].ToString();
}
}
return impressoraPadrao;
}
*/
private void Print()
{
string printerName = _impName;
if (m_streams == null || m_streams.Count == 0)
return;
PrintDocument printDoc = new PrintDocument();
printDoc.PrinterSettings.PrinterName = _impName;
printDoc.PrinterSettings.Copies = Convert.ToInt16( _qtdeCopias);
if (!printDoc.PrinterSettings.IsValid)
{
string msg = String.Format("Can't find printer \"{0}\".", printerName);
Console.WriteLine(msg);
return;
}
printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
printDoc.Print();
}
public void Run(int _nfeId, string ImpName, int QtdeCopias)
{
this._nfeId = _nfeId;
this._impName = ImpName;
this._qtdeCopias = QtdeCopias;
Business_Enterprise.EmpresaBO Emp = new Business_Enterprise.EmpresaBO();
DanfeClass danfeClass = new DanfeClass();
Emp.FillByPrimaryKey(1);
MicroFour.StrataFrame.Business.BusinessBindingSource bsEmpresas = new MicroFour.StrataFrame.Business.BusinessBindingSource();
bsEmpresas.BusinessObject = Emp;
CarregaXmlClass car = new CarregaXmlClass();
danfeClass = car.carregaXml(_nfeId);
System.Windows.Forms.BindingSource bsNFe = new System.Windows.Forms.BindingSource();
bsNFe.DataSource = danfeClass;
Produtos prod = new Produtos(_nfeId);
System.Windows.Forms.BindingSource bsProd = new System.Windows.Forms.BindingSource();
bsProd.DataSource = prod.GetProdutos();
LocalReport report = new LocalReport();
report.ReportPath = "DanfePag1.rdlc"; //"..\\..\\..\\Relatorios\\Reports\\Danfe\\DanfePag1.rdlc";
report.DataSources.Add(new ReportDataSource("Empresa", bsEmpresas));
report.DataSources.Add(new ReportDataSource("NFe", bsNFe));
report.DataSources.Add(new ReportDataSource("Produtos", bsProd));
Export(report);
m_currentPageIndex = 0;
Print();
}
public void Dispose()
{
if (m_streams != null)
{
foreach (Stream stream in m_streams)
stream.Close();
m_streams = null;
}
}
}
}
I wait for a help!