Cluskitt
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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...
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
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.
-----------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.CreateDiskFi leDestinat ionOptions
diskOptions.DiskFileName = filePath
Select Case filter
Case Is = 1 'Rich Text
expOptions.ExportFormatTyp e = ExportFormatType.RichText
Case Is = 2 'Excel
expOptions.ExportFormatTyp e = ExportFormatType.Excel
Case Is = 3 'Excel-Data Only
expOptions.ExportFormatTyp e = ExportFormatType.ExcelReco rd
Case Is = 4 'PDF
expOptions.ExportFormatTyp e = ExportFormatType.PortableD ocFormat
Case Is = 5 'Word
expOptions.ExportFormatTyp e = ExportFormatType.WordForWi ndows
Case Else
expOptions.ExportFormatTyp e = ExportFormatType.NoFormat
End Select
expOptions.ExportDestinati onType = ExportDestinationType.Disk File
expOptions.ExportDestinati onOptions = diskOptions
Return expOptions
End Function 'ConfigureExportOptions
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
-----------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.CreateDiskFi
diskOptions.DiskFileName = filePath
Select Case filter
Case Is = 1 'Rich Text
expOptions.ExportFormatTyp
Case Is = 2 'Excel
expOptions.ExportFormatTyp
Case Is = 3 'Excel-Data Only
expOptions.ExportFormatTyp
Case Is = 4 'PDF
expOptions.ExportFormatTyp
Case Is = 5 'Word
expOptions.ExportFormatTyp
Case Else
expOptions.ExportFormatTyp
End Select
expOptions.ExportDestinati
expOptions.ExportDestinati
Return expOptions
End Function 'ConfigureExportOptions
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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.
ASKER
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.
ASKER
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.