Link to home
Start Free TrialLog in
Avatar of holemania
holemania

asked on

ReportViewer Print without Viewing

I am using ReportViewer as a template so that I can print out labels from a Zebra printer.  I am able to do this successfully by using my textboxes as parameters.  The problem that I run into is that I want the user to just click a button on my application and have the label print out without the ReportViewer showing up.  This article walks through it, but I can't seem to follow.  I get error saying that it can't find the executable when compiling.

http://msdn2.microsoft.com/en-us/library/ms251693(VS.80).aspx

So do I create a module or class and call it, or what?  Can someone walk me through this by steps?

Example:
1) From your application put in this code
2) From module/class put this
3) Etc.....
Avatar of kdwood
kdwood

Holmania,

Just a quick question for clarification.  When you say that you are using the ReportViewer as a template, do you mean that you are using a VB.NET report (rdlc) file as a template?  

The ReportView itself, is just a container/viewer to bind an existing report to.   Based on your description, you may be better off not using the reportviewer control and printing the RDLC file directly.   I have code that I can post for that, based on your answer to my question.

Regards,

Keith
Avatar of holemania

ASKER

kdwood,

Yes if I don't need to use ReportViewer and only the RDLC even better.  The Zebra TLP 2844 uses EPL langauge to design the template and this is a very tedious process.  I was able to use a report(rdlc) as template and have it bring all my data to the report as parameter.  Only thing is that now when a user clicks print on the application it is not doing that.  They would have to print in ReportViewer to have it print.  

So provide me the code sample you have and I can look at it.
ASKER CERTIFIED SOLUTION
Avatar of kdwood
kdwood

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This line is throwing me an exception error.

report.Render("Image", deviceInfo, AddressOf CreateStream, warnings)

The error is  says that LocalProcessingException was unhandled.

I am binding the textboxes from my application as parameter to the template since is only serves as a template.

Parameter Example:
Dim PO As New Generic.List(Of Microsoft.Reporting.WinForms.ReportParameter)
PO.Add(New Microsoft.Reporting.WinForms.ReportParameter("PO", txtPO.Text, True))
ReportViewer.ReportViewer1.LocalReport.SetParameters(PO)
oops....I lied to you about not needing to modify the Reporting Class!  :)

You need to fix this section and point it to a valid folder on your hard drive that it can render the report stream.

-----  This is what you will fix

   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("U:\" + name + "." + fileNameExtension, FileMode.Create)     <--------  Right here.  Set it to a valid path on your system

        m_streams.Add(stream)
        Return stream
    End Function
 -------------------

Sorry about that.
When you said to set it to a valid path on my system, it does not matter where as long as I have a valid path where it can steram my report right?  I'm still getting the same error after setting it to  "C:\".

 Error: "An error occurred during local report processing".
No, it should not matter as long as it is a valid path and you have the proper permission to write to that folder.

Double check your report path and make sure you have it correct :

report.ReportPath = "C:\data\reports\ActReq.rdlc"  <--- this is behind the print button in my example.  It must be a physical path to the report and not an embedded object.
One more thing....remove the line below from the Reporting class file.  I was doing some duplex printer testing for someone else and forgot I had that line in there.

printDoc.PrinterSettings.Duplex = Duplex.Horizontal
This has been changed to my C:\ drive.

From Reporting.vb codebehind:
Dim stream As Stream = _
        New FileStream("C:\" + name + "." + fileNameExtension, FileMode.Create)  <--------change with a valid path
        m_streams.Add(stream)
        Return stream

From print button:
report.ReportPath = "C:\Report\TestReport.rdlc"   <-------------------actual path where the report is

From Reporting.vb codebehind:
report.Render("Image", deviceInfo, AddressOf CreateStream, warnings)  <-------Getting exception error here


So far I think this will work once I can get this exception error resolved.
Can you post your entire code behind the print button click event.   Maybe I can see something there.
It's same as what you got except that getting data from a dataset, I'm passing the data from my application via a parameter to the report field.  So PO = Purchase Order Number, PO122232, from the application is a parameter that is passed to the report and will print "PO122232".

Code from print button:

 Dim myRPTForm As New Reporting ' This would be your rdlc report for your labels
        Dim report As Microsoft.Reporting.WinForms.LocalReport = New Microsoft.Reporting.WinForms.LocalReport()

        myPRT = New Reporting

       'Textbox to report field
        Dim PO As New Generic.List(Of Microsoft.Reporting.WinForms.ReportParameter)
        PO.Add(New Microsoft.Reporting.WinForms.ReportParameter("PO", txtPO.Text, True))
        ReportViewer.ReportViewer1.LocalReport.SetParameters(PO)

        ' Set the physical path to your report rdlc file.      
        report.ReportPath = "C:\Report\TestReport.rdlc"


        ' 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()
I'm thinking even without the following it should still print my report since it's just a template:
'Textbox to report field
        Dim PO As New Generic.List(Of Microsoft.Reporting.WinForms.ReportParameter)
        PO.Add(New Microsoft.Reporting.WinForms.ReportParameter("PO", txtPO.Text, True))
        ReportViewer.ReportViewer1.LocalReport.SetParameters(PO)

Pretty much with your code it should be able to print my report regardless if I make any changes.  There must be something I'm missing.  I should be able to take your code and throw my report for it to print and it should print static values.
Try removing the this line as you are no longer using the ReportViewer control.

ReportViewer.ReportViewer1.LocalReport.SetParameters(PO)  <---- Remove This

Also, on your RDLC file, make sure you have defined a Report Parameter for PO, since you will not be passing the parameter to the ReportViewer any longer.





I just did a quicked little test project, and I'm getting the same error as you.

Hang in there, I will see what I can figure out.

I removed the line as you said, but seems to always error out at the following line:

report.Render("Image", deviceInfo, AddressOf CreateStream, warnings)  <----Gives Exception error

I noticed that you have this "rptActionReq" in this line.

Dim myRPTForm As New rptActionReq  ' This would be your rdlc report for your labels

I changed mine to Reporting for the Reporting.vb.  My rdlf name is TestReport.rdlc.  Maybe that might be where the problem is?
Ok,

This should fix it.  We were missing one step!   Since you are no longer using the ReportViewer control we have to set the PO Parameter you defined directly to the report.   Make sure you do so after the ReportPath line shown below:

  report.ReportPath = "C:\Report\TestReport.rdlc"
  report.SetParameters(PO)   <-----  Add This

Also you can remove this line as it serves no purpose:

Dim myRPTForm As New Reporting

Hope this works for you, it did for me.

Regards,

Keith




So...this would be your new print button code base on what you posted:

        Dim report As Microsoft.Reporting.WinForms.LocalReport = New Microsoft.Reporting.WinForms.LocalReport()

        myPRT = New Reporting

       'Textbox to report field
        Dim PO As New Generic.List(Of Microsoft.Reporting.WinForms.ReportParameter)
        PO.Add(New Microsoft.Reporting.WinForms.ReportParameter("PO", txtPO.Text, True))

        ' Set the physical path to your report rdlc file.      
        report.ReportPath = "C:\Report\TestReport.rdlc"
        report.SetParameters(PO)

        ' 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()
Ahhh sweet lord!!! Thank you! Thank you!

Is there a way to disabled the print dialog that pops up?

Also can a printer be assigned to it?
When you say the print dialog popping up.  Do you mean the window that asks you to select a printer and your print preferences?   I just get a quick printing status screen that flashes for a quick second.   Is is possible that you don't have a default printer set in Windows?
I have a default printer, but the way my program work is that it sends in the parameter to the printer and keeps looping until end of file(Quantity print specified).  So if a user wants to print 15 labels of the same label, it will loop through the print function 15 times or if the user wants to print label 1-10 different label, it will loop 10 times.  Not the most efficient, but the application is design this way so that the user can see the data being rendered by the application and not through the reportviewer.

As of now each time it prints it gives status of 1 and fades and repeat until the amount print specified is complete.  So if 10 quantity is specified it will pop that status box 10 times.
This function will get me the printer I'm looking for.

 printDoc.PrinterSettings.PrinterName
I didn't know that I can use the PrinterSettings to set it so that I can do Copies or Range.  That will work with what I'm looking for than.

Thanks again.
My pleasure, glad I could help.  The ReportViewer and Reports in VS2005 are so under documented it makes me sick.  I've spent hours searching for solutions for simple things that end up being a few lines of code.

Take care,

Keith
hi

i am trying to accomplish a similar task. i have been able to set up and walkthorugh most of the code through the comments.

I am having issues as far as trying to figure out what parameters you are passing through the txtPO.text box, i have my report set up with a sql data source

do i need it to pass it parameters what do they accomplish?

any suggestions on where to go from here?