printing crystal report
-
Hello , I am trying to pring crystal report without openning a window with the crystal report itself. I succeeded to print crystal report without parameters by doing: ReportDocument report = new ReportDocument(); report.Load@"G:\Reports\Rp3Reciept.rpt"); report.PrintOptions.PrinterName = @"\\HPLaserJ5"; report.PrintOptions.PrinterDuplex = PrinterDuplex.Default; report.PrintToPrinter(1,false,1,1); and it is working good , but how can I send parameters to reportDocument. or How can I print report from CrystalReportViewer(becuase to it I can send parameters). (the printReport() method is virtual , that meens I have to write it?!) Thank you very much for any answer.:) sharon
-
Hello , I am trying to pring crystal report without openning a window with the crystal report itself. I succeeded to print crystal report without parameters by doing: ReportDocument report = new ReportDocument(); report.Load@"G:\Reports\Rp3Reciept.rpt"); report.PrintOptions.PrinterName = @"\\HPLaserJ5"; report.PrintOptions.PrinterDuplex = PrinterDuplex.Default; report.PrintToPrinter(1,false,1,1); and it is working good , but how can I send parameters to reportDocument. or How can I print report from CrystalReportViewer(becuase to it I can send parameters). (the printReport() method is virtual , that meens I have to write it?!) Thank you very much for any answer.:) sharon
You will need to use an object called ParameterFieldDefinitions. It's a collection of the parameters on your report. Here is a simple little function that you may be able to use. It takes a string for the parameter name and in integer for the parameter value. Of course, you could overload this function for other parameter types, but it demonstrates how to set a parameter value on a report. Crystal could certainly make this process easier than they do. public void SetParameterValue(string szParamName, int iValue) { ParameterFieldDefinitions paramFlds = report.DataDefinition.ParameterFields; ParameterFieldDefinition paramFld = paramFlds[szParamName]; ParameterDiscreteValue discreteValue = new ParameterDiscreteValue(); ParameterValues paramValues = paramFld.CurrentValues; paramValues.Clear(); discreteValue.Value = iValue; paramValues.Add(discreteValue); paramFld.ApplyCurrentValues(paramValues); } Ron Ward
-
You will need to use an object called ParameterFieldDefinitions. It's a collection of the parameters on your report. Here is a simple little function that you may be able to use. It takes a string for the parameter name and in integer for the parameter value. Of course, you could overload this function for other parameter types, but it demonstrates how to set a parameter value on a report. Crystal could certainly make this process easier than they do. public void SetParameterValue(string szParamName, int iValue) { ParameterFieldDefinitions paramFlds = report.DataDefinition.ParameterFields; ParameterFieldDefinition paramFld = paramFlds[szParamName]; ParameterDiscreteValue discreteValue = new ParameterDiscreteValue(); ParameterValues paramValues = paramFld.CurrentValues; paramValues.Clear(); discreteValue.Value = iValue; paramValues.Add(discreteValue); paramFld.ApplyCurrentValues(paramValues); } Ron Ward
Thank you for this , but I still don't know how can I print this report with its parameters. ok, let say i made collection of parameters, how can i connect it to the report in order to print the report without preview? maybe with ReportDocument or some other object? Thank u anyway, sharon
-
Thank you for this , but I still don't know how can I print this report with its parameters. ok, let say i made collection of parameters, how can i connect it to the report in order to print the report without preview? maybe with ReportDocument or some other object? Thank u anyway, sharon
In your original message you indicated that you were able to print to the printer; with a report that had no parameters. Your code is correct there; you assign the PrinterName property and then call the PrintToPrinter method. If there are parameters you need to pass and/or database connection information you need to set for your report to print, you simply must set those properties before you call the PrintToPrinter method. All of these methods and properties are within the ReportDocument object. So the code would be something like the following: public class Form1 : System.Windows.Forms.Form { ReportDocument m_PrintJob; public Form1() { try { m_PrintJob = new ReportDocument(); m_PrintJob.Load("C:/MyReport.rpt"); if (m_PrintJob.IsLoaded) { SetConnectionInfo("MyDBServer", "MyDBName", "MyUserName", "MyPassword"); SetParameterValue("ParameterName", 100); m_PrintJob.PrintToPrinter(1, false, 0, 0); } } catch (Exception e) { MessageBox.Show(e.Message); } } public void SetConnectionInfo(string szServer, string szDatabase, string szUser, string szPassword) { TableLogOnInfo crtableLogoninfo = new TableLogOnInfo(); Tables CrTables = m_PrintJob.Database.Tables; for (int iIndex = 0; iIndex < CrTables.Count; iIndex++) { crtableLogoninfo.ConnectionInfo.ServerName = szServer; crtableLogoninfo.ConnectionInfo.DatabaseName = szDatabase; crtableLogoninfo.ConnectionInfo.UserID = szUser; crtableLogoninfo.ConnectionInfo.Password = szPassword; Table CrTable = CrTables[iIndex]; CrTable.ApplyLogOnInfo(crtableLogoninfo); } } public void SetParameterValue(string szParamName, int iValue) { ParameterFieldDefinitions paramFlds = m_PrintJob.DataDefinition.ParameterFields; ParameterFieldDefinition paramFld = paramFlds[szParamName]; ParameterDiscreteValue discreteValue = new ParameterDiscreteValue(); ParameterValues paramValues = paramFld.CurrentValues; paramValues.Clear(); discreteValue.Value = iValue; paramValues.Add(discreteValue); paramFld.ApplyCurrentValues(paramValues); } } Ron Ward