Export Crystal Report to PDF and send in Email as attachment using VB.Net in Windows Application

Last Reply one month ago By dharmendr

Posted one month ago

I want to Export Crystal Report to PDF and send in Email as an Attachment in Windows Application using VB.Net using crystal report viewer and in the crystal report viewer on button click, I want to send an email to the respective person about the report in pdf format 

Below code of my crystal report viewer on button click from another form 

Imports TestingNewFeature.DynamicReport
Imports System.Drawing.Printing
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.CrystalReports
Imports CrystalDecisions.Enterprise
Imports CrystalDecisions.ReportSource
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports System.Data.SqlClient
Imports CrystalDecisions.Shared
Imports CrystalDecisions.Windows.Forms
Imports Microsoft.SqlServer

Public Class BillReport


    Private Sub BillReport_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ''Private Sub BillReport_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If con.State = ConnectionState.Open Then
            con.Close()
        End If

        If rpt1 = "Billing1" Then
            'Me.crv1.set_Formulas(0, "ob ='" & a + "'")
            Dim ds As New DataSet
            Dim da As New SqlClient.SqlDataAdapter("select * from BBillinfo where BillNo= " & dts, con)

            'SELECT 'Original' as grp, * FROM Billinfo b full outer join Items_Sold i on b.BillNo=i.IBill_No UNION SELECT 'Duplicate' as grp, * FROM Billinfo b full outer join Items_Sold i on b.BillNo=i.IBill_No UNION SELECT 'Triplicate' as grp, * FROM Billinfo b full outer join Items_Sold i on b.BillNo=i.IBill_No UNION SELECT 'Extra Copy' as grp, * FROM Billinfo b full outer join Items_Sold i on b.BillNo=i.IBill_No and b.BillNo= " & dts, con)
            con.Open()

            da.Fill(ds, "BBillinfo")

            Dim obj As New Billing1
            obj.Database.Tables(0).SetDataSource(ds.Tables("BBillinfo"))
            crv1.ReportSource = obj

            con.Close()

        ElseIf rpt1 = "BGLBillBck" Then
            Dim ds As New DataSet
            ''  Dim cryrpt As ReportDocument = New ReportDocument
            'Dim da As New SqlClient.SqlDataAdapter("select * from Billinfo b, Items_Sold i where b.BillNo=i.IBill_No and b.BillNo= " & dts, con)
            Dim da As New SqlClient.SqlDataAdapter("select * from Billinfo b, Items_Sold i where b.BillNo=i.IBill_No and b.BillNo='" & dts & "'" & " and Date between'" & dttp1 & "'" & "and '" & dttp2 & "'", con)
            con.Open()

            da.Fill(ds, "Billinfo, Items_Sold")

            Dim obj As New BGLBillBck
            obj.Database.Tables(0).SetDataSource(ds.Tables("Billinfo, Items_Sold"))
            crv1.ReportSource = obj
            crv1.Refresh()
            '' cryrpt.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, "D:\Report.pdf")
            '' MessageBox.Show("Exported Successfully")
            con.Close()

            

        End If

    End Sub
Private Sub SavePDF_Click(sender As Object, e As EventArgs) Handles SavePDF.Click


        Dim cryrpt As ReportDocument = New ReportDocument
        ''cryrpt.Load("BGLBillBck.rpt")
        'crv1.ReportSource = cryrpt
        'crv1.Refresh()
        cryrpt.ExportToDisk(ExportFormatType.PortableDocFormat, "E:\Report.rpt")
        '' cryrpt.ExportToDisk(ExportFormatType.PortableDocFormat, Server.MapPath("Crystal-to-PDF\EmployeeDetails.pdf"))

        MessageBox.Show("Exported Successfully")
    End Sub
End Class

Please help me with saving the crystal report in pdf format. I have the code for sending an email with the attachment.

Posted one month ago

Refer below code.

Private Sub SavePDF_Click(sender As Object, e As EventArgs) Handles SavePDF.Click
    Dim cryrpt As ReportDocument = New ReportDocument
    ''cryrpt.Load("BGLBillBck.rpt")
    'crv1.ReportSource = cryrpt
    'crv1.Refresh()
    cryrpt.ExportToDisk(ExportFormatType.PortableDocFormat, "E:\Report.pdf")
    Using mm As New MailMessage(txtEmail.Text.Trim(), txtTo.Text.Trim())
        mm.Subject = txtSubject.Text
        mm.Body = txtBody.Text
        mm.Attachments.Add(New Attachment("E:\Report.pdf"))
        mm.IsBodyHtml = False
        Dim smtp As New SmtpClient()
        smtp.Host = "smtp.gmail.com"
        smtp.EnableSsl = True
        Dim NetworkCred As New NetworkCredential(txtEmail.Text.Trim(), txtPassword.Text.Trim())
        smtp.UseDefaultCredentials = True
        smtp.Credentials = NetworkCred
        smtp.Port = 587
        smtp.Send(mm)
        MessageBox.Show("Email sent.", "Message")
    End Using
End Sub

For export refer below article.

Export Crystal Report to Word Excel PDF and CSV in Windows Application using C# and VB.Net