Sunday, November 12, 2006

Creating Crystal Reports in Asp.NET

1. Create a typed dataset. Add a Crystal report in your project and bing it to this dataset.
2. create a class file which gets this crystal report.

here is a class code file...

Imports Microsoft.VisualBasic
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.ReportSource
Imports CrystalDecisions.Shared
Imports System
Imports System.ComponentModel

Public Class rptMain
Inherits ReportClass


Public Sub New()
MyBase.New()
End Sub


Public Overrides Property ResourceName() As String
Get
Return "rptMain.rpt" ' This is the Report Name
End Get
Set(ByVal value As String)
'Do nothing
End Set
End Property


End Class

Now, calling this Report in your main .aspx page

here is the code: on .aspx page add a crystalViewer component. take it from toolbox


Dim conn As New SqlConnection("Server=localhost;database=ranet;uid=sa;pwd=dbserver;")

Dim rptMain1 As New rptMain - create object of the class u made
Dim Dsmain1 As New DSMain - object of typed dataset
Try
Dim adpAdapter As New SqlDataAdapter("Select * from tblEmployees where intEmployeeID=131", conn)
adpAdapter.Fill(Dsmain1, Dsmain1.Tables(0).TableName) ' in asp 2.0 u have 2 mention
'Dsmain1.Tables(0).TableName
rptMain1.SetDataSource(Dsmain1)
rptMain1.SetDatabaseLogon("sa", "11global34", "mgschd", "TNCIntranet")
CrystalReportViewer1.ReportSource = rptMain1

Response.Write(" File is created. ")
Catch err As Exception
Response.Write(err.Message)
Finally
End Try

*************************************************************

I hope this will help every 1.

Thanks and Regards,
Dev