Sams Teach Yourself .Net in 21 Days
- Table of Contents
- Copyright
- About the Author
- About the Technical Editor
- Acknowledgments
- We Want to Hear from You
- Introduction
- Week 1: At a Glance
- Day 1. Introduction to the Microsoft .NET Framework
- Day 2. Introduction to Visual Studio .NET
- Day 3. Writing Windows Forms Applications
- Day 4. Deploying Windows Forms Applications
- Day 5. Writing ASP.NET Applications
- Day 6. Deploying ASP.NET Applications
- Day 7. Exceptions, Debugging, and Tracing
- Week 1. In Review
- Week 2: At a Glance
- Day 8. Core Language Concepts in Visual Basic .NET and C#
- Day 9. Using Namespaces in .NET
- Day 10. Accessing Data with ADO.NET
- Day 11. Understanding Visual Database Tools
- Day 12. Accessing XML in .NET
- Day 13. XML Web Services in .NET
- Day 14. Components and .NET
- Week 2. In Review
- Week 3: At a Glance
- Day 15. Writing International Applications
- Day 16. Using Macros in Visual Studio .NET
- Day 17. Automating Visual Studio .NET
- Day 18. Using Crystal Reports
- Day 19. Understanding Microsoft Application Center Test
- Day 20. Using Visual SourceSafe
- Day 21. Object Role Modeling with Visio
- Week 3. In Review
Printing a Report Without the Viewer Control
You can bypass the viewer control and print a report directly from a menu item or the click event of a button. Using the PrintToPrinter method of the ReportDocument class, you can print a loaded report directly to a printer. Listing 18.2 demonstrates how you could accomplish this in the click event of a button. Notice the code comments for the parameters for the PrintToPrinter method that sets the properties on the printed document.
Example 18.2. Printing a Report with the PrintToPrinter Method
Private Sub button1_Click(sender As Object, e As System.EventArgs)
oRpt = New ReportDocument()
oRpt.Load("C:\Report1.rpt")
Try
oRpt.PrintOptions.PrinterName = "\jbnet\hp-color"
' The PrintToPrinter method parameters are:
' # of copies
' Collated
' Start Page
' End Page
oRpt.PrintToPrinter(1, True, 1, 1)
Catch err As Exception
MessageBox.Show(err.ToString())
End Try
End Sub
private void button1_Click(object sender, System.EventArgs e)
{
oRpt = new ReportDocument();
oRpt = new ReportDocument();
oRpt.Load(@"C:\Report1.rpt");
try
{
oRpt.PrintOptions.PrinterName =
"@\\jbnet\\hp-color";
// The PrintToPrinter method parameters are:
// # of copies
// Collated
// Start Page
// End Page
oRpt.PrintToPrinter(1, true, 1, 1);
}
catch(Exception err)
{
MessageBox.Show(err.ToString());
}
}
After you add this code to the click event of a button and modify the location of the printer and the report name, the file prints directly without a viewer control.
Exporting a Report Programmatically | Next Section

Account Sign In
View your cart