none
Autoprinting RRS feed

  • Question

  • Can anyone get the vb code on this page to work for automatically printing?

    http://msdn.microsoft.com/en-us/library/ms252091%28VS.80%29.aspx

    I always get the following error:

    "An error occurred during local report processing."

    It occurs on the line of code below.

    report.Render("Image", deviceInfo, AddressOf CreateStream, warnings)

    Friday, July 9, 2010 3:48 PM

Answers

  • I fixed this problem.  The problem was due to having report parameters in my report that were not being set. 
    • Marked as answer by James12314909 Monday, July 12, 2010 7:44 PM
    Monday, July 12, 2010 7:44 PM

All replies

  • Can you show us your code? The process does work, I use it all the time. Show how your load the report and create the streams and maybe we can see the error. Also, verify that you have data to give to the report.

    BTW, I was able to get the example to work, I did have to change the printer name in the Print function to the name of a printer that exists on my workstation.

     

    Friday, July 9, 2010 5:41 PM
  • Please look at the run sub.  I am setting the report to the localreport from the reportviewer where the datasource was set through  the setupParameterOnlyReport at the end.  This report runs fine if I don't try to automatically send it to the printer.

    Private Function CreateStream(ByVal name As String, _
           ByVal fileNameExtension As String, _
           ByVal encoding As Encoding, ByVal mimeType As String, _
           ByVal willSeek As Boolean) As Stream
            Dim stream As Stream = _
                New FileStream("..\..\" + _
                 name + "." + fileNameExtension, FileMode.Create)
            m_streams.Add(stream)
            Return stream
        End Function

        Private Sub Export(ByVal report As LocalReport)
            Dim deviceInfo As String = _
              "<DeviceInfo>" + _
              "  <OutputFormat>EMF</OutputFormat>" + _
              "  <PageWidth>8.5in</PageWidth>" + _
              "  <PageHeight>11in</PageHeight>" + _
              "  <MarginTop>0.25in</MarginTop>" + _
              "  <MarginLeft>0.25in</MarginLeft>" + _
              "  <MarginRight>0.25in</MarginRight>" + _
              "  <MarginBottom>0.25in</MarginBottom>" + _
              "</DeviceInfo>"
            Dim warnings() As Warning = Nothing
            m_streams = New List(Of Stream)()
            report.Render("Image", deviceInfo, AddressOf CreateStream, _
               warnings)

            Dim stream As Stream
            For Each stream In m_streams
                stream.Position = 0
            Next
        End Sub

        Private Sub PrintPage(ByVal sender As Object, _
        ByVal ev As PrintPageEventArgs)
            Dim pageImage As New Metafile(m_streams(m_currentPageIndex))
            ev.Graphics.DrawImage(pageImage, ev.PageBounds)

            m_currentPageIndex += 1
            ev.HasMorePages = (m_currentPageIndex < m_streams.Count)
        End Sub

        Private Sub Print()
            Const printerName As String = _
              "Microsoft Office Document Image Writer"

            If m_streams Is Nothing Or m_streams.Count = 0 Then
                Return
            End If

            Dim printDoc As New PrintDocument()
            printDoc.PrinterSettings.PrinterName = printerName
            If Not printDoc.PrinterSettings.IsValid Then
                Dim msg As String = String.Format( _
                    "Can't find printer ""{0}"".", printerName)
                Console.WriteLine(msg)
                Return
            End If
            AddHandler printDoc.PrintPage, AddressOf PrintPage
            printDoc.Print()
        End Sub

        Private Sub Run()
            Dim report As LocalReport = New LocalReport()
            report = ReportViewer.LocalReport
            report.DisplayName = "PrintTest"
            'report.ReportPath = "..\..\Report.rdlc"
            'report.DataSources.Add(New ReportDataSource("Sales", _
            '   LoadSalesData()))

            Export(report)

            m_currentPageIndex = 0
            Print()
        End Sub

        Public Overloads Sub Dispose()
            If Not (m_streams Is Nothing) Then
                Dim stream As Stream
                For Each stream In m_streams
                    stream.Close()
                Next
                m_streams = Nothing
            End If
        End Sub

        Public Sub automaticallyPrint()
            Run()
        End Sub

        Private Sub setupParameterOnlyReport()

            Dim rds As New ReportDataSource
            Dim ds As New DataSet
            Dim dt As DataTable
            Dim dr As DataRow
            Dim idCoulumn As DataColumn
            Dim nameCoulumn As DataColumn
            dt = New DataTable()
            idCoulumn = New DataColumn("ID", Type.GetType("System.Int32"))
            nameCoulumn = New DataColumn("Name", Type.GetType("System.String"))
            dt.Columns.Add(idCoulumn)
            dt.Columns.Add(nameCoulumn)
            dr = dt.NewRow()
            dr = dt.NewRow()
            dr("ID") = 1
            dr("Name") = "Name1"
            dt.Rows.Add(dr)
            ds.Tables.Add(dt)
            rds.Name = "TEST"
            rds.Value = ds.Tables(0)
            ReportViewer.LocalReport.ReportPath = reportPath(0)
            Me.ReportViewer.LocalReport.DataSources.Add(rds)
        End Sub

    Friday, July 9, 2010 6:24 PM
  • I went ahead and added the loadSalesData function, but changing it to the following.  I'm still not getting it to work.

        Private Function loadSalesData() As DataTable
                'Dim rds As New ReportDataSource
            Dim ds As New DataSet
            Dim dt As DataTable
            Dim dr As DataRow
            Dim idCoulumn As DataColumn
            Dim nameCoulumn As DataColumn
            dt = New DataTable()
            idCoulumn = New DataColumn("ID", Type.GetType("System.Int32"))
            nameCoulumn = New DataColumn("Name", Type.GetType("System.String"))
            dt.Columns.Add(idCoulumn)
            dt.Columns.Add(nameCoulumn)
            dr = dt.NewRow()
            dr = dt.NewRow()
            dr("ID") = 1
            dr("Name") = "Name1"
            dt.Rows.Add(dr)
            ds.Tables.Add(dt)
            Return ds.Tables(0)
        End Function

    Friday, July 9, 2010 6:41 PM
  • If you copy and paste the code exactly as the example give it, what happens? What error do you get and at what point in the code do you get it?

    If you want to get the example to work, you have to match the code exactly to get started, then you can modify it to you liking.

     

    Friday, July 9, 2010 8:33 PM
  • I fixed this problem.  The problem was due to having report parameters in my report that were not being set. 
    • Marked as answer by James12314909 Monday, July 12, 2010 7:44 PM
    Monday, July 12, 2010 7:44 PM