Solved

Visual Basic 2008 print report automatically

Posted on 2009-04-13
17
955 Views
Last Modified: 2012-05-06
I have a simple program that uses the Microsoft Report Viewer to view a report on my Report server, SQL SSRS report server.  That works very well, now what I really want to do is print the report without the user having to click on anything or even see the report.  I have looked everywhere I can but I have not been able to come up with a solution.  This is a link to a page that might work but i don't know the first thing about C# programming and converting it to vb.net doesn't seem to work, http://blogs.msdn.com/bryanke/articles/71491.aspx.   Any help would be greatly appreciated as I have been working on this simple item for far too long.
0
Comment
Question by:talbers_bci
  • 10
  • 6
17 Comments
 
LVL 11

Expert Comment

by:CraigYellick
ID: 24134148
When configured for a remote/server-based report, the ReportViewer control is just a visual front-end on top of the Reporting Services web service. You can call the service endpoint directly without using the control. The article you found looks to me to be a very complete discussion of the topic, however, it does require that you have a properly configured and functioning web service reference. The control does this behind the scenes, so if the report works fine using the control then we know that the endpoint is working and it's just a matter of calling it correctly.

There are free C#-to-VB code converters available, are you saying that the converted code did not work? Or that the converter you used failed to handle the code?

  http://www.developerfusion.com/tools/convert/csharp-to-vb/
  http://converter.telerik.com/

If you provide the error message or other details about what is going wrong I'm sure it can be fixed.
0
 
LVL 1

Author Comment

by:talbers_bci
ID: 24134344
Ok, I have conveted the code, added a web reference to the report server and added the system.drawing reference.  It is saying that the Imports PrintReport.reportserver   "Namespace or type specified in the Imports 'PrintReport.reportserver' doesn't contain any public member or cannot be found. Make sure the namespace or the type is defined and contains at least one public member. Make sure the imported element name doesn't use any aliases.".  Also it is saying  "Type 'ReportingService' is not defined.      "

I am not sure where to go from here.  I assume it is missing a reference but I am not sure what one is missing.


0
 
LVL 1

Author Comment

by:talbers_bci
ID: 24134367
There are a number of other errors, I can post if necessary.  They may clear up if the missing references are added.
0
 
LVL 11

Expert Comment

by:CraigYellick
ID: 24134398
The C# example is a console (aka command line) application and is hard-coded to render one report and print it. It will only work as a VB application if the VB project is set up exactly the same way.

I assume you're trying to print the report in an existing WinForms application, so we can disregard the "Namespace PrintReport" as well as the "Class app" portions of the example.

Put a test button on your main form, and in the click even place this code:

    Dim pe As New PrintExample()
    ' The name of the printer should be added here;
    ' this could be a local or network printer.
    pe.PrintReport("PrinterName")

Add a new class module to your application and cut-and-paste the entire "PrintExample" class.

At the top of the new class module, include all of the "Imports" except for "PrintReport.reportserver".

0
 
LVL 1

Author Comment

by:talbers_bci
ID: 24134407
I am using a new console application, i added the copied code to a new class form.  This may be incorrect, I don't have very much experience with console applications.  
0
 
LVL 11

Expert Comment

by:CraigYellick
ID: 24134478
Unless you have a reason to test this with a console application you are better off adding a new class to your existing WinForms application. It already has all of the System.Drawing-related components referenced and ready to go.

If you want to keep using a console app you'll have to add references to System.Drawing and System.Web.Services.  Disregard the "class app" in the example and copy-paste the body of the Main procedure from the example into your console app's "Sub Main" procedure.
0
 
LVL 1

Author Comment

by:talbers_bci
ID: 24134483
Ok, I'll try that and let you know.
0
 
LVL 11

Expert Comment

by:CraigYellick
ID: 24134486
I just noticed that the automatic conversion routine that I used did not handle the callback delegate properly. In the line below, it was missing the "AddressOf" modifier.

m_delegate = New Graphics.EnumerateMetafileProc(AddressOf MetafileCallback)
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 1

Author Comment

by:talbers_bci
ID: 24134586
OK, I put this into my windows application, added a class and pasted the entire conversion, deleted the namespace and app, commented out the imports printreport.  I still have a number of errors.

errors.JPG
0
 
LVL 1

Author Comment

by:talbers_bci
ID: 24134614
I have cleaned up some of the errors, here a few lines that are still giving me trouble.

Private rs As ReportingService  -  ReportingService Not defined

Dim warnings As Warning() = Nothing  -  warning not defined

Dim reportHistoryParameters As ParameterValue() = Nothing    -   Parametervalue not defined

pd.PrintPage += New PrintPageEventHandler(AddressOf Me.pd_PrintPage)    - 'Public Event PrintPage(sender As Object, e As System.Drawing.Printing.PrintPageEventArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.      
0
 
LVL 11

Accepted Solution

by:
CraigYellick earned 500 total points
ID: 24135354
ReportingService is the name of the web service proxy, as defined when you add a web reference to your project.

The warnings and reportHistoryParameters are not used. I'm not sure where C# is getting the types but you should be able to define them as object arrays.

      Dim warnings() As Object = Nothing
      Dim reportHistoryParameters() As Object = Nothing

The pd.PrintPage syntax should be:

        ' Print report
        Console.WriteLine("Printing report...")
        AddHandler pd.PrintPage, AddressOf Me.pd_PrintPage
        pd.Print()

0
 
LVL 1

Author Comment

by:talbers_bci
ID: 24139040
Yeah, it is working.   I will award you the points for this but it is doing something strange with the scale.  I can start a new question if you would like.  

The prints that I am getting are scaled up in size compared to the print from a web browser.  This is not a big deal unless it scales it up so much that it rolls one page into two pages.  I don't have time to look at the code to see if there is a way to set the scale, my desk is piling up with items that need attention.  But I will look at it later.  If you have any suggestions that would be great.

Thanks for all your help and patience,  it has been greatly appreciated.

Todd
0
 
LVL 11

Expert Comment

by:CraigYellick
ID: 24139089
The article you referenced in your question has a long series of comments and I think I recall seeing someone posting adjustments to the author's code to handle scaling.

If you are thinking that this entire process seems insanely complicated just to print a report, I totally agree! I had no idea it would be this complex. I would have assumed Microsoft had a method or helper class that would accomplish all of this without so much fuss.
0
 
LVL 1

Author Comment

by:talbers_bci
ID: 24140918
Yeah, I was expecting to be able to use the report viewer, or something similar to the crystal report and go me.reportviewer.print  or something to that effect
0
 
LVL 1

Author Closing Comment

by:talbers_bci
ID: 31569699
I have the program up and running, even a console version.  The true answer uses all of the comments.  

I really appreciate your help, this has been a real pain to figure out.  It would be nice if Microsoft made it easier but at least it is now working.
0
 

Expert Comment

by:mwidholm
ID: 24661060
Thanks for this thread.  I followed the same methodology to get it into VB.net and am having some errors I haven't been able to resolve.

The biggest one is that my call to the ReportingService web service is giving me a "Type expected" error.  Would you be able to post the code you ended up with on this thread?

Also, I opened a question on this:
http://www.experts-exchange.com/Database/Reporting_/Q_24503770.html
0
 
LVL 1

Author Comment

by:talbers_bci
ID: 24661522
I posted a code sample to the question above.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This code started out as a fix for a customer that had incoming data that was hunderds of numbers and words long that was to fit in one column. The problem was that the customer did not want to split words or numbers when wrapping in the column. …
Introduction Earlier I wrote an article about the new lookup functions (http://www.experts-exchange.com/A_3433.html) that ship with SQL Server 2008 R2.  In this article I’m going to show you another new feature of SSRS 2008 R2, this time in the vis…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now