Results 1 to 13 of 13

Thread: [RESOLVED] Print Directly to Printer with RDLC Report

Hybrid View

  1. #1
    Addicted Member
    Join Date
    Jun 12
    Location
    Minnesota
    Posts
    128

    Resolved [RESOLVED] Print Directly to Printer with RDLC Report

    I am wondering how I can print directly to a printer with a local rdlc report instead of using the report viewer. Basically what I am trying to do is allow the user to push a button and print a report using the record that the user is currently viewing on the screen.

    I have tried using the code on the MSDN http://msdn.microsoft.com/en-us/library/ms252091.aspx, but I get an error Variable 'warnings' is passed by reference before it has been assigned a value. A null reference exception could result at runtime. Which is this part of the code:
    Dim warnings As Warning()
    m_streams = New List(Of Stream)()
    report.Render("Image", deviceInfo, AddressOf CreateStream, warnings)
    For Each stream As Stream In m_streams
    stream.Position = 0

    So it won't run. Also the msdn sample code wants an XML file and I only have an xsd.

    Can't believe this is so much work just to print the report without viewing it.

    Any help you can provide would be much appreciated as I am new to vb.

  2. #2
    Addicted Member
    Join Date
    Jun 12
    Location
    Minnesota
    Posts
    128

    Re: Print Directly to Printer with RDLC Report

    Ok I've got the example from MSDN working now. The example calls for an XML file instead of an XSD. I am wondering how I can convert (or get) my XSD files to XML. This little piece of code is my problem.
    Private Function LoadSalesData() As DataTable
    ' Create a new DataSet and read sales data file
    ' data.xml into the first DataTable.
    Dim dataSet As New DataSet()
    dataSet.ReadXml("C:\MaintenanceTracker\MaintenanceTracker\POReport.xml")
    Return dataSet.Tables(0)

    End Function
    Thanks for any help you can provide.
    Stacy

  3. #3
    Frenzied Member
    Join Date
    Sep 05
    Location
    Modesto, Ca.
    Posts
    1,590

    Re: Print Directly to Printer with RDLC Report

    I don't understand, what does this have to do with printing directly to the printer. A XSD file is not a data file. It contains the code for accessing a data file.

  4. #4
    Addicted Member
    Join Date
    Jun 12
    Location
    Minnesota
    Posts
    128

    Re: Print Directly to Printer with RDLC Report

    wes4bt,
    Thanks for your reply.
    In the MSDN example it wants an XML file to Read from (example above) I don't have an XML file for it to read from, but I already have a XSD file though so maybe I don't even need to do that step exactly like that. I am just guessing as to what the code in the MSDN example is doing. I think it makes an XSD file from an XML.

    I was able to get the example to work for me using a report that doesn't have any data (or DataSet hooked to it) on it and that was fine but the Report I am trying to print directly to the printer will have data on it and is hooked to a dataset.

    I'm a little out of my league here as I have only been working with .net for a few months. Not sure what the Return dataset.Table(0) does. If that is so maybe I need to change the code for this function to return my existing dataset table I already have. Not sure exactly how to do that though.
    Your knowledge and input would be greatly appreciated.
    Stacy

  5. #5
    Frenzied Member
    Join Date
    Sep 05
    Location
    Modesto, Ca.
    Posts
    1,590

    Re: Print Directly to Printer with RDLC Report

    This link has some videos on creating rdlc reports http://windowsclient.net/learn/videos.aspx there's lots of info on the subject, just Google something like ".net rdlc dataset"

  6. #6
    Addicted Member
    Join Date
    Jun 12
    Location
    Minnesota
    Posts
    128

    Re: Print Directly to Printer with RDLC Report

    wes4dbt,

    Oh I already have my report built - it's the getting it to print directly to the printer without the preview that is causing the problem.
    Stacy

  7. #7
    Frenzied Member
    Join Date
    Sep 05
    Location
    Modesto, Ca.
    Posts
    1,590

    Re: Print Directly to Printer with RDLC Report

    After reviewing the link in post #1, I think I understand your problem.
    report.DataSources.Add(New ReportDataSource("Sales", LoadSalesData()))
    You don't need to use the "LoadsalesData", basically what you need is this,
    report.DataSources.Add(New ReportDataSource("thenameofyourdataset",thenameofthedatatable))
    Read this,
    Hooking up the DataSource at Runtime
    Finally, you need to let the ReportViewer know where to get the actual data from so it can feed that into the report. In your form's OnLoad method (or wherever is appropriate for your application), you need to add code much like this:


    private void Form1_Load(object sender, EventArgs e) {
    // somehow get the data at runtime, application specific
    this.myData = GetDataSource();

    this.reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet_Person", myData));
    this.reportViewer1.RefreshReport();
    }

    Notice we are adding a DataSource to the ReportViewer. This is the bridge from your data into the report. Key here is the name you specify for the ReportDataSource, it must match the name found in the .rdlc file for this dataset. This name was determined by how you named the DataSet you added to your project and designed. If you view the .rdlc file in the XML editor (right click it and select "Open With..." then pick "XML Editor"), you will find a <DataSets> node, containing a <DataSet> node, this node is the report's understanding of the data for this report, and what your actual data will map to at runtime. Notice the Name attribute of the DataSet node, this is the name you need to specify in the ReportDataSource constructor.
    Make sure you fill the datatable.

  8. #8
    Addicted Member
    Join Date
    Jun 12
    Location
    Minnesota
    Posts
    128

    Re: Print Directly to Printer with RDLC Report

    Quote Originally Posted by wes4dbt View Post
    Notice we are adding a DataSource to the ReportViewer. This is the bridge from your data into the report.
    I can get the report to work perfectly if I use the reportviewer. But the user doesn't want to view them - just print directly to the printer.

    Can't I just change the LoadSales() Function to fill and use my dataset and then return that table? Not sure exactly how to do that though. I have tried several different things but I always get that same error from my previous post.

    Stacy

  9. #9
    Addicted Member
    Join Date
    Jun 12
    Location
    Minnesota
    Posts
    128

    Re: Print Directly to Printer with RDLC Report

    Ok I have tried doing your above idea. In my Run() I changed the code so it doesn't use the LoadSales() function anymore and add as you suggested the following...
    Private Sub Run()
    Dim report As New LocalReport()
    report.ReportPath = ("C:\MaintenanceTracker\MaintenanceTracker\Reports\PO.rdlc")
    'report.DataSources.Add(New ReportDataSource("Sales", LoadSalesData()))
    DataTable1TableAdapter.Fill(Me.POReport.DataTable1)
    report.DataSources.Add(New ReportDataSource("POReport", "DataTable1"))
    Export(report)
    Print()
    End Sub
    So it used to do...
    report.DataSources.Add(New ReportDataSource("Sales", LoadSalesData()))

    I now fill my datatable and do the datasource add as follows...
    DataTable1TableAdapter.Fill(Me.POReport.DataTable1)
    report.DataSources.Add(New ReportDataSource("POReport", "DataTable1"))
    I am still getting an error when I try to run. I don't know about using your suggestions for the following lines...
    this.reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet_Person", myData));
    this.reportViewer1.RefreshReport();
    Because I am not using the reportViewer - I want to print directly to the printer without using the reportviewer.

    Thanks for all your help with this. I think it is really close!
    Stacy

    P.S. When I run it with these lines added and taken out I get an error on this line of code under my Private Sub Export
    report.Render("Image", deviceInfo, AddressOf CreateStream, warnings)
    and it says LocalProcessingException was unhandled
    An error occurred during local report processing.
    Last edited by StacyOW; Oct 4th, 2012 at 03:29 PM.

  10. #10
    Frenzied Member
    Join Date
    Sep 05
    Location
    Modesto, Ca.
    Posts
    1,590

    Re: Print Directly to Printer with RDLC Report

    Ignore the the code referring to the reportviewer, I just wanted you to read about adding a datasource, especially this,
    Key here is the name you specify for the ReportDataSource, it must match the name found in the .rdlc file for this dataset. This name was determined by how you named the DataSet you added to your project and designed. If you view the .rdlc file in the XML editor (right click it and select "Open With..." then pick "XML Editor"), you will find a <DataSets> node, containing a <DataSet> node, this node is the report's understanding of the data for this report, and what your actual data will map to at runtime. Notice the Name attribute of the DataSet node, this is the name you need to specify in the ReportDataSource constructor.
    If this line is causing the error,
    report.Render("Image", deviceInfo, AddressOf CreateStream, warnings).
    Have you checked the other parameters?

    If you still can't find the problem, post all relevant code.
    Last edited by wes4dbt; Oct 4th, 2012 at 09:02 PM.

  11. #11
    Addicted Member
    Join Date
    Jun 12
    Location
    Minnesota
    Posts
    128

    Re: Print Directly to Printer with RDLC Report

    I believe it is the warnings that is causing the problem which in turn refers back to my datasource being the problem. If I run this code with a report that doesn't have a datasource attached to it - it works perfectly. It's when I try to use the code to refer to my dataset that I get the errors when I run it. The error is so generic though so it isn't much help.

    Thanks,
    Stacy

  12. #12
    Addicted Member
    Join Date
    Jun 12
    Location
    Minnesota
    Posts
    128

    Re: Print Directly to Printer with RDLC Report

    Ok I can get it to print but it is printing blank pages. I did as you said and opened the RDLC file with XML editor and the <DataSet Name ="DataSet1"> the DataSource is <DataSource Name="POReport"> that is near the top of the file. Towards to bottom is another section that has dataset it looks like this...
    <Query>
    <DataSourceName>POReport</DataSourceName>
    <CommandText>/* Local Query */</CommandText>
    </Query>
    <rdataSetInfo>
    <rdataSetName>POReport</rdataSetName>
    <rd:SchemaPath>C:\Users\Stacy\documents\visual studio 2010\Projects\MaintenanceTracker\MaintenanceTracker\POReport.xsd</rd:SchemaPath>
    <rd:TableName>DataTable1</rd:TableName>
    <rd:TableAdapterFillMethod>Fill</rd:TableAdapterFillMethod>
    <rd:TableAdapterGetDataMethod>GetData</rd:TableAdapterGetDataMethod>
    <rd:TableAdapterName>DataTable1TableAdapter</rd:TableAdapterName>
    </rdataSetInfo>
    </DataSet>
    Not sure which area in the file you are referring too.
    So I tried leaving the LoadSales() and just changing it so it fills and uses my current dataset for the report. This is what I have for the LoadSales() right now. Apparently it is not filling the dataset cuz the report is printing blank.
    Private Function LoadSalesData() As DataTable
    ' Create a new DataSet and read sales data file
    ' data.xml into the first DataTable.
    Dim dataSet1 As New DataSet("POReport")
    dataSet1.Tables.Add("DataTable1")
    DataTable1TableAdapter.Fill(POReport.DataTable1)
    Return dataSet1.Tables(0)

    End Function
    I'm pretty sure I have screwed up this vb code somehow but too new to figure out where. Not sure how it works when you Dim a new DataSet and then how do you fill it?
    Thanks,
    Stacy
    Last edited by StacyOW; Oct 10th, 2012 at 12:35 PM.

  13. #13
    Addicted Member
    Join Date
    Jun 12
    Location
    Minnesota
    Posts
    128

    Re: Print Directly to Printer with RDLC Report

    wes4dbt,
    I finally got it! YEAH! I took out all the Dim stuff and just used the following to fill my existing datatable and then return it and it works perfectly!
    DataTable1TableAdapter.Fill(POReport.DataTable1)
    Return POReport.Tables(0)
    Thank you so much for all your advise - I really appreciate it! I couldn't have figured it out without your help.
    Stacy

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •