none
printing rdlc without the reportviewer RRS feed

  • Question

  • I am reading a few posts about printing the rdlc report using PDF or excel. 
    I am new to this area and need assistance.
    I am using VS 2008 (VB).
    Can't follow all that is needed for this procedure.
    I have 2 or 3 reports that need to process in batch.  I have the reports created, but I don't need the viewer to display.  I just want the reports to print automatically. 
    I see there is a way to hide the viewer.  Is there a way to force the print with it hidden?

    Duane Sanders
    Wednesday, August 26, 2009 10:11 PM

All replies

  • Hi Duane,
    For printing, check out Brian Blog on printing:  http://blogs.msdn.com/brianhartman/archive/2009/02/27/manually-printing-a-report.aspx

    If you want to export to Excel, check out this sample  http://msdn.microsoft.com/en-us/library/ms251839.aspx.  To do it for PDF, it is similar.  Simply pass in "PDF" for the format when calling the Render method.

    There are many ways to hide the viewer. How about using ReportViewer.Visible property to set the visibility?

    Thanks.

    Stella Chan
    SQL Server Reporting Services

    -----------
    This post is provided as-is with no warranty or support.
    Thursday, August 27, 2009 5:02 AM
  • Stella,

    Yes, I have used the .Visible property.  I was wondering if there was a property that would automatically print the report from the viewer without displaying the viewer.

    I have seen Brians blog and many others.
    I am not familiar with namespaces, List(Stream) or the API's that Brian mentions.  Haven't had enough time to really dig into all that.

    I don't understand why it is so difficult to print a report that is already designed and has a datasource attached to it.

    I will try to use the code Brian gives.  Can you tell me where the code will be placed in the project?  In a separate Mod?  In the ReportViewer Load?  I am still fairly new to the VS scene....

    I may be able to tell more about how to use this code after I convert it to VB.......fun!

    For now....wish me luck...

    and thanks for the help.
    Duane Sanders
    Thursday, August 27, 2009 5:11 PM
  • Duane,

    Thank you for your clarification.  I think I understand what you are trying to do better now.

    Take a look at this sample:  http://msdn.microsoft.com/en-us/library/ms252091(VS.80).aspx  It has a VB one in there.

    Thanks.

    Stella Chan
    SQL Server Reporting Services


    Saturday, August 29, 2009 5:17 AM
  • Stella,

    Thanks for the info.
    I have reviewed that code.
    I am still having issues with this.

    I have developed a module that creates a dataset and table using SQL statement. This is an in-memory dataset/table named "Commissions/Invoices".
    I was told that I had to have a datasource to build the MS Report, so I built a dummy dataset/table (IBM_dataset/Invoices_x) in my project to use.

    In code, I change the report datasource using this code:

     

    Me.Invoices_xBindingSource.DataSource = iNet.Commissions
    Me.Invoices_xBindingSource.DataMember = "INVOICES"
    Me.ReportViewer1.RefreshReport()

    the reportviewer displays the report great.

    But now I want to run the report without the viewer.
    so I use code that I got from different places on the forums.  Most of it I do not understand yet.

    I change the datasource to "Commissions/Invoices",  but it gives an error during the Report.Render. 
      Error: "A data source instance has not been supplied for the data source "IBM_DataSet_Invoices_x".

    I have read the forums concerning this error message and I tried most of the suggestions with no luck.

    What am I missing?

     

     

     

    Private m_currentPageIndex As Integer
    Private m_streams As IList(Of Stream)
    Private Function LoadSalesData() As DataTable
    Dim dataSet As New DataSet("iNet.Commissions")
    Return dataSet.Tables("INVOICES")
    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)
    Try
    report.Render("Image", deviceInfo, AddressOf CreateStream, warnings)
    Catch ex As Exception
    MsgBox(ex.ToString)
    End Try

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

    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, 0, 0)
    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

    EndClass

    -duane


    Duane Sanders
    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 rc As Stream = New FileStream(name + "." + fileNameExtension, FileMode.Create)
    m_streams.Add(rc)
    Return rc
    End Function

    ImportsSystem
    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 DirectPrinting

    Public
    Sub Run()
    Dim report As LocalReport = New LocalReport()
    report.ReportPath =
    "rpt_Terr_Payables.rdlc"
    Dim rds As ReportDataSource = New ReportDataSource 
           (
    "Commissions_Invoices",modData.Commissions.Tables("Invoices"))
    iNet.frm_commissionspayables.ReportViewer1.LocalReport.DataSources.Clear()
    iNet.frm_commissionspayables.ReportViewer1.LocalReport.DataSources.Add(rds)iNet.frm_commissionspayables.ReportViewer1.LocalReport.Refresh()

    Export(report)
    m_currentPageIndex = 0
    Print()
    End Sub
    Tuesday, September 1, 2009 4:47 PM
  • Duane,
    From the information given, my guess is as follow:

    1.  In your RDLC file, the dataset name is probably named as:  IBM_DataSet_Invoices_x.  I am guessing this based on the error.
    2.  I assume that you want to use the modData.Commissions.Tables("Invoices") to provide the data.

    If yes, I would try changing the following line from:
    Dim rds As ReportDataSource = New ReportDataSource 
           (
    "Commissions_Invoices",modData.Commissions.Tables("Invoices"
    ))

    to:

    Dim rds As ReportDataSource = New ReportDataSource 
           ("
    IBM_DataSet_Invoices_x",modData.Commissions.Tables("Invoices"
    ))


    Thanks.

    Stella Chan
    SQL Server Reporting Services

    ---------------

    This post is provided as-is with no warranty or support. 
    Tuesday, September 1, 2009 7:00 PM
  • Stella,

    You are correct in your assumption.

    I tried that code also today. Same error.

    why would we put "IBM_DataSet_Invoices_x" in the new ReportDataSource since I am wanting to change from it?
    Maybe I am not understanding the ReportDataSource property....

    any other suggestions?

    Duane Sanders
    Tuesday, September 1, 2009 8:24 PM