Results 1 to 5 of 5

Thread: [CR .NET 2k5 Edition] Printing Crystal Reports via code.

  1. #1

    Thread Starter
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    [CR .NET 2k5 Edition] Printing Crystal Reports via code.

    One thing that has baffled me in the past was I could not figure out how to print a Crystal Report without first sending it to the CRViewer first. Basically, I want to put a big "Print" button on my forms that would send the things straight to printer, do not pass Go, do not collect $200.

    My implementation of Crystal Reports is that I first pull a DataSet from the database with given parameters. Then I declare a new report object and load in the corresponding *.rpt file that was built using the schema.xml from that dataset. I pass the report any additional parameters it might need and send the thing to the CRViewer.

    Assuming I can create that Report document object and populate it as I have been, does anyone have a code-example of popping a printer select dialog and upon exit, sending it off to the printer of choice w/o having to go through the CRViewer?

    Any help would be most appreciated. I can provide code samples if required.

  2. #2
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: [CR .NET 2k5 Edition] Printing Crystal Reports via code.

    Here is the sub I use: (I connect to the db change were required)

    vb.net Code:
    1. Public Sub outputToPrinter(ByVal mRepName As String, ByVal mstrW As String, Optional ByVal strPars As String = "")
    2.         Dim ConInfo As New CrystalDecisions.Shared.TableLogOnInfo
    3.         Dim paraValue As New CrystalDecisions.Shared.ParameterDiscreteValue
    4.         Dim currValue As New CrystalDecisions.Shared.ParameterValues
    5.         'Sub report object of crystal report.
    6.         Dim mySubReportObject As CrystalDecisions.CrystalReports.Engine.SubreportObject
    7.         'Sub report document of crystal report.
    8.         Dim mySubRepDoc As New CrystalDecisions.CrystalReports.Engine.ReportDocument
    9.         Dim i, x, z As Integer 'Some Counters
    10.         Dim mRep As New CrystalDecisions.CrystalReports.Engine.ReportDocument
    11.  
    12.         Try
    13.             ' Load the report
    14.             'Need to add the Report dll yet nothing works
    15.             Dim rep As New RSMReports.clsReports(mRepName)
    16.             mRep = rep.returnRep()
    17.             mRep.RecordSelectionFormula = mstrW
    18.  
    19.             ConInfo.ConnectionInfo.UserID = "UserName"
    20.             ConInfo.ConnectionInfo.Password = "Password"
    21.             ConInfo.ConnectionInfo.ServerName = mdlGeneral.serverName
    22.             ConInfo.ConnectionInfo.DatabaseName = mdlGeneral.dbName
    23.  
    24.             For i = 0 To mRep.Database.Tables.Count() - 1
    25.                 mRep.Database.Tables(i).ApplyLogOnInfo(ConInfo)
    26.             Next i
    27.  
    28.             'Check for Subreprots
    29.             For i = 0 To mRep.ReportDefinition.Sections.Count - 1
    30.                 For x = 0 To mRep.ReportDefinition.Sections(i).ReportObjects.Count - 1
    31.                     With mRep.ReportDefinition.Sections(i)
    32.                         If .ReportObjects(x).Kind = CrystalDecisions.Shared.ReportObjectKind.SubreportObject Then
    33.                             mySubReportObject = CType(.ReportObjects(x), CrystalDecisions.CrystalReports.Engine.SubreportObject)
    34.                             mySubRepDoc = mySubReportObject.OpenSubreport(mySubReportObject.SubreportName)
    35.                             For z = 0 To mySubRepDoc.Database.Tables.Count - 1
    36.                                 mySubRepDoc.Database.Tables(z).ApplyLogOnInfo(ConInfo)
    37.                             Next z
    38.                         End If
    39.                     End With
    40.                 Next x
    41.             Next i
    42.             'Parameter Fields
    43.             If strPars.Trim() <> String.Empty Then
    44.                 Dim arPars As String() = strPars.Split("~")
    45.                 Dim strVal As String()
    46.                 For xx As Integer = 0 To arPars.GetUpperBound(0)
    47.                     strVal = arPars(xx).Split("=")
    48.                     paraValue.Value = strVal(1)
    49.                     currValue = mRep.DataDefinition.ParameterFields(strVal(0)).CurrentValues
    50.                     currValue.Add(paraValue)
    51.                     mRep.DataDefinition.ParameterFields(xx).ApplyCurrentValues(currValue)
    52.                 Next
    53.             End If
    54.             mRep.PrintToPrinter(1, False, 0, 0)
    55.         Catch crExp As CrystalDecisions.CrystalReports.Engine.LoadSaveReportException
    56.             MessageBox.Show("Error printing Report " & mRepName & System.Environment.NewLine & _
    57.                 "The error number is: " & Err.Number.ToString() & System.Environment.NewLine & _
    58.                 "The error Message is: " & System.Environment.NewLine & crExp.Message.Trim(), "Print Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    59.         Catch ex As Exception
    60.             MessageBox.Show("Error printing Report " & mRepName & System.Environment.NewLine & _
    61.                             "The error number is: " & Err.Number.ToString() & System.Environment.NewLine & _
    62.                             "The error Message is: " & System.Environment.NewLine & ex.Message.Trim(), "Print Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    63.         Finally
    64.             mRep.Dispose()
    65.         End Try
    66.     End Sub
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  3. #3

    Thread Starter
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: [CR .NET 2k5 Edition] Printing Crystal Reports via code.

    Hmm... unfortunately, I just tried PrintToPrinter and it didn't do anything at all. No error, no print...

    Here's my code that I tried minus some extra selection / options criteria to make it easier to see the parts that matter.

    Code:
        Private Sub CostCustomerShow()
            Dim strSQL As String = String.Empty
            Dim ds As New DataSet
    
            Dim cRpt As New CrystalDecisions.CrystalReports.Engine.ReportDocument
            Dim strRpt As String = String.Format("{0}\Reports\CpCust\{1}", Application.StartupPath, g.SELECTED_CUSTCOST_REPORT)
    
            cRpt.Load(strRpt, CrystalDecisions.Shared.OpenReportMethod.OpenReportByTempCopy)
    
            Dim mCon As MySql.Data.MySqlClient.MySqlConnection
            mCon = CType(ReturnGlobalMCon(), MySql.Data.MySqlClient.MySqlConnection)
    
    
            strSQL = String.Format("SELECT * FROM FormulaUse WHERE UseDate BETWEEN '{0:yyyy-MM-dd}' AND '{1:yyyy-MM-dd}'; ", dtpCostFrom.Value, dtpCostTo.Value)
            strSQL &= "SELECT * FROM CustomerList; "
    
            Dim da As New MySql.Data.MySqlClient.MySqlDataAdapter(strSQL, mCon)
            da.Fill(ds)
    
            cRpt.SetDataSource(ds)
            cRpt.SetParameterValue("From", dtpCostFrom.Value)
            cRpt.SetParameterValue("To", dtpCostTo.Value)
            cRpt.SetParameterValue("Units", g.UNITS & "s")
    
            cRpt.PrintToPrinter(1, False, 0, 0)
    
            'crViewer.ReportSource = cRpt
            'crViewer.Zoom(1)
    
            da.Dispose()
            mCon.Close()
        End Sub

  4. #4

    Thread Starter
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: [CR .NET 2k5 Edition] Printing Crystal Reports via code.

    Shameless bump... still unresolved.

    Anybody know this one? I refuse to accept that I can only get the stupid thing to print while being displayed in the viewer. I messed with it for about 4 hours today and got nowhere. I even tried a variation of Mazz1's routine and couldn't get it to happen.

  5. #5
    Addicted Member
    Join Date
    Jan 2001
    Location
    MPLS
    Posts
    187

    Re: [CR .NET 2k5 Edition] Printing Crystal Reports via code.

    It may be that the printer that the report was designed with is not found in your local printers on your machine. Try setting the printer to a valid printer to print to via code just before the printtoprinter command.

    'set the printer name
    Dim CrPrintOptions As PrintOptions = CRpt.PrintOptions
    CrPrintOptions.PrinterName = "My favorite printer"

    'send the report to the printer
    CRpt.PrintToPrinter(1, False, 1, -1)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width