none
VS 2010 Reporting Services render to PDF RRS feed

  • Question

  • Dear all, I'm pretty sure I'm missing something, but I couldn't figure it out :(

    I have a very simple report, it is so simple all it has is just a text box with "aaa" as its value. Then i have the following code to create a PDF version of the report.

            Dim rptViewer As New ReportViewer
            Dim warnings As Warning() = Nothing
            Dim streamids As String() = Nothing
            Dim mimeType As String = Nothing
            Dim encoding As String = Nothing
            Dim extension As String = Nothing
            Dim bytes As Byte()

            With rptViewer.LocalReport
                Try
                    .ReportPath = "D:\Development\Source Code\rptBackCover.rdlc"
                    bytes = .Render("PDF", Nothing, mimeType, encoding, extension, streamids, warnings)
                    Using fs As New FileStream("Reports\Report.pdf", FileMode.Create)
                        fs.Write(bytes, 0, bytes.Length)
                    End Using
                Catch ex As LocalProcessingException
                    MsgBox(ex.Message)
                End Try
            End With

    I keep getting the error "Referenced object has a value of 'Nothing'." at

    bytes = .Render("PDF", Nothing, mimeType, encoding, extension, streamids, warnings)

    I have tried all possible ways I could think of, please kindly help me :(

    Many thanks.

    Thursday, August 12, 2010 10:10 AM

Answers

  • FINALLY!!!!!

    Well, my fault ..... I'm doing my test using a solution/project converted from VS2005. The project was referencing reporting services 8. I changed that to 10 and all good now :)

    Many thanks to all those that helped :)

    • Marked as answer by WaiLoon Monday, August 16, 2010 3:42 AM
    Monday, August 16, 2010 3:42 AM

All replies

  • I didn't the exception you're talking about using your code, but I did get an exception when the ReportViewer tries to unload its app domain.

    If you're not using the viewer to display a report in a Windows Form, you should just create a LocalReport object. I don't get any exception when I made this change. For example:

     

     

    Dim report As New LocalReport
    Dim warnings As Warning() = Nothing
    Dim streamids As String() = Nothing
    Dim mimeType As String = Nothing
    Dim encoding As String = Nothing
    Dim extension As String = Nothing
    Dim bytes As Byte()

     

     

    With report
      Try
       
    .ReportPath = "<your_report_path>"
       
    bytes = .Render("PDF", Nothing, mimeType, encoding, extension, streamids, warnings)
       
    Using fs As New FileStream("Reports\Report.pdf", FileMode.Create)
          fs.Write(bytes, 0, bytes.Length)
       
    End Using
     
    Catch ex As LocalProcessingException
       
    MsgBox(ex.Message)
     
    End Try
    End With


    Cephas Lin This posting is provided "AS IS" with no warranties.
    Thursday, August 12, 2010 4:04 PM
    Moderator
  • Hi, thanks a lot for your reply. I have tried your code, and I'm getting the following error:

    The definition of the report 'Main Report' is invalid

    I did search around and found suggestions about copying some dll files to the bin folder, I've tried those but couldn't get it to work. Is there any property values I need to say for the report? Perhaps build action (which currently is Embedded Resource) etc?

    Btw, I tried using ReportEmbeddedResource instead of ReportPath but the result is the same.

    Friday, August 13, 2010 3:42 AM
  •  this.reportViewer1.RefreshReport();
    
          //Page settings for PDF File.
          string mimeType;
          string encoding;
          string extension;
          string deviceInfo =
         "<DeviceInfo>" +
         " <OutputFormat>PDF</OutputFormat>" +
         " <PageWidth>8.5in</PageWidth>" +
         " <PageHeight>10.5in</PageHeight>" +
         " <MarginTop>0.05in</MarginTop>" +
         " <MarginLeft>0.05in</MarginLeft>" +
         " <MarginRight>0.05in</MarginRight>" +
         " <MarginBottom>0.05in</MarginBottom>" +
         "</DeviceInfo>";
    
          string[] streamids;
          Microsoft.Reporting.WinForms.Warning[] warnings;
    
        
          byte[] bytes = reportViewer1.LocalReport.Render("PDF", deviceInfo, out mimeType, out encoding, out extension, out streamids, out warnings);
    
          string filepath = "D:\MyFile.pdf"
          
          FileStream fs = new FileStream(filepath, FileMode.Create, FileAccess.Write, FileShare.Read);
          fs.Write(bytes, 0, (int)bytes.Length);
          fs.Close();
    
          System.Diagnostics.Process.Start(filepath);
    Friday, August 13, 2010 4:56 AM
  • Hi, Hiren, same result after converting your code to VB.net :(

    The definition of the report 'Main Report' is invalid

    at byte[] bytes = reportViewer1.LocalReport.Render("PDF", deviceInfo, out mimeType, out encoding, out extension, out streamids, out warnings);

    From all sample codes I've found so far (which basically are the same), I suspect this is more like a setting type problem; properties perhaps .....

    I think I should highlight that I am using VS 2010 professional, and I have done some reports that are working in VS 2005. Just can't understand why such simple report is giving problem.

    Friday, August 13, 2010 8:14 AM
  • Hi all,

    Just a thought. As I have mentioned, the report I created here has nothing but a text box with "aaa" as its value. Wonder if reporting services doesn't allow reports with no data, parameter at all ..... I shall try passing in some data.

    Friday, August 13, 2010 8:26 AM
  • Have tried passing in some report parameters, didn't help :(

    Can someone help me, please? Am totally lost .....

    Monday, August 16, 2010 3:31 AM
  • FINALLY!!!!!

    Well, my fault ..... I'm doing my test using a solution/project converted from VS2005. The project was referencing reporting services 8. I changed that to 10 and all good now :)

    Many thanks to all those that helped :)

    • Marked as answer by WaiLoon Monday, August 16, 2010 3:42 AM
    Monday, August 16, 2010 3:42 AM