I am trying to render a PDF from a reportviewer on a button click. I have found the following code, but it only works when is placed in the Page_Load event. I can't seem to make it work for the Button_Click event. I receive an error. I'm doing this as a "Work Around" for the lack of a print button on the ReportViewer Control when in local mode. Is there a better way than this? Thanks.
Code for Button_Click Event:
Dim AuditInv As New DVDWarehouse
ReportViewer1.ProcessingMode = ProcessingMode.Local
Dim rep As LocalReport = ReportViewer1.LocalReport
rep.ReportPath = "InventoryAudit.rdlc"
Dim dt As DataTable = AuditInv.GetDataTable("Select Inventory.BoxID, Inventory.SKU, DVDInfo.Title, Inventory.ItemCount from Inventory, DVDInfo where DVDInfo.SKU = Inventory.SKU ORDER by BoxID", "Inventory")
Dim dsAudit As New ReportDataSource
dsAudit.Name = "InventoryAudit_InventoryAudit"
dsAudit.Value = dt
rep.DataSources.Clear()
rep.DataSources.Add(dsAudit)
Dim warnings As Microsoft.Reporting.WebForms.Warning() = Nothing
Dim streamids As String() = Nothing
Dim mimeType As String = Nothing
Dim encoding As String = Nothing
Dim extension As String = Nothing
Dim DeviceInfo As String = "<DeviceInfo>" _
& " <OutputFormat>PDF</OutputFormat>" _
& " <PageWidth>8.5in</PageWidth>" _
& " <PageHeight>11.5in</PageHeight>" _
& " <MarginTop>0.6in</MarginTop>" _
& " <MarginLeft>0.6in</MarginLeft>" _
& " <MarginRight>0.4in</MarginRight>" _
& " <MarginBottom>0.4in</MarginBottom>" _
& "</DeviceInfo>"
Dim bytes As Byte()
bytes = ReportViewer1.LocalReport.Render("PDF", DeviceInfo, mimeType, encoding, extension, streamids, warnings)
Response.Clear()
Response.ContentType = mimeType
Response.AddHeader("content-disposition", "attachment; filename=Test." & extension)
Response.BinaryWrite(bytes)
Response.End()
Error Message when i click the button:
Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed. Common causes for this error are when the response is modified by calls to Response.Write(), response filters, HttpModules or server trace is enabled.
Details: Error parsing near '%PDF-1.3%
1 0 obj'.
Thanks