locked
How to show PrintDialog when I click Print button instead of printing directly to the printer RRS feed

  • Question

  • User-1123701243 posted

    Hi all

    I designed a crystal report and added a button to print the report contents using the following code:

        Protected Sub btnPrint_Click(sender As Object, e As EventArgs) Handles btnPrint.Click
            crystalReport.PrintToPrinter(1, True, 0, 0)
        End Sub

    this command send the report directly to the printer without showing the Print Dialog Box

    I need to show the dialog box so I can select the printer and pages to print. !!!

    Please don't provide links to similar subject because I went through many of them but with no result.

    Tuesday, April 1, 2014 4:33 PM

All replies

  • User422038134 posted

    Try the following code

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

        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();
        }
    Tuesday, April 1, 2014 5:04 PM
  • User-1185172246 posted

    Are you aware that your code runs on the server side and that it will print the report to some printer available at the server? If you expect to get that code working on the client i.e. that the user can print the displayed report to his local printer, then it won't work. Now, your code is working on your machine because it is acting like the client & the server i.e. both roles but if you publish your code to an external server, then it won't work. So the question is... do you want to get your report printed at the server to some printer installed there OR to the client printer? If it's the first one, then just create some page asking the user for num of copies and go ahead with your code. BUT if it is the second option you need, then you can export the report to PDF and then display it to the user so it can print it "manually" or try using this code which makes the job a little bit smoothly.

    Thursday, April 3, 2014 7:40 AM
  • User422038134 posted

    I hope the solution helped you. Please let us know.

    Thursday, April 3, 2014 10:43 AM