How to show print dialog in Crystal Report?

13,800

Try the following code

    private void Button1_Click(object sender, EventArgs e)
    {
        CrystalReport1 report1 = new CrystalReport1();
        PrintDialog dialog1 = new PrintDialog();

        report1.SetDatabaseLogon("username", "password");

        dialog1.AllowSomePages = true;
        dialog1.AllowPrintToFile = false;

        if (dialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            int copies = dialog1.PrinterSettings.Copies;
            int fromPage = dialog1.PrinterSettings.FromPage;
            int toPage = dialog1.PrinterSettings.ToPage;
            bool collate = dialog1.PrinterSettings.Collate;

            report1.PrintOptions.PrinterName = dialog1.PrinterSettings.PrinterName;
            report1.PrintToPrinter(copies, collate, fromPage, toPage);            
        }

        report1.Dispose();
        dialog1.Dispose();
    }

you will have to change the "username" and the "password" with the credentials of your database.

EDIT

This code can be used for server side printing only.

Share:
13,800

Related videos on Youtube

barsan
Author by

barsan

Updated on September 15, 2022

Comments

  • barsan
    barsan over 1 year

    I want to print my Crystal report direct to the printer. Currently I am having the export to PDF. But my client want this to go to Printer directly. How can I show the Print Dialog on click of Print Button to Print the report directly to Printer.

    I would like to mention: I am using C# and asp.net for my project.

    Thank you.

  • barsan
    barsan almost 11 years
    Hello I would like to inform when I upload this code into the server then I get this error: Showing a modal dialog box or form when the application is not running in UserInteractive mode is not a valid operation. Specify the ServiceNotification or DefaultDesktopOnly style to display a notification from a service application will you please suggest me what can I do on this now?
  • David -
    David - almost 11 years
    This code is for server side printing only. If you want a client side solution check this approach aspalliance.com/…