Solved

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

Posted on 2008-06-17
5
1,016 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

773 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