locked
Emailing Crystal Report RRS feed

  • Question

  • User-192448202 posted

    I'm a complete noob when it comes to Crystal Reports and i really need some help with a solution.  I basically want a Crystal Report in where when it has been generated.  I would want to email it to a customer.  How do i do this with a textbox in where i enter the customers email address and a button which then emails the report.  As i have said i am a novice when it comes to this so clear explanation would be muchly appreciated. 

    Also, when i say clear i need to know at which point the report is emailed, eg, is it at a button press or when he page is loaded from a previous button press.  Also need to know the coding from start to finish.  I have got as far as knowing that i have to type in 'Imports System.Net.Mail in the top part of the code..The rest is a mystery to me.  Here is my code so far.  I have a report in where when a user types in an Invoice number and presses the button an Invoice is produced.  I want another button underneath that so when the user is ready to email the Invoice to the customer, all the user has to do is Enter the email address in a text box and then press a button.  Is it as simple as that????  Also, does anything need to go into Web.config as well in order for me to send mail?

    Here is my current code:

    Imports CrystalDecisions.CrystalReports.Engine
    Imports CrystalDecisions.Shared
    Imports
    System.Net.Mail
    Partial Class Default2
        Inherits System.Web.UI.Page
        Dim cryRpt As New ReportDocument

        Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim cryRpt As New ReportDocument
            cryRpt.Load("C:\Documents and Settings\xxxxxxxxxxxxxxxxxx\My Documents\Visual Studio 2005\WebSites\Invoices\CrystalReport3.rpt")
            Dim crParameterFieldDefinitions As ParameterFieldDefinitions
            Dim crParameterFieldDefinition As ParameterFieldDefinition
            Dim crParameterValues As New ParameterValues
            Dim crParameterDiscreteValue As New ParameterDiscreteValue
            crParameterDiscreteValue.Value = TextBox1.Text
            crParameterFieldDefinitions = _
            cryRpt.DataDefinition.ParameterFields
            crParameterFieldDefinition = _
            crParameterFieldDefinitions.Item("InvoiceNo")
            crParameterValues = crParameterFieldDefinition.CurrentValues
            crParameterValues.Clear()
            crParameterValues.Add(crParameterDiscreteValue)
            crParameterFieldDefinition.ApplyCurrentValues(crParameterValues)
            CrystalReportViewer1.ReportSource = cryRpt
            CrystalReportViewer1.RefreshReport()
        End Sub
    End Class

    I have searched the forums but none of it makes sense to me.  Have also looked at David Wantu's system.net.mail website and have also looked at the new asp video, i can understand it to a certain extent but get confused on when things are executed and what the structure of the coding should be... [:S]

     Please Help.

     Regards, Jonah

    Monday, October 6, 2008 7:10 AM

Answers

  • User-192448202 posted

    Right i've managed to bodge my way through it using the code below.  Thank You

    Protected Sub btnMail_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnMail.Click

    If txtFrom.Text.Trim = "" Then

    lblResult.Text = "Please enter the FROM E-mail Address."

    Exit Sub

    ElseIf txtTo.Text.Trim = "" Then

    lblResult.Text = "Please enter the Address to send to."

    Exit Sub

    ElseIf txtSubject.Text.Trim = "" Then

    lblResult.Text = "Please enter the Subject."

    Exit Sub

    End If

    Try

    Dim msg As New System.Net.Mail.MailMessage(txtFrom.Text, txtTo.Text)

    If FileUpload1.PostedFile.FileName = "" Then

    Else

    msg.Attachments.Add(New System.Net.Mail.Attachment(FileUpload1.PostedFile.FileName))

    End If

    Dim mySmtp As New Net.Mail.SmtpClient("1xxx.xxx.xxx.xxx")

    mySmtp.Send(msg)

    lblResult.Text = "Mail Sent"

    Catch ex As Exception

    lblResult.Text = ex.Message

    End Try

    End Sub

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 8, 2008 11:13 AM

All replies

  • User-192448202 posted

    Right, i've managed to make sense of emailing an attachment. [:D] Thanks to StrongTypes for the send.net.mail tutorial...I actually had an [Idea] moment

    Now i have a question which invovles the text box and attachment of a certain file. So after running the report.  I want to email it to a specified person.  I would do this via a text box.  StrongTypes explains how to do this but im still not sure how it is done.  Also, on the button click, i want the option for the user to choose the PDF file within a directory.  Then once they have chosen it, they can then send the file.  How is this achieved?

    Cheers, Jonah 

    Monday, October 6, 2008 11:31 AM
  • User-192448202 posted

    Right i've managed to bodge my way through it using the code below.  Thank You

    Protected Sub btnMail_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnMail.Click

    If txtFrom.Text.Trim = "" Then

    lblResult.Text = "Please enter the FROM E-mail Address."

    Exit Sub

    ElseIf txtTo.Text.Trim = "" Then

    lblResult.Text = "Please enter the Address to send to."

    Exit Sub

    ElseIf txtSubject.Text.Trim = "" Then

    lblResult.Text = "Please enter the Subject."

    Exit Sub

    End If

    Try

    Dim msg As New System.Net.Mail.MailMessage(txtFrom.Text, txtTo.Text)

    If FileUpload1.PostedFile.FileName = "" Then

    Else

    msg.Attachments.Add(New System.Net.Mail.Attachment(FileUpload1.PostedFile.FileName))

    End If

    Dim mySmtp As New Net.Mail.SmtpClient("1xxx.xxx.xxx.xxx")

    mySmtp.Send(msg)

    lblResult.Text = "Mail Sent"

    Catch ex As Exception

    lblResult.Text = ex.Message

    End Try

    End Sub

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 8, 2008 11:13 AM
  • User-1848975386 posted
    how to implement it in payslip generation and sending the payslips to email id's selected through database table.kindly provide the source code in c#.
    Thursday, April 4, 2013 7:20 AM
  • User-1848975386 posted
    how to implement it in payslip generation and sending the payslips to email id's selected through database table.kindly provide the source code in c#.
    Thursday, April 4, 2013 7:20 AM