• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1790
  • Last Modified:

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 studio.net 2005).  Expert assistance is required.

<%@ webservice language="VB" class="CrystalReportService" %>
Imports System
Imports System.data
Imports System.data.sqlclient
Imports System.Data.OleDb
Imports System.Web.Services
Imports CrystalDecisions.Shared
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.ReportSource
Imports CrystalDecisions.Web.Services
< WebService( Namespace:="http://crystaldecisions.com/reportwebservice/9.1/" ) > _
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

  • 3
  • 2
1 Solution
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.data.sqlclient
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


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


Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now