Link to home
Start Free TrialLog in
Avatar of Cluskitt
CluskittFlag for Portugal

asked on

Crystal Reports with ASP.Net tutorial

I am trying to create a static report in my ASP.Net website. The purpose of this report is to receive data from a page's controls and then convert to PDF and present it to the user for printing/saving.
I have never before worked with crystal reports and I'm having some difficulty achieving this. I've browsed the web for some time but pretty much all I find is connecting a report to a database, presenting the report to the user as is, etc.

What I need is to see some example that will get me started, that creates a simple report, creates a simple page, uses the report to present a pdf file (the user should never see the report part of the file. He clicks print, and a pop-up appears with the pdf). My main difficulties so far are on the handling part of the report, that is, converting to pdf and opening it in a transparent way.
SOLUTION
Avatar of rgn2121
rgn2121
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Cluskitt

ASKER

I've looked into that, but I don't think there's an example there that can help me. All conversions are saved into the local disk, which I don't want to, and all visualizations are done with a viewer, which I don't think I want either.
What I need is to create the report at runtime, loading the parameters from the web page controls, convert it into pdf and display it on the user's computer. I have seen examples on how to load parameters into a report, but only using a viewer as well.
Seeing as you've done similar things, could you maybe post some sample code? Just the code for a button on a page that has a textbox. It should send the parameter to the report, convert it to pdf on the fly and redirect there. Most likely, I can pick it up from there to adapt to my needs.
If I remember correctly, that document has code like what you need for the parameters.  I don't use those in my reports, I build a formula at runtime that filters the data down to what has been requested.  I have attached the code though were I populate a textbox on the report and I believe the code in the walk-through is vary similar for dealing with parameters.

I will have to look at a different project to see about the exporting directly to PDF, because the one that I have a lot of reports in uses the crystalReportViewer control...

    ''' <summary>
    ''' Re-usable method to set the Text value of a ITextObject of a Crystal report.
    ''' </summary>
    ''' <param name="strTextObject"></param>
    ''' <param name="strTextValue"></param>
    ''' <remarks></remarks>
    Private Sub ApplyTextObject(ByVal strTextObject As String, _
                                    ByVal strTextValue As String)
        Try
            CType(ReportDocument.ReportDefinition.ReportObjects(strTextObject), _
            CrystalDecisions.CrystalReports.Engine.TextObject).Text = strTextValue
        Catch ex As Exception
            MessageBox.Show("ApplyTextObject" & Environment.NewLine & "There is no >>" & _
            strTextObject & "<< object in this report!")
        End Try

    End Sub 'Apply_Text_Object

Open in new window

Okay...I don't have access to the code anymore that I programmitically exported to a pdf, but I have code for it that should at least give you enough to go on to make changes and get it working.

Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared

Dim ReportDocument as New ReportDocument
ReportDocument.Load("myReport.rpt")

'Pass the dataset to the report
ReportDocument.SetDataSource(RptData)

'Set the values of the text objects...
ApplyTextObject("MyTextBox", "Test")

'Export
ReportDocument.Export(ConfigureExportOptions(4, "Test.pdf"))

'Pass the Report Document object to the viewer
CrystalReportViewer1.ReportSource = ReportDocument

Open in new window


-----------Export Function-------------
    ''' <summary>
    ''' Configures the report for exporting...
    ''' </summary>
    ''' <param name="filter">The filter chosen by the user.</param>
    ''' <param name="filePath">The user specified path where the report will be saved.</param>
    ''' <returns>The export options used to finish the export.</returns>
    ''' <remarks></remarks>
    Private Function ConfigureExportOptions( _
                                    ByVal filter As Integer, _
                                    ByVal filePath As String) As ExportOptions

        Dim expOptions As New ExportOptions
        Dim diskOptions As DiskFileDestinationOptions
        diskOptions = ExportOptions.CreateDiskFileDestinationOptions
        diskOptions.DiskFileName = filePath

        Select Case filter

            Case Is = 1     'Rich Text

                expOptions.ExportFormatType = ExportFormatType.RichText

            Case Is = 2     'Excel
                expOptions.ExportFormatType = ExportFormatType.Excel

            Case Is = 3     'Excel-Data Only
                expOptions.ExportFormatType = ExportFormatType.ExcelRecord

            Case Is = 4     'PDF
                expOptions.ExportFormatType = ExportFormatType.PortableDocFormat

            Case Is = 5     'Word
                expOptions.ExportFormatType = ExportFormatType.WordForWindows

            Case Else
                expOptions.ExportFormatType = ExportFormatType.NoFormat
        End Select

        expOptions.ExportDestinationType = ExportDestinationType.DiskFile
        expOptions.ExportDestinationOptions = diskOptions

        Return expOptions

    End Function 'ConfigureExportOptions
ASKER CERTIFIED SOLUTION
Avatar of Mike McCracken
Mike McCracken

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ok, it seems it can never be completely on the fly. We are going to create the PDF, save it on file and display it. It just leads to some more file handling, which isn't a big deal. It isn't ideal, but it will do.
Now I just need a way to pass the textbox into a report without creating a dataset. I've seen a few promising pieces of code that might do the trick. Will have to fiddle around a bit to see how it goes.
Thanks for all the help so far.
This did the trick. I only had to use the PDF conversion one. I applied the SetParameterValue dynamically and it's working like a charm. I just have to adjust the crystal fields and formulas, but otherwise it seems to have me unstuck.<br /><br />I have also assigned some points rgn2121. The pdf has some very interesting code I can use.<br /><br />Thanks to both.