Create and call a web service that exports a crystal report to pdf


I'd like to create a web service that exports a crystal report to pdf.  I've attached some sample code that I think should do this but I'm not sure how to call it from my web application or even if my code is correct.

I'm using ASP.Net and VB.Net (visual 2005).  Expert assistance is required.

<%@ webservice language="VB" class="CrystalReportService" %>
Imports System
Imports System.Data.OleDb
Imports System.Web.Services
Imports CrystalDecisions.Shared
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.ReportSource
Imports CrystalDecisions.Web.Services
< WebService( Namespace:="" ) > _
Public Class CrystalReportService
    Inherits ReportServiceBase
    Public Sub New()
        Dim MyConnection As New OleDbConnection
        Dim MyCommand As New OleDbCommand
        Dim MyDA As New OleDbDataAdapter
        Dim myDS As New Data.DataSet
        MyConnection.ConnectionString = "Data Source=speedy;Initial Catalog=Northwind;Integrated Security=True;providerName=System.Data.SqlClient"
        MyCommand.Connection = MyConnection
        MyCommand.CommandText = "Select * from Customers"
        MyCommand.CommandType = Data.CommandType.Text
        MyDA.SelectCommand = MyCommand
        MyDA.Fill(myDS, "Customers")
        Dim rpt As New ReportDocument
        rpt.ExportToDisk(ExportFormatType.PortableDocFormat, "c:\test.pdf")
    End Sub
End Class

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

geraintcollinsAuthor Commented:
Thanks for the response mimcc, unfortunately  I have this file alreadyand found that it does not address my needs.  I'm looking for some feedback into the code I've attached and how to call it.
Unfortunately I am not web savvy.  Very good with the reports but outside of VB6 applications, I don't know much.

geraintcollinsAuthor Commented:
Ok I've sorted this myself and for anyone in the same boat as me that's lost some hair over this one this is how I did it (see attached code).  The trick is to know that everything report related goes in the web service and having the required dlls installed on the web server hosting the web service.

Web Service:
Imports System.Data
Imports System.Diagnostics.Debug
Imports System.Web.Configuration.WebConfigurationManager
Imports System.IO
Imports System.Data.OleDb
Imports System.Configuration
Imports System.Web.Services
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Imports System.Web.UI.WebControls
Public Class ClassNameHere
    Dim myDataSet As New DataSet()
    Private Function GetDataSet(ByVal strSQL As String, ByVal sTableName As String) As DataSet
        '1. Create a connection
        Dim myConnection As New SqlConnection
        myConnection.ConnectionString = ConnectionStrings("WEARD").ConnectionString
        '2. Create the command object, passing in the SQL string    
        Dim myCommand As New SqlCommand(strSQL, myConnection)
        '3. Create the DataAdapter
        Dim myDataAdapter As New SqlDataAdapter()
        myDataAdapter.SelectCommand = myCommand
        '4. Populate the DataSet and close the connection
        myDataAdapter.Fill(myDataSet, sTableName)
        'Return the DataSet
        Return myDataSet
    End Function
    <WebMethod()> Public Function GetCustomerPDF(ByVal CustomerID As Integer) As Byte()
        Dim sSQL As String
        Dim rpt As New ReportDocument
        Dim st As Stream
        Dim myDs As New DataSet
        sSQL = "Select * from viewname where CustomerID = " & CustomerID 
        myDS = GetDataSet(sSQL, tablename)
       sSQL = "Select * from secondtable where customerID = " & customerid
        myDs = GetDataSet(sSQL, "secondtable")
        st = rpt.ExportToStream(ExportFormatType.PortableDocFormat)
        Dim arr(st.Length) As Byte
        st.Read(arr, 0, Int(st.Length))
        Return arr
    End Function
End Class
Web Site code: (add the web reference in solution explorer refd as webservicename below)
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Create an instance of the Proxy class
        Dim consumeWebService As New webservicename.classname
        consumeWebService.PreAuthenticate = True
        consumeWebService.Credentials = System.Net.CredentialCache.DefaultCredentials
        'Bind the results of GetFAQsInCategory to dgCategoryFAQs
        Dim buffer() As Byte
        buffer = consumeWebService.GetCustomerPDF(531)
        'Display Contents on web form
        Response.ContentType = "application/pdf"
        Response.OutputStream.Write(buffer, 0, buffer.Length)
    End Sub
The following dlls must be installed and registered on the web server that hosts the web service, just make sure you know the right version.
The following lines are required in the web service web config make sure you have the right version no:
				<add assembly="CrystalDecisions.CrystalReports.Engine, Version=10.0.3300.0, Culture=neutral, PublicKeyToken=692FBEA5521E1304"/>
				<add assembly="CrystalDecisions.ReportSource, Version=10.0.3300.0, Culture=neutral, PublicKeyToken=692FBEA5521E1304"/>
				<add assembly="CrystalDecisions.Shared, Version=10.0.3300.0, Culture=neutral, PublicKeyToken=692FBEA5521E1304"/>
				<add assembly="CrystalDecisions.Web, Version=10.0.3300.0, Culture=neutral, PublicKeyToken=692FBEA5521E1304"/>

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial

Click the DELETE QUESTION link in your question and follow the prompts to accept your last cimment as the answer

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.