Friday, January 20, 2012

code for crystal report

Visual Studio 2005 and Crystal Report XI.

1. Create a Crystal Report as usual in Visual studio 2005 .net
using ODBC connection. In this example "CrystalReport.rpt"

2. Create a aspx web form to display the report. This form will
be a place holder for crystal report.

3. In design mode, drag and drop Crystal Report viewer control and
don't configure anything.

4. Open code behind .aspx.vb

'Declare public variable

Private myReportDocument As ReportDocument

'Note: This is Page Init event not Page Load event

Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
Dim myConnectionInfo As ConnectionInfo = New ConnectionInfo()

' Note: This is your local ODBC Name NOT SQL SERVER NAME

myConnectionInfo.ServerName = "PUBSODBCNAME"

' If your ODBC connection defaulted to your application database,
' you can remove the following line.

myConnectionInfo.DatabaseName = "Pubs"
myConnectionInfo.UserID = "userid"
myConnectionInfo.Password = "password"

myReportDocument = New ReportDocument()
'If your crystal report is in some other folder, give proper path
' In this example, .rpt file in the same folder as .aspx file
Dim reportPath As String = Server.MapPath("CrystalReport.rpt")
SetDBLogonForReport(myConnectionInfo, myReportDocument)
CrystalReportViewer1.ReportSource = myReportDocument
' You can customize the Crystal Reports toolbar.
' like the following. I have disabled the business object logo
' in the tool bar.

CrystalReportViewer1.HasCrystalLogo = False

End Sub

'This Sub is necessary for Crystal report database connection

Private Sub SetDBLogonForReport(ByVal myConnectionInfo As ConnectionInfo, ByVal myReportDocument As ReportDocument)
Dim myTables As Tables = myReportDocument.Database.Tables
For Each myTable As CrystalDecisions.CrystalReports.Engine.Table In myTables
Dim myTableLogonInfo As TableLogOnInfo = myTable.LogOnInfo
myTableLogonInfo.ConnectionInfo = myConnectionInfo
5. Now run the .aspx page. You will see the report or the crystal report parameter
form. It will not ask you DB login information.

6. dont forget to Imports dll

Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Imports CrystalDecisions.ReportSource

No comments:

Post a Comment