joe joe - 2 months ago 10
Vb.net Question

Create and convert .rpt to .pdf without ReportViewer

I'm writing a function on a button click that allow the user to send an email to a list of customers which an attached .pdf of the invoice, which is made using Crystal Reports. I've got the email function working, I can retrieve the correct information and send an email to each customer in the list, but I'm having trouble with the attachment side of things.

In the same project, I do have a window with a ReportViewer on that I'm using to generate and view individual reports, however, due to the large amount of reports that potentially are being created and sent at once, I need to now create them without using this window.

Is it possible to generate a report and then convert it to .pdf without using a ReportViewer, and if so, how can I do this?

I thought maybe by using SQL to select all of the data that I need and store it in a DataSet, but I'm not sure what would need doing after this.

Answer

It's actually more simple than I think you're imagining it to be.

Just setup the login info for the database required, then retrieve the data from the database using SQL, and store it in a DataSet, which you set to be the reports DataSource, then export it. Obviously you may have different ways of doing things but this should roughly do it

Dim report As New ReportDocument

                Dim dest As String = ` This is where you put your file destination
                report.Load(dest)

                Dim info As CrystalDecisions.Shared.TableLogOnInfo
                info = New CrystalDecisions.Shared.TableLogOnInfo()

                info.ConnectionInfo.DatabaseName = ' Your DB name
                info.ConnectionInfo.ServerName =' Your DB server name
                info.ConnectionInfo.Password = ' Your DB password
                info.ConnectionInfo.UserID = ' Your DB UserID
                report.Database.Tables(0).ApplyLogOnInfo(info)

                sql = "SELECT * FROM your_table"
                Dim rda As New OleDb.OleDbDataAdapter(sql, connectionstring)
                Dim rds As New DataSet
                rda.Fill(rds)

                report.SetDataSource(rds)

                CrDiskFileDestinationOptions.DiskFileName = ' Your directory path and chosen file name
                CrExportOptions = report.ExportOptions

                With CrExportOptions
                    .ExportDestinationType = ExportDestinationType.DiskFile
                    .ExportFormatType = ExportFormatType.PortableDocFormat
                    .DestinationOptions = CrDiskFileDestinationOptions
                    .FormatOptions = CrFormatTypeOptions
                End With

                report.Export()