Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I have a print button in my web application. On clicking that print button, at runtime my all pages get printed from crystal report.

But I want at runtime that after clicking on print button, a print wizard dialog box shows where I can mention page numbers, or select printer or paper and then print the pages.


This is the code which I have written which currently prints all the pages without showing the print dialog box.

HTML
protected void btnPrint_Click(object sender, EventArgs e)
   {
       ReportDocument rDoc = new ReportDocument();
       rDoc.Load("C:/Users/SUDESHNA/Documents/Visual Studio 2010/Projects/Inventory/StockCrystalReport1.rpt");
       rDoc.SetParameterValue("@Category", ddlCategory.SelectedValue);
       rDoc.SetDatabaseLogon("sa", "gariahat");
       //this.crystalReportViewer.ReportSource = rDoc;
       //this.crystalReportViewer.DataBind();
       //this.crystalReportViewer.Focus();
       //rDoc.Refresh();

       rDoc.PrintToPrinter(1, true, 0, 0);
       }



Kindly please help me.

I have searched google. Found many codes but none working. I used ShowDialog() method but its showing error as namespace if i mention System.Windows.Control its not Coming.
Its showing System.Web....

So anyone please could modify the code below and tell me what to do to display PrintDialog box at runtime

Thanks in advance
Sudeshna
Posted

1 solution

I Googled "C# How to print from Crystal Reports with Print Dialog" and found this. It falls right along with the way I would try it.
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();
    }
Also, for PrintDialog to work, you must have a reference to System.Windows.Forms
 
Share this answer
 
Comments
sudeshna from bangkok 4-Mar-15 11:10am    
Hi mine is a web application. I have tried to write System.Windows.Forms, its not coming. System.Web comes.
sudeshna from bangkok 4-Mar-15 11:12am    
Its showing error if I write System.Windows.Forms. I am using Asp.net web application
HKHerron 4-Mar-15 13:20pm    
Not sure what you mean by "I have tried to write System.Windows.Forms, its not coming."

In VS, under your project references, add a reference to System.Windows.Forms. It's not coming up because you do not have a reference to this library. Which is where access to the PrintDialog is located.

Also here is another example I found out there:

try
{

using (PrintDialog printDialog = new PrintDialog())
{
ReportPrintDocument rp = new ReportPrintDocument(rvPermit.ServerReport);

Form currentForm = new Form();
currentForm.Show();
currentForm.Activate();
currentForm.TopMost = true;
currentForm.Focus();
currentForm.Visible = false;

if (printDialog.ShowDialog(currentForm) == DialogResult.OK)
{
if (PrintReport != null)
PrintReport(this, e);

rp.PrinterSettings = printDialog.PrinterSettings;
rp.Print();
}

currentForm.Close();
}
}
catch (Exception)
{
// Prevent any error while calling the printer dialog
}
sudeshna from bangkok 5-Mar-15 0:25am    
Hi. Your code working fine. But 1 thing the dialog box is coming at the background of the application. So I was unable to recognize it. Every time I have to minimize my application and then select options from the print dialog box. Can you please tell the code for bringing it at the front of the application?
HKHerron 5-Mar-15 10:31am    
As I mentioned, these are not my code, but some examples I found for you. This also seems to be a big issue for other users too. It's not really your code, but a bug when trying to integrate with your crystal report class. Crystal reports print from the "Default" printer by design of their software. Over-riding is possible but may not be the way you intend it to be.

I would start looking at Crystal Report forums, and ask you question there and see if any other users have found a solution to this. As I mentioned, it's not really a C# question, but more of a Crystal Reports question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900