Solved

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

Posted on 2008-06-17
5
999 Views
Last Modified: 2013-11-26
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

0
Comment
Question by:kspolikoff
  • 3
  • 2
5 Comments
 
LVL 48

Expert Comment

by:jpaulino
ID: 21807906
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/
0
 

Author Comment

by:kspolikoff
ID: 21812191
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
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 21812280
Whre you have the error ?

Have you added the dataset name as I showed you ?
report.DataSources.Add(New ReportDataSource("DataSet1_myTable", reader))
0
 

Author Comment

by:kspolikoff
ID: 21813057
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

0
 
LVL 48

Accepted Solution

by:
jpaulino earned 500 total points
ID: 21840963
Have you checked if "DataSet1_myTable" is really the name of the dataset that you have in the report ? And do you checked in the innerexception the message that you got when you're trying to render ?
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now