VB.NET Winforms render report as pdf giving "Error occurred during local report processing"

1.9k views Asked by At

Hello I am just trying to get my head round reporting in VB.NET.

The report view preview is working fine so it doesn't appear to be a problem with the report itself but the render as pdf is giving "Error occurred during local report processing", inner exception is giving "Error occurred during report processing"

I'm obviously missing something but can't see what. Be grateful if anybody could help shed any light on this please.

Private Sub ExportToPDFToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExportToPDFToolStripMenuItem1.Click
        Try
            Dim viewer As New Microsoft.Reporting.WinForms.ReportViewer
            Dim report = New Microsoft.Reporting.WinForms.LocalReport
            viewer.LocalReport.ReportEmbeddedResource = "ECUWMS.10day.rdlc"
            viewer.ProcessingMode = ProcessingMode.Local


            Dim rpJob As New ReportParameter
            rpJob.Name = "JobNo"
            rpJob.Values.Add("0000030")
            viewer.LocalReport.SetParameters(New ReportParameter() {rpJob})
            viewer.RefreshReport()

            Dim reportinfo As Byte() = viewer.LocalReport.Render("PDF") ' --> Errors here.

            Dim SFD As New SaveFileDialog
            SFD.Filter = "PDF Files(*.PDF) | *.pdf"
            If SFD.ShowDialog() = Windows.Forms.DialogResult.OK Then
                Dim newfile As New FileStream(SFD.FileName, FileMode.Create, FileAccess.Write)
                newfile.Write(reportinfo, 0, reportinfo.Length)
                newfile.Close()

            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK)

        End Try

    End Sub
2

There are 2 answers

0
tezzo On

As 'suggested' in the ex.InnerException.InnerException.Message you need to provide a ReportDataSource named dsJobs to your LocalReport.

0
Graham On

Thanks for help. I think this is fairly close to finished, I also like the idea of being able to move the filtering to the dataset rather than the report. The sandbox thing appears to be a gotcha. Without clearing this VS throws an exception. This logic also works for Excel and Word by just changing the sfd and outputs.

Here is some code if it helps somebody

Private Sub ExportToPDFToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExportToPDFToolStripMenuItem1.Click
    Dim viewer As New Microsoft.Reporting.WinForms.ReportViewer
    Dim report = New Microsoft.Reporting.WinForms.LocalReport
    Try
        Me.Tblsop_hTableAdapter.Fill(Me.SqlecuDataSet.tblsop_h)

        viewer.LocalReport.ReportEmbeddedResource = "ECUWMS.10day.rdlc"
        viewer.ProcessingMode = ProcessingMode.Local
        viewer.LocalReport.Refresh()

        Dim dsJobs As New sqlecuDataSet
        Dim ta As sqlecuDataSetTableAdapters.tblsop_hTableAdapter = Tblsop_hTableAdapter
        ta.Fill(dsJobs.tblsop_h)
        Dim repData As New ReportDataSource
        repData.Name = "dsjobs"
        repData.Value = dsJobs.tblsop_h
        viewer.LocalReport.DataSources.Clear()
        viewer.LocalReport.DataSources.Add(repData)

        Dim rpJob As New ReportParameter
        rpJob.Name = "JobNo"
        rpJob.Values.Add("0000030")
        viewer.LocalReport.SetParameters(New ReportParameter() {rpJob})
        viewer.RefreshReport()
        Dim reportinfo As Byte() = viewer.LocalReport.Render("PDF")

        Dim SFD As New SaveFileDialog
        SFD.Filter = "PDF Files(*.PDF) | *.pdf"
        If SFD.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Dim newfile As New FileStream(SFD.FileName, FileMode.Create, FileAccess.Write)
            newfile.Write(reportinfo, 0, reportinfo.Length)
            newfile.Close()

        End If
    Catch ex As Exception
        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK)
        MessageBox.Show(ex.InnerException.Message, "Error", MessageBoxButtons.OK)
        MessageBox.Show(ex.InnerException.InnerException.Message, "Error", MessageBoxButtons.OK)


    End Try
    Viewer.LocalReport.ReleaseSandboxAppDomain()

End Sub