Saturday, October 15, 2011

VB.Net 2010 and Crystal Report for .Net Step by Step Tutorial



Add New Windows Form


Choose Crystal Report


Click Close when Crystal Report Gallery Appears

Then, goto- Project- Project Name Properties

Click on Compile-Advanced Compile Options


Change Target Framework to .Net Framework 4 - click OK
Go back to your form-scroll down to Reporting Toolbox- and choose - CrystalReportViewer

- then define the area for your crystal report viewer-
name the report viewer "crviewer" or any name relevant to your report.

goto to solution Explorer-right click App.config-click open


click the image below and zoom-in, then type the highlighted code exactly as seen





paste the code below on your module

Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared

Module Module_Name_Here

Friend RptDataset As New DataSet
Friend RptTbl As String

'RptFile will contain the path and the name of the crystal report you design
'Example: Application.Startup & "\reports\rptfile.rpt
Friend RptFile As String


End Module

'end of module


'frmcrypt_report

Private Sub frmcrypt_report_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim CryRpt As New CrystalDecisions.CrystalReports.Engine.ReportDocument

CryRpt.Load(RptFile)

'the code below is a must to refresh your dataset
RptDataset.Reset()

'use your preferred code to dynamically query your database. the code is for mysql using
'a function that i created to query my database

da = MySelectRows(Mycon, sqlstring, RptTbl)
da.Fill(RptDataset, RptTbl)

'if you are to connect to a server use the code below to login to your server
'it is a must to login to your server.

CryRpt.DataSourceConnections.Item(0).SetConnection("127.0.0.1", "dbname", True)
CryRpt.DataSourceConnections.Item(0).SetLogon("userid", "password")

'end of login to server code

CryRpt.SetDataSource(RptDataset.Tables(RptTbl))

crviewer.ReportSource = CryRpt
crviewer.Refresh()

End Sub



'Code from the form which will call your crystal report form

RptFile = Application.StartupPath & "\REPORT\rptfile.rpt"
RptTbl = "table_name"
frmcrypt_report.ShowDialog()

0 comments: