1

I have the following code which lets me append a gridview and detailview as PDF file But what I hope to achieve is not to download it but to attach it as email to send to the designated user. Is there any way I can modify the code so that I can send it as email and instead of download.

   Dim strQuery As String = "SELECT * FROM [PointOrder] WHERE PointOrderId =" & Session("PointOrder")
    Dim cmd As New SqlCommand(strQuery)
    Dim dt As DataTable = GetData(cmd)

    Dim DetailsView1 As New DetailsView()
    DetailsView1.AllowPaging = False
    DetailsView1.DataSource = dt
    DetailsView1.DataBind()

    Dim strQuery2 As String = "SELECT PointsOrderDetail.RedeemId, Redeem.RedeemName, Redeem.RedeemPoints, PointsOrderDetail.RedeemOrderQuantity, Redeem.RedeemPoints * PointsOrderDetail.RedeemOrderQuantity AS Total_Points FROM PointsOrderDetail INNER JOIN Redeem ON PointsOrderDetail.RedeemId = Redeem.RedeemId WHERE PointsOrderDetail.PointOrderId = " & Session("PointOrder")
    Dim cmd2 As New SqlCommand(strQuery2)
    Dim dt2 As DataTable = GetData(cmd2)

    Dim GridView1 As New GridView()
    GridView1.AllowPaging = False
    GridView1.DataSource = dt2
    GridView1.DataBind()


    Response.ContentType = "application/pdf"
    Response.AddHeader("content-disposition", "attachment;filename=" & Session("ID") & "-" & Session("PointOrder") & "-RedeemConfirm.pdf")
    Response.Cache.SetCacheability(HttpCacheability.NoCache)
    Dim sw As New StringWriter()
    Dim hw As New HtmlTextWriter(sw)
    DetailsView1.RenderControl(hw)
    GridView1.RenderControl(hw)
    Dim sr As New StringReader(sw.ToString())
    Dim pdfDoc As New Document(PageSize.A4, 10.0F, 10.0F, 10.0F, 0.0F)
    Dim htmlparser As New HTMLWorker(pdfDoc)
    PdfWriter.GetInstance(pdfDoc, Response.OutputStream)
    pdfDoc.Open()
    htmlparser.Parse(sr)
    pdfDoc.Close()
    Response.Write(pdfDoc)
    Response.End()

Private Function GetData(ByVal cmd As SqlCommand) As DataTable
    Dim dt As New DataTable()
    Dim strConnString As [String] = System.Configuration.ConfigurationManager.ConnectionStrings("LegacySGConnectionString").ConnectionString()
    Dim con As New SqlConnection(strConnString)
    Dim sda As New SqlDataAdapter()
    cmd.CommandType = CommandType.Text
    cmd.Connection = con
    Try
        con.Open()
        sda.SelectCommand = cmd
        sda.Fill(dt)
        Return dt
    Catch ex As Exception
        Throw ex
    Finally
        con.Close()
        sda.Dispose()
        con.Dispose()
    End Try
End Function
Andrea
  • 11,801
  • 17
  • 65
  • 72
Guo Xiuhao
  • 25
  • 4
  • 11
  • Instead of using the response output stream you can use a file our memory stream to store the PDF in a file or byte array. – mkl Jan 03 '13 at 06:52
  • I have researched something on the memory stream and it seems that it will store the pdf in the file system, is that so? I am not too sure – Guo Xiuhao Jan 03 '13 at 07:55
  • If you instantiate the `PdfWriter` with a memory stream, the PDF will be created in memory as a byte[]. If you instantiate it with a file stream, the PDF will be created in the file system as a file. Obviously, if you have a byte[], you can in turn store it as file, too. And if you have a file, you obviously can read it into memory. Now you merely have to look for a way to send a generic file or byte array as mail attachment. See also [iTextSharp - Sending in-memory pdf in an email attachment](http://stackoverflow.com/questions/1196059/itextsharp-sending-in-memory-pdf-in-an-email-attachment). – mkl Jan 03 '13 at 08:40

0 Answers0