ARTICLE

Accessing Crystal Report in ASP.NET using VB.Net

Posted by Munir Shaikh Articles | Crystal Reports in VB.NET December 11, 2006
This article is for web-developers who want to access crystal reports in ASP.Net by using multiple tables or views etc. This application might help them and save their development time.
Download Files:
 
Reader Level:

A web developer may or may not be aware of crystal report in depth as compared to desktop application developer.

Couple of months back I was suppose to access crystal reports in asp.net, but I found many problems while working with crystal report using crystalreportviewer in asp.net. I am writing this application for web-developers who want to access crystal reports in asp.net by using multiple tables or views etc. This application might help them and save their development time.

List of problems which I came across while accessing crystal reports in asp.net

  • Logon failure
    Refer to following URL
    http://support.businessobjects.com/library/kbase
    /articles/c2010371.asp
  • DLL not found
    On a development machine if crystal report is not installed then you might get above error
  • Fail to render page
    Basically there could be two possible ways for the solution
    1. This error can occur due to the unsuppressed report header, removing space between the header might solve the problem.
    2. This error can be solved by assigning permission to ASPNET user by default. ASPNET user does not have permission on windows XP and XP professional machine. To explain in detail it creates crystal report image in the local machine's temp directory or in the same directory where application is residing, due to security issue or permission problem. ASP.net unable to render image created in the memory and hence error occur.

Refer to following URL for the detail information

http://support.businessobjects.com/library/kbase/articles/c2013414.asp
http://support.businessobjects.com/library/kbase/articles/c2014843.asp
http://support.businessobjects.com/library/kbase/articles/c2014618.asp

Merge Modules

Once build is ready and wanted to deployed it on the development server. You should add merge module in the build. This is important because you never know that crystal report is installed on the development machine and it is not possible for you to provide crystal report to the client. You can include following modules in the build and it will take care of all the further issues of deployment on the server.

  • Crystal_managed2003.msm
  • Crystal_database_Access2003.msm
  • Crystal_datavase_Access2003.msm
  • Crystal_regwiz2003.msm

-- I have used Northwind database of SQL-SERVER
--View  Categorywise_products on product and categories table

create view ProductsList AS
SELECT Categories.CategoryName, Products.ProductName, Products.QuantityPerUnit, Products.UnitsInStock, Products.Discontinued
FROM Categories INNER JOIN Products ON Categories.CategoryID = Products.CategoryID
WHERE Products.Discontinued <> 1

To create a dataset object from a database

Create a new schema file in the project:

  1. In the Solution Explorer, right-click the project name, point to Add, and click Add New Item.
  2. In the Categories area of the Add New Item dialog box, expand the folder and select Data.
  3. In the Templates area, select Dataset.
  4. Accept the default name Dataset1.xsd.

This creates a new schema file (Dataset1.xsd) that will be used to generate a strongly-typed dataset. The schema file will be displayed in the ADO.NET Dataset Designer.

Specify where the database is located:

  1. In the Server Explorer, right-click Data Connections and select Add Connection.
  2. In the Data Link Properties dialog box, click the Provider tab and select a provider (for example, Microsoft OLE DB Provider for SQL Server).
  3. Click the Connection tab and specify the location of your database. Enter server and logon information where necessary.
  4. Click OK.

Your database, its tables, and its fields now appear in the Server Explorer under the Data Connections node.

In the Solution Explorer, double-click Dataset1.xsd, if it is not already the active view. Dataset1.xsd should now be displayed in the Dataset tab.

To build a schema for your dataset, drag the desired tables from the Server Explorer to the Dataset tab of Dataset1.xsd. Click Save Dataset1.xsd to save the Dataset1.xsd file. On the Build menu, click Build to generate the dataset object for the project.

There is another reason why I have created xsd (schema file) for report. Let us consider a situation where you want to deploy your application on client's server. Now situation is you have set location of crystal report to your development server and though you set logon information programmatically logon failure occurs. To overcome this problem, I have a view associated in schema file. I can set location of ADO.NET Dataset to the xsd view and this will take care of all the problems for setting location while runtime, since it is independent of any of the server and to access dataset we are applying separate connection string from the web.config file. This way you can get rid of all the problems to access crystal reports in asp.net.

Actual code as below

Imports System

Imports System.Collections

Imports System.ComponentModel

Imports System.Data

Imports System.Data.SqlClient

Imports System.Drawing

Imports System.Web

Imports System.Web.SessionState

Imports System.Web.UI

Imports System.Web.UI.WebControls

Imports System.Web.UI.HtmlControls

Imports CrystalDecisions.Shared

Imports CrystalDecisions.CrystalReports.Engine

Imports System.Configuration

Namespace CrystalReports

          ''' <summary>

          ''' Summary description for WebForm1.

          ''' </summary>        

Public Class WebForm1

    Inherits System.Web.UI.Page

    Protected CrystalReportViewer1 As CrystalDecisions.Web.CrystalReportViewer

    Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)

        ' Put user code to initialize the page here

    End Sub 'Page_Load

 

Protected Overrides Sub OnInit(ByVal e As EventArgs)

    '

    ' CODEGEN: This call is required by the ASP.NET Web Form Designer.

    '

    InitializeComponent()

    MyBase.OnInit(e)

    'Database connectivity

    Dim mycon As New SqlConnection(ConfigurationSettings.AppSettings("connectionString"))

    Dim dtbl As New DataTable

    Dim dtst As New DataSet

    Dim sqdt As SqlDataAdapter

    Try

        'Database activity

        sqdt = New SqlDataAdapter("SELECT * FROM ProductsList", mycon)

        sqdt.Fill(dtbl)

        dtst.Tables.Add()

        Dim crptProList As New productList

        crptProList.SetDataSource(dtst)

        CrystalReportViewer1.DisplayGroupTree = False

        CrystalReportViewer1.ReportSource = crptProList

       'Passing parameter to the crystal report

       'let us assume that i have added a parameter to the crystal report which accepts

       'categoryname and based on this generate report this parameter you can pass

       'from any web form as a request parameter

       'string catName = Request.QueryString["catageoryName"];

       'you can set parameter after setting report source as below

       'crptProList.SetParameteraValue("CatagoryName",catName) similarly you can set

       'multiple parameters to the crystal report

       'Overcomming problem of setting location at runtime -------------

       'Setting Logon Information--------------------------------------

     Catch ex As Exception

        Response.Write(ex.Message)

    End Try

End Sub

 

     ''' <summary>

     ''' Required method for Designer support - do not modify

     ''' the contents of this method with the code editor.

     ''' </summary>

Private Sub InitializeComponent()

End Sub 'InitializeComponent

 

End Class 'WebForm1

End Namespace 'CrystalReports

 

NOTE: THIS ARTICLE IS CONVERTED FROM C# TO VB.NET USING A CONVERSION TOOL. ORIGINAL ARTICLE CAN BE FOUND ON C# CORNER (http://www.c-sharpcorner.com/).

share this article :
post comment
 

Hello,

I have a form of VB.NET with displayed informations from a database; could you explain me how to print this form without preparing a report for that?

Thanks in advance!

Best regards,

Fritslet Louis

Posted by Fritslet Louis May 28, 2010

Dear y'all,

I am working on VB.NET 2005 and SQL Server 2005. I want to to open a doc in Ms word with a simple click on a VB.NET application form that I created. Could you help me, please!

Posted by Fritslet Louis May 28, 2010

i dont know the abc of crystal reports please tell me hpw to use

Posted by aryan t Oct 01, 2008

my asp.net application using crystal reports is creating mulitple .vb files. I have a report named cryExoneration.rpt and then there is a cryExoneration.vb. But there is now also a cryExoneration1.vb and a cryExoneration2.vb. This is creating amiguous class refer errors. If I delete cryExoneration1.vb and cryExoneration2.vb the application won't compile because those files are missing. I want to delete the references to those files but I don't know where they are referenced. Can someone please help me. Thanks Jim

Posted by Jim Berkes Feb 01, 2008

hello munir i am using this method u have stated but i won' t be able to create the object of the crystal report plz help shahid shaikh

Posted by shahid shaikh Dec 26, 2007
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Become a Sponsor