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

Posted on 2008-11-11
Last Modified: 2012-05-05

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

Question by:geraintcollins
    LVL 100

    Expert Comment


    Author Comment

    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.
    LVL 100

    Expert Comment

    Unfortunately I am not web savvy.  Very good with the reports but outside of VB6 applications, I don't know much.


    Accepted Solution

    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

    LVL 100

    Expert Comment


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


    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
    Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    755 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

    24 Experts available now in Live!

    Get 1:1 Help Now