471,897 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,897 software developers and data experts.

sending a .rdlc report to printer directly without viewing it.

Hello Friends i m stuck up to a place wherein i want to print a report which is displaying on a reportviewer.

I actually want to send the report to printer directly without viewing it.

i am using a code for the same which i m posting here. I am getting an error on the line wherein i m rendering the report.

the error is " a window handle is not created for the control"

My code is :

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;

public class Demo : IDisposable
private int m_currentPageIndex;
private IList<Stream> m_streams;

private DataTable LoadSalesData()
// Create a new DataSet and read sales data file
// data.xml into the first DataTable.
DataSet dataSet = new DataSet();
return dataSet.Tables[0];
// Routine to provide to the report renderer, in order to
// save an image for each page of the report.
private Stream CreateStream(string name,
string fileNameExtension, Encoding encoding,
string mimeType, bool willSeek)
Stream stream = new FileStream(@"..\..\" + name +
"." + fileNameExtension, FileMode.Create);
return stream;
// Export the given report as an EMF (Enhanced Metafile) file.
private void Export(LocalReport report)
string deviceInfo =
"<DeviceInfo>" +
" <OutputFormat>EMF</OutputFormat>" +
" <PageWidth>8.5in</PageWidth>" +
" <PageHeight>11in</PageHeight>" +
" <MarginTop>0.25in</MarginTop>" +
" <MarginLeft>0.25in</MarginLeft>" +
" <MarginRight>0.25in</MarginRight>" +
" <MarginBottom>0.25in</MarginBottom>" +
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
ev.Graphics.DrawImage(pageImage, ev.PageBounds);
ev.HasMorePages = (m_currentPageIndex < m_streams.Count);

private void Print()
const string printerName =
"Microsoft Office Document Image Writer";
if (m_streams == null || m_streams.Count == 0)
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");
printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
// Create a local report for Report.rdlc, load the data,
// export the report to an .emf file, and print it.
private void Run()
LocalReport report = new LocalReport();
report.ReportPath = @"..\..\Report.rdlc";
new ReportDataSource("Sales", LoadSalesData()));
m_currentPageIndex = 0;

public void Dispose()
if (m_streams != null)
foreach (Stream stream in m_streams)
m_streams = null;


the code in bold is the place i am getting error ......please help me please i m badly stuck..

Oct 10 '08 #1
0 3849

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

9 posts views Thread by Altman | last post: by
18 posts views Thread by =?Utf-8?B?SGVyYg==?= | last post: by
reply views Thread by =?Utf-8?B?RnJhbmsgVXJheQ==?= | last post: by
reply views Thread by YellowAndGreen | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.