Link to home
Create AccountLog in
Avatar of kspolikoff
kspolikoff

asked on

ReportViewer Print without Viewing (Need Help Got Start FromAnother Post)

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

This line gives a LocalProcessingException error
i think my issues is involved with binding my datasource to the report, but i dont know how to fix it
Imports System.IO
Imports System.Data
Imports System.Text
Imports System.Drawing.Imaging
Imports System.Drawing.Printing
Imports System.Collections.Generic
Imports Microsoft.Reporting.WinForms
Public Class Reporting
    Implements IDisposable
 
    Public m_currentPageIndex As Integer
    Public m_streams As IList(Of Stream)
 
    Public 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("C:\CSB" + name + "." + fileNameExtension, FileMode.Create)
        m_streams.Add(stream)
        Return stream
    End Function
 
    Public 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
 
    Public 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
 
    Public Sub Print()
 
        Const printerName As String = "Microsoft Office Document Image Writer"
 
        If m_streams Is Nothing OrElse 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
 
    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
 
    Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
        Dim report As Microsoft.Reporting.WinForms.LocalReport = New Microsoft.Reporting.WinForms.LocalReport()
 
        Dim myPRT As New Reporting
 
 
        ' Set the physical path to your report rdlc file.      
        report.ReportPath = "C:\Documents and Settings\Kevin\Desktop\CSB Windows\CSB\CSB\Report1.rdlc"
        report.DataSources.Add(New ReportDataSource(Me.ASSIGNTableAdapter.Fill(Me.CSBOLDSQLDataSet.ASSIGN)))
 
        ' This next code prepares the rdlc file for printing and calls the print function from the Reporting class file
        myPRT.Export(report)
        myPRT.m_currentPageIndex = 0
        myPRT.Print()
    End Sub
End Class

Open in new window

Avatar of Jorge Paulino
Jorge Paulino
Flag of Portugal image

Hi kspolikoff,

you must do something like this:
Dim reader As SqlDataReader
Dim SQL As String = "SELECT * FROM myTable"
Dim cmd As SqlCommand = New SqlCommand(SQL, conn)

reader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
report.DataSources.Add(New ReportDataSource("DataSet1_myTable", reader))

Where "DataSet1_myTable" is the name of the dataset that you add to your report. You can open the report with the xml editor and check in this section the right name <DataSet Name="DataSet1_myTable">

Also you can do in the try ... catch ex.InnerException instead of ex.Message. Add a breakpoint there (InnerException ) and look the right exception.

You can find more information on this links:
http://msdn.microsoft.com/en-us/library/ms252091(VS.80).aspx
http://gotreportviewer.com/ 
Avatar of kspolikoff
kspolikoff

ASKER

I tried, i am able to pass the report the datasource directly from sql, but the report is set up to use data from an ADO.NET datasource and because of this it is still passing the same error. Any idea how i can programatically pass the ADO source to the report when i try i get a message saying unable to convert to Microsoft.Reporting.WinForms.ReportDataSource
Whre you have the error ?

Have you added the dataset name as I showed you ?
report.DataSources.Add(New ReportDataSource("DataSet1_myTable", reader))
here is my updated code
i have a warning that Function SQLReader doesnt return a value on all paths a numm referance exception could occur

but i think that can be solved with a return line,

when running it still gives me the LocalProcessingException error at
report.Render("Image", deviceInfo, AddressOf CreateStream, warnings)

i think this is because my report is set up to get data from one of the ADO.NET sources that refrences the same view from sql. so i am unsure of how to either change the links in the report to accept the programatic data, or to pass the report a ADO.NET data source

Imports System.IO
Imports System.Data
Imports System.Text
Imports System.Drawing.Imaging
Imports System.Drawing.Printing
Imports System.Collections.Generic
Imports Microsoft.Reporting.WinForms
Imports System.Data.SqlClient
 
Public Class Reporting
    Implements IDisposable
    Private m_currentPageIndex As Integer
    Private m_streams As IList(Of Stream)
    Private Function SQLReader()
        Dim conn As New SqlConnection()
        conn.ConnectionString = "Data Source=KSP;Initial Catalog=CSBOLDSQL;Integrated Security=True"
        conn.Open()
        Dim reader As SqlDataReader
        Dim SQL As String = "SELECT * FROM dbo.view_1"
        Dim cmd As SqlCommand = New SqlCommand(SQL, conn)
        reader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
 
 
 
    End Function
    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("C:\CSB\" + _
             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
 
 
    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
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim report As LocalReport = New LocalReport()
        report.ReportPath = "C:\Documents and Settings\Kevin\Desktop\CSB Windows\CSB\CSB\Report1.rdlc"
        report.DataSources.Add(New ReportDataSource("DataSet1_myTable", SQLReader()))
 
 
        Export(report)
 
        m_currentPageIndex = 0
        Print()
    End Sub
End Class

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Jorge Paulino
Jorge Paulino
Flag of Portugal image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer