Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

kudos goes to solution. Crystal Report PrintToPrinter problem.

Status
Not open for further replies.

jcisco2

Programmer
Apr 13, 2004
102
US
I'm having a small problem with the PrintToPrinter method with my crystal reports. every time i print with it none of the resulting data is sent with the report to the printer. all that prints is the report static fields. but if i set the report to a crystal viewer it shows all the data just fine, and if i click the print icon on the viewer the data prints. below is the code.

Code:
'calling form that creates the reporting form. (reporting form has a crystal viewer set to it.)
  Private Sub DirectPrint_Activate(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DirectPrint.Activate
        Dim _frm As New Report
        _frm.loadNumber = 37280
        _frm.crType = Report.crystalType.billoflading
        _frm.Show()
        _frm.BringToFront()
    End Sub

'crystal report form handles all crystal printing.
  Private _loadNumber As String
    Private _crType As crystalType
    Private dLoginName As String = "someUser"
    Private dPassword As String = "somePassword"

 Enum crystalType
        packlist
        dropTrailer
        billoflading
    End Enum
    Public WriteOnly Property loadNumber() As String
        Set(ByVal loadNmb As String)
            _loadNumber = loadNmb
        End Set
    End Property
    Public WriteOnly Property crType() As crystalType
        Set(ByVal type As crystalType)
            _crType = type
        End Set
    End Property

 Private Sub Report_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim param1Fileds As New CrystalDecisions.Shared.ParameterFields
        Dim param1Field As New CrystalDecisions.Shared.ParameterField
        Dim param1Range As New CrystalDecisions.Shared.ParameterDiscreteValue

        'setup report

'this code works
        If _crType = crystalType.packlist Then
            Dim crReport As New packinglistNew
            crReport.SetDatabaseLogon(dLoginName, dPassword)
            param1Field.ParameterFieldName = "loadnumb" ' Parameter Name In Crystal Report
            param1Range.Value = _loadNumber           ' value For Parameter Field 
            param1Field.CurrentValues.Add(param1Range)
            param1Fileds.Add(param1Field)                   ' To add parameter in parameterslist
            CrystalReportViewer1.ParameterFieldInfo = param1Fileds 'to pass parameter 
            Me.CrystalReportViewer1.ReportSource = crReport
        End If

'all code below this works if i set the crystal object to the viewer, but it will not print the result data
        If _crType = crystalType.dropTrailer Then
            Dim crReport As New BOLDROPNew
            param1Field.ParameterFieldName = "loadnumb" ' Parameter Name In Crystal Report
            param1Range.Value = _loadNumber           ' value For Parameter Field 
            param1Field.CurrentValues.Add(param1Range)
            param1Fileds.Add(param1Field)                   ' To add parameter in parameterslist
            CrystalReportViewer1.ParameterFieldInfo = param1Fileds 'to pass parameter 
            ' Me.CrystalReportViewer1.ReportSource = crReport
            crReport.Load()
            crReport.SetDatabaseLogon(dLoginName, dPassword)
            crReport.Refresh()
            crReport.PrintToPrinter(2, True, 1, -1)
            Me.Close()
        End If

        If _crType = crystalType.billoflading Then
                       Dim crReport As New packinglistNew
            crReport.SetDatabaseLogon(dLoginName, dPassword)
            param1Field.ParameterFieldName = "loadnumb" ' Parameter Name In Crystal Report
            param1Range.Value = _loadNumber           ' value For Parameter Field 
            param1Field.CurrentValues.Add(param1Range)
            param1Fileds.Add(param1Field)                   ' To add parameter in parameterslist
            CrystalReportViewer1.ParameterFieldInfo = param1Fileds 'to pass parameter 
            crReport.PrintToPrinter(1, False, 0, 0)
            Me.CrystalReportViewer1.ReportSource = crReport
            Me.Close() 'direct close don't show report form
        End If
    End Sub

any solutions?
cheers.
 
I had trouble with the report.printtoprinter method too, and in the end I gave up on it. (CR10)

Instead I used the CrystalViewer.print method. Im sure like me you're wanting to print the report without viewing it.

Simply pass the report into the viewer form, but dont show it, and create a custom method just to perform the print with no view option.

If however you are wanting to get true control over the pages being printed, you will need to fathom out why PrinttoPrinter doesnt work, and I for one would also be interested. I was even at a loss to work out the start and end page numbers to pass to this method.





Sweep
...if it works dont mess with it
 
i was thinking along the same lines. the only problem (and maybe you have a solution to this one.) is that if i change from this:
crReport.PrintToPrinter(1, False, 0, 0)
to this:
' crReport.PrintToPrinter(1, False, 0, 0)
Me.CrystalReportViewer1.ReportSource = crReport
Me.CrystalReportViewer1.PrintReport()

I get the windows printer dialog box is there a way to suppess this box from poping up and have report print to the default printer?
 
Hmmmmm...good point

I havent been able to suppress the print dialog either, but its not such a bad thing. Maybe there is a way to do this through the PrintOptions object but Im yet to fully investigate this.



Sweep
...if it works dont mess with it
 
hmm, I use CR.Net to print external CR9 files. We have a form with a CR viewer on it, but we can print w/o displaying the form. Here's a snippet:
Code:
Dim Parameter As General.Structures.CrystalReportsParameters
cr = New CrystalDecisions.CrystalReports.Engine.ReportDocument()

'Load the external file
cr.Load(ReportPath & ReportName)

'Load Parameters
If Not Parameters Is Nothing Then
 For Each Parameter In Parameters
  Try
   If Not Parameter.ParameterName Is Nothing Then cr.SetParameterValue(Parameter.ParameterName, Parameter.ParameterValue)
  Catch
   GFC.General.LogManager.LogInfo("Specified Parameter: " & Parameter.ParameterName & " not found on report")
  End Try
 Next
End If

'Set the Source, printer, and tray
CrystalReportViewer1.ReportSource = cr
cr.PrintOptions.PrinterName = Printer
cr.PrintOptions.PaperSource = PaperSource

'Break the print jobs up into 100 page jobs. 
'We had some invoice jobs that were in the 30gb range, the net admins were...
'unhappy about us overloading the poor print server
If Print Then
 Try
  Dim iPrintBlock As Integer
  For iPrintBlock = 0 To 99
   cr.PrintToPrinter(1, False, (iPrintBlock * 100), ((iPrintBlock + 1) * 100 - 1))
  Next
 Catch exc As Exception
  If exc.Message.IndexOf("The specified start page is greater than the last page on the report.") = -1 Then
   Throw exc
  End If
 End Try
Else
 'display the form
 Me.ShowDialog(m_OwnerForm)
End If

The report path and name are passed in, along with the collection of parameters, the printer and tray name, and the boolean value Print that determines if the report will be printed or displayed.

-Rick

----------------------

[monkey] I believe in killer coding ninja monkeys.[monkey]
[banghead]
 
this also worked.

If _crType = crystalType.billoflading Then
Dim crReport As New BOLNew
crReport.SetDatabaseLogon(dLoginName, dPassword)
crReport.SetParameterValue("loadnumb", _loadNumber)
crReport.PrintToPrinter(1, False, 0, 0)
Me.Close()
End If

thanks for all the suggestions.

cheers,
john
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top