Click here to Skip to main content
16,011,538 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello!
I'm having an issue with VB 2010 and the report-viewer.

I have a form where I have placed a report viewer linked to a report (Report1.rdlc)
There is also a parameter.

What I want to do is to print automatically the report so the user don't have to press the printer icon.

But it only works if I don't use the parameter (although it appears right when I compile). The error I get is in the "report.Render("Image", deviceInfo, AddressOf CreateStream, warnings)"

ONE OR MORE PARAMETERS NEED TO BE SPECIFIED.... (more or less as it's in spanish)

Here's the code. Hope someone can help

HOW CAN I PASS THERE PARAMETERS TO THE RENDER METHOD??


oscarsaez1975@hotmail.com
"
VB
Imports System.Data.SqlClient
Imports Microsoft.Reporting.WinForms
Imports System.IO
Imports System.IO.Stream
Imports System.Data
Imports System.Text
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Drawing.Printing
Imports System.Collections.Generic
Imports System.Windows.Forms
Public Class Form1
    Implements IDisposable
    Public m_currentPageIndex As Integer
    Public m_streams As IList(Of Stream)
    Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: esta línea de código carga datos en la tabla 'ContadoresDataSet.Contadores' Puede moverla o quitarla según sea necesario.

        'Dim valor As String
        'valor = "Hola"

        Dim params(1) As Microsoft.Reporting.WinForms.ReportParameter
        params(0) = New Microsoft.Reporting.WinForms.ReportParameter("reportTitle", "Hola")
        params(1) = New Microsoft.Reporting.WinForms.ReportParameter("reportFile", "MAS")
        ReportViewer1.LocalReport.SetParameters(params)
        Me.ReportViewer1.RefreshReport()
    End Sub

    Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Run()
    End Sub
    Public Function LoadSalesData() As DataTable
        Dim dataSet As New DataSet()
        dataSet.ReadXml("data.xml")
        Return dataSet.Tables(0)
    End Function

    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(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 = "\\Printserver\RICOH Aficio MPC2500_MAT"

        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)
            Debug.WriteLine(msg)
            Return
        End If
        AddHandler printDoc.PrintPage, AddressOf PrintPage
        printDoc.Print()
    End Sub

    Public Sub Run()
        Dim report As LocalReport = New LocalReport()
        report.ReportPath = "Report1.rdlc"
        report.DataSources.Add(New ReportDataSource("Sales", LoadSalesData()))
        report.GetParameters()

        Export(report)
        m_currentPageIndex = 0
        Print()
    End Sub

    Public Overloads Sub Dispose() Implements IDisposable.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

End Class

"
Posted
Updated 26-Mar-14 4:57am
v4
Comments
[no name] 26-Mar-14 9:01am    
You need to pass the correct number of parameters to the Render method.
bpcs56 26-Mar-14 10:58am    
Hello!
But how can I pass the parameters to the render method?
Thanks!
[no name] 26-Mar-14 11:04am    
See http://msdn.microsoft.com/en-us/library/ms252172(v=vs.90).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1
bpcs56 26-Mar-14 11:08am    
That's what I am doing. I don't see any parameter sending/receving on that link...
bpcs56 26-Mar-14 12:00pm    
Any other help...?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900