Results 1 to 3 of 3

Thread: Crystal Reports .ReportSource

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2003
    Posts
    89

    Question Crystal Reports .ReportSource

    Hello,

    Is there a way to have one CrystalReportViewer, that can be reused over and over with different reports?

    To accomplish this "reuseability" can we pass a new reportsource at run-time depending on the user's selections?


    like ("C:\My Documents\crReport.rpt")


    Thanks for your suggestions in advance.


    Chris

  2. #2
    Addicted Member MasterBlaster's Avatar
    Join Date
    Jul 2002
    Location
    Seattle
    Posts
    196
    If you are using crystal reports that ships with visual studio.net then no, not the way you are thinking of doing it. The reports work the same way classes do. you will have to create an instance of your report object and set it as the report viewers source.

    This should help
    "And most of the evils of society can, in fact, be cured through information. We have a society that has been disinformed and based on the disinformation has made irrational choices. And that's what I mean by 'ignorance.' People, who ordinarily might be smart, are deprived of the data by which to make a rational decision, don't have the data to do it."
    Frank Zappa

  3. #3
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Yes, you can. Here is what a co-worker of mine wrote for a function. It is still not completely debugged, but he said it worked pretty well. It loads a report and uses a dataset as the report data source.

    It doesn't show you how to assign it to the viewer (because he prints it, and no one looks at it), but here is the code to do that:
    VB Code:
    1. CrystalReportViewer1.ReportSource = myReport
    Crystal reports is pretty good, once you learn how. It is the learning process with Crystal that is painful. The integration with .Net is seriously lacking IMO, and the error messages it throws are just ridiculously short on detail.
    VB Code:
    1. Private Function PrintCertificate(ByVal lngCertificateID As Long) As Boolean
    2.  
    3.     Dim myReport As New ReportDocument
    4.     Dim myDS As New Data.DataSet
    5.     Dim myDataSet As New dsCertificate
    6.     Dim myDataTable As Data.DataTable
    7.     Dim params(0) As SqlClient.SqlParameter
    8.     Dim strReportPath As String
    9.     Dim margins As PageMargins
    10.  
    11.     Try
    12.         params(0) = New SqlClient.SqlParameter("@CertificateID", lngCertificateID)
    13.  
    14.       'First update the Date Printed field of the certificate
    15.  
    16.       SqlHelper.ExecuteScalar(General.ConnectionString, CommandType.StoredProcedure, "op_Certficate_Print", params)
    17.  
    18.       myDataTable = SqlHelper.ExecuteDataset(General.ConnectionString, CommandType.StoredProcedure, "op_Certificate_Info", params).Tables(0).Copy
    19.  
    20.       myDataSet.Tables.Add(myDataTable)
    21.  
    22.       If Not General.ReportPath.Substring(General.ReportPath.Length - 1) = "\" Then
    23.         strReportPath = General.ReportPath.ToString & "\"
    24.       Else
    25.         strReportPath = General.ReportPath.ToString
    26.       End If
    27.  
    28.       If General.CertificateReportName Is Nothing Then
    29.         MessageBox.Show("You must set the certificate report to print.", "Report Not Selected", MessageBoxButtons.OK, MessageBoxIcon.Hand)
    30.  
    31.         Dim frmPrinterOptions As New frmSetDefaultPrinter
    32.         frmPrinterOptions.ShowDialog()
    33.       Else
    34.  
    35.         If General.CertificateReportName.Substring(General.CertificateReportName.Length - 4) = ".rpt" Then
    36.           strReportPath = strReportPath & General.CertificateReportName
    37.         Else
    38.           strReportPath = strReportPath & General.CertificateReportName & ".rpt"
    39.         End If
    40.  
    41.         If Not IO.File.Exists(strReportPath) Then
    42.           MessageBox.Show("The file " & strReportPath & " does not exist.  Please contact technical support.", "File Does Not Exist", MessageBoxButtons.OK, MessageBoxIcon.Error)
    43.           Exit Function
    44.         End If
    45.  
    46.         myReport.Load(strReportPath)
    47.  
    48.         myReport.SetDataSource(myDataSet.Tables(0))
    49.  
    50.         myReport.PrintOptions.PrinterName = General.CertPrinter
    51.  
    52.         ' Get the PageMargins structure and set the
    53.         ' margins for the report.
    54.  
    55.         margins = myReport.PrintOptions.PageMargins
    56.         margins.topMargin = General.CertificateTopMargin
    57.         margins.bottomMargin = General.CertificateBottomMargin
    58.         margins.leftMargin = General.CertificateLeftMargin
    59.         margins.rightMargin = General.CertificateRightMargin
    60.  
    61.         ' Apply the page margins.
    62.         myReport.PrintOptions.ApplyPageMargins(margins)
    63.  
    64.         If General.CertificateLandscape = True Then
    65.           myReport.PrintOptions.PaperOrientation = PaperOrientation.Landscape
    66.         Else
    67.           myReport.PrintOptions.PaperOrientation = PaperOrientation.Portrait
    68.         End If
    69.  
    70.         myReport.PrintToPrinter(1, True, 1, 1)
    71.       End If
    72.  
    73.       Return True
    74.     Catch ex As Exception
    75.       MessageBox.Show("btnPrint_Click - " & ex.Message & vbCr & ex.GetType.ToString)
    76.     End Try
    77.   End Function

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