none
Exporting reports from vb.net

    Question

  • I am using visual studio 2008 and have a report that I want to have automatically saved as a pdf file without displaying the report on the screen. Below is the code as it exists now that opens the report for viewing (which I want to bypass).

    Thanks.

           Try
     
            Me.Qry_PatDsgSurvey_ResponseSummaryTableAdapter.FillPatDsg_Response(Me.PatientDsgSurveyRptSet.Qry_PatDsgSurvey_ResponseSummary, FacNumRpt, StartDate1, EndDate1)
            Catch ex As System.Exception
                System.Windows.Forms.MessageBox.Show(ex.Message)
            End Try


            NoDataFlag = "N"


            Dim RptParam_FacName(0) As ReportParameter
            RptParam_FacName(0) = New ReportParameter("RptParam_FacName")
            RptParam_FacName(0).Values.Add(FacName)

            Dim RptParam_StartDate(0) As ReportParameter
            Dim RptParam_EndDate(0) As ReportParameter
            'Setting Name to Report Parameter
            ' Parameter Name defined here should be exactly similar to name defined in Report
            RptParam_StartDate(0) = New ReportParameter("RptParam_StartDate")
            RptParam_EndDate(0) = New ReportParameter("RptParam_EndDate")
            'Assigning Values to Parameter
            RptParam_StartDate(0).Values.Add(StartDate1)
            RptParam_EndDate(0).Values.Add(EndDate1)

     

            'Finally Add The Parameter To Report Viewer
            ReportViewer1.LocalReport.SetParameters(RptParam_StartDate)
            ReportViewer1.LocalReport.SetParameters(RptParam_EndDate)
            ReportViewer1.LocalReport.SetParameters(RptParam_FacName)
     
            Me.ReportViewer1.LocalReport.EnableExternalImages = True

            Me.ReportViewer1.RefreshReport()

    Tuesday, October 04, 2011 7:21 PM

Answers

  • I corrected the issue by moving the export code to the same routine wherethe parameters are set and the report is opened. It worked once I did that.

    Thanks for your help.

    • Marked as answer by J-Bal Wednesday, October 12, 2011 6:48 PM
    Wednesday, October 12, 2011 6:48 PM

All replies

  • I have almost got it working with the code below. But when it gets to this line:

    bytes = viewer.LocalReport.Render("PDF", Nothing, mimeType, encoding, extension, streamids, warnings)

    I get the error message 'The source of the report definition has not been specified'. What am I missing?

     

            Dim ReportViewer1 As New LocalReport

            FacNumRpt = FacNum
            StartDate1 = StartDate.Text
            EndDate1 = EndDate.Text

            ReportViewer1.ReportEmbeddedResource = "PatientDsgSurvey.rptDsgSurveyReponses_Fac.rdlc"

            Try
                Me.Qry_PatDsgSurvey_ResponseSummaryTableAdapter.FillPatDsg_Responses(Me.PatientDsgSurveyRptSet.Qry_PatDsgSurvey_ResponseSummary, FacNumRpt, StartDate1, EndDate1)
            Catch ex As System.Exception
                System.Windows.Forms.MessageBox.Show(ex.Message)
            End Try

            Try
                Me.Qry_tblOptimumPatientDsgSurvey_TotDsgTableAdapter.FillTotDsg(Me.PatientDsgSurveyRptSet.Qry_tblOptimumPatientDsgSurvey_TotDsg, FacNumRpt, StartDate1, EndDate1)
            Catch ex As System.Exception
                System.Windows.Forms.MessageBox.Show(ex.Message)
            End Try

            NoDataFlag = "N"

            If IsDBNull(TextBox_TotDsg.Text) Or TextBox_TotDsg.Text = "" Then
                TotDsg = "0"
            Else
                TotDsg = TextBox_TotDsg.Text
            End If
     

            Dim RptParam_FacName(0) As ReportParameter
            RptParam_FacName(0) = New ReportParameter("RptParam_FacName")
            RptParam_FacName(0).Values.Add(FacName)

            Dim RptParam_StartDate(0) As ReportParameter
            Dim RptParam_EndDate(0) As ReportParameter
            'Setting Name to Report Parameter
            ' Parameter Name defined here should be exactly similar to name defined in Report
            RptParam_StartDate(0) = New ReportParameter("RptParam_StartDate")
            RptParam_EndDate(0) = New ReportParameter("RptParam_EndDate")
            'Assigning Values to Parameter
            RptParam_StartDate(0).Values.Add(StartDate1)
            RptParam_EndDate(0).Values.Add(EndDate1)

            Dim RptParam_NumQuestions(0) As ReportParameter
            RptParam_NumQuestions(0) = New ReportParameter("RptParam_NumQuestions")
            RptParam_NumQuestions(0).Values.Add(NumQuestions)

            Dim RptParam_TotDsg(0) As ReportParameter
            RptParam_TotDsg(0) = New ReportParameter("RptParam_TotDsg")
            RptParam_TotDsg(0).Values.Add(TotDsg)

            'Finally Add The Parameter To Report Viewer
            ReportViewer1.SetParameters(RptParam_StartDate)
            ReportViewer1.SetParameters(RptParam_EndDate)
            ReportViewer1.SetParameters(RptParam_FacName)
            ReportViewer1.SetParameters(RptParam_NumQuestions)
            ReportViewer1.SetParameters(RptParam_TotDsg)

            ExportToPDF_new()


        End Sub

        Private Sub ExportToPDF_new()

            Dim warnings As Warning() = Nothing
            Dim streamids As String() = Nothing
            Dim mimeType As String = Nothing
            Dim encoding As String = Nothing
            Dim extension As String = Nothing
            Dim bytes As Byte()
            Dim viewer As New ReportViewer
            ' Dim pdfFile As String = "\\mfa-file1\proddbs\AdmissionPacket\CorpTotalsRpt.pdf"
            Dim pdfFile As String = "\\mfa-file1\isswfiles\test\testrpt.pdf"
            File.Delete(pdfFile)
            'Then create new pdf file
            bytes = viewer.LocalReport.Render("PDF", Nothing, mimeType, encoding, extension, streamids, warnings)
            'Dim fs As New FileStream(FolderLocation & "CorpTotalsRpt.PDF", FileMode.Create)
            Dim fs As New FileStream(pdfFile, FileMode.Create)
            fs.Write(bytes, 0, bytes.Length)
            fs.Close()
        End Sub

    Thursday, October 06, 2011 8:34 PM
  • Your ExportToPDF_new method is using a differant instance of the ReportViewer then the one defined in the calling method, and this instance has none of the properties set such as the ReportEmbeddedResource. If you pass in the ReportViewer1 instance to your Export method it should work.

    Matt M


    This posting is provided "AS IS" with no warranties

    Friday, October 07, 2011 11:17 PM
  • Thanks Matt!

    I am still missing something as I am getting this error: 

    'Public member 'LocalReport' on type 'LocalReport' not found.'

    I updated the code as follows (beginning with the call to the export routine):

    ExportToPDF_new(ReportViewer1)

    Private Sub ExportToPDF_new(ByVal ReportViewer1)

            Dim warnings As Warning() = Nothing
            Dim streamids As String() = Nothing
            Dim mimeType As String = Nothing
            Dim encoding As String = Nothing
            Dim extension As String = Nothing
            Dim bytes As Byte()
            Dim viewer As New ReportViewer
            Dim pdfFile As String = "\\mfa-file1\isswfiles\test\testrpt.pdf"
            File.Delete(pdfFile)
            'Then create new pdf file
     
            bytes = ReportViewer1.LocalReport.Render("PDF", Nothing, mimeType, encoding, extension, streamids, warnings)
     
            Dim fs As New FileStream(pdfFile, FileMode.Create)
            fs.Write(bytes, 0, bytes.Length)
            fs.Close()
        End Sub

    • Marked as answer by J-Bal Wednesday, October 12, 2011 6:48 PM
    • Unmarked as answer by J-Bal Wednesday, October 12, 2011 6:48 PM
    Monday, October 10, 2011 5:27 PM
  • I corrected the issue by moving the export code to the same routine wherethe parameters are set and the report is opened. It worked once I did that.

    Thanks for your help.

    • Marked as answer by J-Bal Wednesday, October 12, 2011 6:48 PM
    Wednesday, October 12, 2011 6:48 PM