我正在尝试在打印机上进行自动打印,但发生错误"An Error Occured during Local report Processing"
。这是我的代码:
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;
}
// Export the given report as an EMF (Enhanced Metafile) file.
private void Export(LocalReport report)
{
string deviceInfo =
"<DeviceInfo>" +
" <OutputFormat>EMF</OutputFormat>" +
" <PageWidth>2.5in</PageWidth>" +
" <PageHeight>11in</PageHeight>" +
" <MarginTop>0.5in</MarginTop>" +
" <MarginLeft>2in</MarginLeft>" +
" <MarginRight>5in</MarginRight>" +
" <MarginBottom>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;
}
// Handler for PrintPageEvents
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 void Print()
{
const string printerName =
"HP LaserJet 2300 PCL6 Class Driver";
if(m_streams == null || m_streams.Count == 0)
return;
PrintDocument printDoc = new PrintDocument();
printDoc.PrinterSettings.PrinterName = printerName;
if(!printDoc.PrinterSettings.IsValid)
{
string msg = String.Format(
"Can't find printer \"{0}\".", printerName);
MessageBox.Show(msg, "Print Error");
return;
}
printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
printDoc.Print();
}
Code Button Click Event
ds.Tables.Add(dt);
ds.WriteXmlSchema("sample.xml");
ds.ReadXml("sample.xml");
ds.WriteXmlSchema("sample.xsd");
LocalReport report = new LocalReport();
report.ReportPath = "SaleReport1.rdlc";
report.DataSources.Add(
new ReportDataSource("Table1", ds.Tables[0]));
Export(report);
m_currentPageIndex = 0;
Print();
答案 0 :(得分:2)
这似乎不是您发布的代码的问题,而是报告定义中的错误。
你必须捕获Exception并查看InnerException的各个级别;即:
ex.Message
ex.InnerException.Message
ex.InnerException.InnerException.Message
答案 1 :(得分:0)
我有同样的问题。报告处理期间发生错误。 DatasetName和我发现连接字符串有问题;我不得不从使用Windows身份验证切换到sql身份验证,然后我的报告工作。
报告数据源名称必须与您通过ReportDataSource对象提供的名称匹配