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 want to print data from crystal report. I have a button print, on clicking that button 1st report will show then on the crystal report print button it should get printed,but its not working, showing error.

I am attaching the code.
 protected void btnPrint_Click(object sender, EventArgs e)
    {
       ReportDocument cryRpt = new ReportDocument();
            cryRpt.Load("C:/Users/SUDESHNA/Documents/Visual Studio 2010/Projects/Inventory/ViewAllCrystalReport1.rpt");

            TableLogOnInfos crtableLogoninfos = new TableLogOnInfos();
            TableLogOnInfo crtableLogoninfo = new TableLogOnInfo();
            ConnectionInfo crConnectionInfo = new ConnectionInfo();
            Tables CrTables;

            crConnectionInfo.ServerName = "INSTANCE";
            crConnectionInfo.DatabaseName = "InventoryDatabase";
            crConnectionInfo.UserID = "sa";
            crConnectionInfo.Password = "gariahat";

            CrTables = cryRpt.Database.Tables;
            foreach (CrystalDecisions.CrystalReports.Engine.Table CrTable in CrTables)
            {
                crtableLogoninfo = CrTable.LogOnInfo;
                crtableLogoninfo.ConnectionInfo = crConnectionInfo;
                CrTable.ApplyLogOnInfo(crtableLogoninfo);
            }

            cryRpt.Refresh();
            cryRpt.PrintToPrinter(2, true, 1, 2);
    }
}


kindly please help me.

Sudeshna
Posted
Comments
/\jmot 4-Feb-15 0:45am    
what's the error??
sudeshna from bangkok 4-Feb-15 0:53am    
Failed to open the connection.
ViewAllCrystalReport1 {509200AC-ED44-49EC-8695-FEF4E5D50013}.rpt
Details: [Database Vendor Code: 17 ]

error coming in last line cryRpt.PrintToPrinter
sudeshna from bangkok 4-Feb-15 0:57am    
Failed to open the connection.
ViewAllCrystalReport1 {509200AC-ED44-49EC-8695-FEF4E5D50013}.rpt
Details: [Database Vendor Code: 17 ]
error coming at the last line.
cryRpt.PrintToPrinter(2,true,1,2)
sudeshna from bangkok 4-Feb-15 1:11am    
hello.Can anyone please help me?
Sinisa Hajnal 4-Feb-15 3:05am    
Patience, its only been two hours since you posted the question.
Can you print with different parameters in PrintToPrinter? Is the printer set on the report? Can you print on the printer from word or notepad with the same user as the report process uses (maybe the printer is not visible)? Try setting that printer as default too.

1 solution

Try the following code instead:
C#
public static void SetCrystalConnection(ReportDocument doc)
{
      doc.SetDatabaseLogon("UserName", "Password");
}


If you have subreports with a different connection try:
C#
public static void SetCrystalConnection(ReportDocument doc)
{
    var defaultDbName = "TheDefaultDatabaseName";
    doc.SetDatabaseLogon("UserName", "Password");
    if (!doc.IsSubreport)
        for (int i = 0; i < doc.Subreports.Count.GetIntValue(); i++)
            if (defaultDbName != doc.Subreports[i].DataSourceConnections[0].DatabaseName)
                SetCrystalConnection(doc.Subreports[i]);
}
 
Share this answer
 
Comments
sudeshna from bangkok 4-Feb-15 5:08am    
this code where to add? the code which i wrote shall i erase it?
InbarBarkai 4-Feb-15 5:10am    
Instead of it all.
Load the document (like you did).
Then set the connection.
Print
sudeshna from bangkok 2-Mar-15 23:07pm    
Hi can you please tell me how to display the print wizard(set up) dialog box at run time after clicking on print button. the code which you had told me its printing all the pages directly after clicking on print button. But i want to mention specific pages in the print set up page or printer name after clicking on print button
InbarBarkai 2-Mar-15 23:34pm    
If you're not displaying the report through a viewer, the only option you have is to display a PrintDialog and use cr.PrintOptions.CopyFrom to copy the options to the crystal reports.

This solution, altough it probably works, is very buggy and might not accept the new options.
Crystal Reports printing is very annoying and the only way it prints perfectly is through a viewer, when everything is handled inside the Crystal Reports API.

If all you want is to choose the pages you want to print, show a dialog of your own, asking for the pages.
Then print them using the print method.

Best of luck
sudeshna from bangkok 2-Mar-15 23:40pm    
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);
}

This is the code that you had told. and I was using it for so many days

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