On page load I am passing centain parameters and calling crytal report to export to pdf below is my revised code but still not working
Imports System.Threading
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Imports CrystalDecisions.Web.HtmlReportRender
Partial Class download_all_docs
Inherits System.Web.UI.Page
Private ReadOnly Obj As New ReportController()
Private ReadOnly ErrorObj As New ErrorLogsController()
Public rptDocu As New ReportDocument
Public ReadOnly DB As New ConnController()
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Try
Dim orno As String = Request.QueryString("orno")
Dim comp As String = Request.QueryString("comp")
Dim agreementParameters As New Dictionary(Of String, Object)
agreementParameters.Add("@Orno", orno)
agreementParameters.Add("@comp", comp)
GenerateAndDownloadDocument(agreementParameters, "rptPurchaseAgreement.rpt", "Agreement-" & orno)
Dim agreementAppendixParameters As New Dictionary(Of String, Object)
agreementAppendixParameters.Add("@t_orno", orno)
agreementAppendixParameters.Add("@t_comp", comp)
agreementAppendixParameters.Add("@t_type", 11)
GenerateAndDownloadDocument(agreementAppendixParameters, "rptPurchaseAgreementAppendix.rpt", "Appendix-" & orno)
Catch ex As Exception
Console.WriteLine("An error occurred: " & ex.Message)
HttpContext.Current.Response.Flush()
HttpContext.Current.Response.SuppressContent = True
End Try
End Sub
Sub GenerateAndDownloadDocument(ByVal parameters As Dictionary(Of String, Object), ByVal reportPath As String, ByVal fileName As String)
Dim memoryStream As New System.IO.MemoryStream()
Dim rptDocu As New ReportDocument()
Dim responseEnded As Boolean = False
Try
rptDocu.Load(System.Web.HttpContext.Current.Server.MapPath(reportPath))
For Each kvp As KeyValuePair(Of String, Object) In parameters
rptDocu.SetParameterValue(kvp.Key, kvp.Value)
Next
rptDocu.SetDatabaseLogon(DB.crvstr.UserID, DB.crvstr.Password, DB.crvstr.DataSource, DB.crvstr.InitialCatalog)
Dim stream As System.IO.Stream = rptDocu.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat)
If stream IsNot Nothing Then
Dim buffer(4096) As Byte
Dim bytesRead As Integer
bytesRead = stream.Read(buffer, 0, buffer.Length)
While bytesRead > 0
memoryStream.Write(buffer, 0, bytesRead)
bytesRead = stream.Read(buffer, 0, buffer.Length)
End While
End If
Finally
rptDocu.Close()
rptDocu.Dispose()
End Try
If Not responseEnded Then
HttpContext.Current.Response.Clear()
HttpContext.Current.Response.Buffer = True
HttpContext.Current.Response.ContentType = "application/pdf"
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + fileName + ".pdf")
memoryStream.Position = 0
memoryStream.WriteTo(HttpContext.Current.Response.OutputStream)
HttpContext.Current.Response.Flush()
HttpContext.Current.Response.End() ' This might cause "Thread was being aborted" error.
responseEnded = True
memoryStream.Close()
memoryStream.Dispose()
End If
End Sub
End Class