Click here to Skip to main content
12,406,687 members (60,296 online)
Click here to Skip to main content
Add your own
alternative version


115 bookmarked

How to Load and Display Crystal Reports in VB.NET

, 6 Mar 2005
Rate this:
Please Sign up or sign in to vote.
An article about how to load and display Crystal Reports and handle the parameters and logon information.


The following code shows how to load Crystal Reports in VB.NET, solving all the issues of logon, including sub reports and parameter passing. You can view your reports by simply calling the required functions with its parameters.

Using the Code

Using this code in your application is very simple. In the first step you need to add a form, and name it frmViewReport, then place the Crystal Report Viewer control on the form and name it rptViewer. In the code section, simply paste the following function. You can call this function from anywhere in your application with reference to the frmViewReport form. The sample code for calling the function is given below:

Dim objForm As New frmViewReport
objForm.ViewReport("C:\test.rtp", , "@parameter1=test¶mter2=10")

Now let me explain you in detail what is going on in the code and what the format of the parameter string is. If there are parameters in the Crystal Reports then they should be passed with their values to the param of the function. The parameter string should be in the following format:

<First Parameter Name>=<First Paramter Value>&
<Second Parameter Name>=<Second Paramter Value>..

Note the parameter name and its value pairs are separated by an '&'. The report name with its full path should be passed to sReportName function.

Following is the function code with comments. I hope there will be no problem in understanding the code. Even if there is anything bothering you then drop me a message I'll explain that:

Friend Function ViewReport(ByVal sReportName As String, _
    Optional ByVal sSelectionFormula As String = "", _
    Optional ByVal param As String = "") As Boolean

    'Declaring variablesables
    Dim intCounter As Integer
    Dim intCounter1 As Integer

    'Crystal Report's report document object
    Dim objReport As New _

    'object of table Log on info of Crystal report
    Dim ConInfo As New CrystalDecisions.Shared.TableLogOnInfo

    'Parameter value object of crystal report 
    ' parameters used for adding the value to parameter.
    Dim paraValue As New CrystalDecisions.Shared.ParameterDiscreteValue

    'Current parameter value object(collection) of crystal report parameters.
    Dim currValue As CrystalDecisions.Shared.ParameterValues

    'Sub report object of crystal report.
    Dim mySubReportObject As _

    'Sub report document of crystal report.
    Dim mySubRepDoc As New _

    Dim strParValPair() As String
    Dim strVal() As String
    Dim index As Integer


        'Load the report

        'Check if there are parameters or not in report.
        intCounter = objReport.DataDefinition.ParameterFields.Count

        'As parameter fields collection also picks the selection 
        ' formula which is not the parameter
        ' so if total parameter count is 1 then we check whether 
        ' its a parameter or selection formula.

        If intCounter = 1 Then
            If InStr(objReport.DataDefinition.ParameterFields(_
                0).ParameterFieldName, ".", CompareMethod.Text) > 0 Then
                intCounter = 0
            End If    
        End If

        'If there are parameters in report and 
        'user has passed them then split the 
        'parameter string and Apply the values 
        'to their concurrent parameters.

        If intCounter > 0 And Trim(param) <> "" Then
            strParValPair = param.Split("&")

            For index = 0 To UBound(strParValPair)
            If InStr(strParValPair(index), "=") > 0 Then
                strVal = strParValPair(index).Split("=")
                paraValue.Value = strVal(1)
                currValue = _ 
            End If
    End If

    'Set the connection information to ConInfo 
    'object so that we can apply the 
    'connection information on each table in the report
    ConInfo.ConnectionInfo.UserID = <User Name>
    ConInfo.ConnectionInfo.Password = <Password>
    ConInfo.ConnectionInfo.ServerName = <Server Name>
    ConInfo.ConnectionInfo.DatabaseName = <Database Name>

    For intCounter = 0 To objReport.Database.Tables.Count - 1

    ' Loop through each section on the report then look 
    ' through each object in the section
    ' if the object is a subreport, then apply logon info 
    ' on each table of that sub report

        For index = 0 To objReport.ReportDefinition.Sections.Count - 1
            For intCounter = 0 To _
                index).ReportObjects.Count - 1
            With objReport.ReportDefinition.Sections(index)
                If .ReportObjects(intCounter).Kind = _
                CrystalDecisions.Shared.ReportObjectKind.SubreportObject Then
                    mySubReportObject = CType(.ReportObjects(intCounter), _
                    mySubRepDoc = _ 
                 For intCounter1 = 0 To mySubRepDoc.Database.Tables.Count - 1
                 End If
             End With
    'If there is a selection formula passed to this function then use that
    If sSelectionFormula.Length > 0 Then
        objReport.RecordSelectionFormula = sSelectionFormula
    End If
    'Re setting control 
    rptViewer.ReportSource = Nothing

    'Set the current report object to report.
    rptViewer.ReportSource = objReport

    'Show the report
        Return True
    Catch ex As System.Exception
    End Try
End Function

Points of Interest

The main problems while developing the function were the sub reports and the selection formula. When there were no parameters and only the selection formula, the function failed because the parameters collection was picking the selection formula. Initially it was hard to detect, but finally I solved that problem.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Danish Alam
Pakistan Pakistan
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralRe: crystal report report Pin
Amila thennakoon9-Oct-11 23:06
memberAmila thennakoon9-Oct-11 23:06 
QuestionHow to pass parameters in Crystal Report Pin
Ashvin Johnson11-Sep-08 4:05
memberAshvin Johnson11-Sep-08 4:05 
GeneralParameters not set Pin
JerryElbow4-Sep-08 8:48
memberJerryElbow4-Sep-08 8:48 
GeneralLicense Question Pin
thumb2216-Apr-08 10:53
memberthumb2216-Apr-08 10:53 
GeneralExcellent!!! Pin
JShow15-Apr-08 6:16
memberJShow15-Apr-08 6:16 
GeneralRe: Excellent!!! Pin
edwarmartinez13-Aug-08 15:34
memberedwarmartinez13-Aug-08 15:34 
GeneralCrystal Report Download Pin
DennisKeeling21-Nov-07 2:26
memberDennisKeeling21-Nov-07 2:26 
GeneralParameter value not set [modified] Pin
Muhammad Shafiq16-Nov-07 3:57
memberMuhammad Shafiq16-Nov-07 3:57 
GeneralConfirmed, this sample does not work [modified] Pin
AliasGoesHere5-Nov-07 7:14
memberAliasGoesHere5-Nov-07 7:14 
GeneralRe: Confirmed, this sample does not work Pin
AliasGoesHere21-Nov-07 11:00
memberAliasGoesHere21-Nov-07 11:00 
GeneralRe: Confirmed, this sample does not work Pin
BruceL14-Mar-08 9:08
memberBruceL14-Mar-08 9:08 
GeneralRe: Confirmed, this sample does not work - Fix for BADINDEX Error Pin
BruceL14-Mar-08 9:35
memberBruceL14-Mar-08 9:35 
GeneralRe: Confirmed, this sample does not work - Fix for BADINDEX Error Pin
nfalk9-Jun-09 3:54
membernfalk9-Jun-09 3:54 
GeneralRe: Confirmed, this sample does not work Pin
HeinD23-Mar-09 10:01
memberHeinD23-Mar-09 10:01 
GeneralRe: Confirmed, this sample does not work - Another way to fix this Pin
Jeanjeanot4-May-09 1:27
memberJeanjeanot4-May-09 1:27 
GeneralRe: Confirmed, this sample does not work - Another way to fix this Pin
Rene Samuel11-May-09 17:28
memberRene Samuel11-May-09 17:28 
QuestionThe Crystal Report Viewer control Pin
snakewine17-Oct-07 11:59
membersnakewine17-Oct-07 11:59 
QuestionLoad report from repository? Pin
Brichowski4-Sep-07 0:00
memberBrichowski4-Sep-07 0:00 
GeneralCrystal Report and VB Express Edition Pin
tutzkiE1-Sep-07 2:43
membertutzkiE1-Sep-07 2:43 
GeneralGood but not display any data Pin
Hetkeval12-Aug-07 12:34
memberHetkeval12-Aug-07 12:34 
QuestionParamater Error Pin
MackiaRSA8-Aug-07 20:52
memberMackiaRSA8-Aug-07 20:52 
Generalnotes on VB Pin
mpyanap16-Jul-07 5:55
membermpyanap16-Jul-07 5:55 
Generalexcellent code Pin
ria1712-Jul-07 20:04
memberria1712-Jul-07 20:04 
Generalexcellnt code Pin
ria1729-Jun-07 1:22
memberria1729-Jun-07 1:22 
Questioncrystal report in Pin
ria1728-Jun-07 20:28
memberria1728-Jun-07 20:28 
AnswerRe: crystal report in Pin
snakewine18-Oct-07 4:46
membersnakewine18-Oct-07 4:46 
Generaldim objform as new frmViewReport Pin
ggill28-Jun-07 8:29
memberggill28-Jun-07 8:29 
GeneralLogon Details Pin
cbmarsh21-Jun-07 12:47
membercbmarsh21-Jun-07 12:47 
GeneralParameter problem Pin
Brian M113-Jun-07 10:28
memberBrian M113-Jun-07 10:28 
GeneralRe: Parameter problem Pin
Kapil_Chicago15-Aug-07 9:16
memberKapil_Chicago15-Aug-07 9:16 
QuestionData From Microsoft Access Pin
snare888-May-07 10:13
membersnare888-May-07 10:13 
GeneralPassing a parameter throu 2003 Pin
ThorgalRose4-May-07 3:37
memberThorgalRose4-May-07 3:37 
QuestionHow to use Results displayed in the form in crystal reports Pin
Min8126-Apr-07 19:48
memberMin8126-Apr-07 19:48 
QuestionCrystal Reports Parameter Passing - Visual Studio 2005 Pin
HeliumHearts15-Apr-07 20:26
memberHeliumHearts15-Apr-07 20:26 
QuestionHow to pass selection criteria Pin
RAJESH M5-Apr-07 22:43
memberRAJESH M5-Apr-07 22:43 
AnswerRe: Thanks but one question Pin
RAJESH M6-Apr-07 19:02
memberRAJESH M6-Apr-07 19:02 
QuestionHelp me: "Load Report Failed" Pin
dougcordova4-Mar-07 5:32
memberdougcordova4-Mar-07 5:32 
AnswerRe: Help me: "Load Report Failed" Pin
thatreallybytes18-Jun-07 9:30
memberthatreallybytes18-Jun-07 9:30 
GeneralThank you Pin
vnkt_ravi25-Feb-07 1:50
membervnkt_ravi25-Feb-07 1:50 
Questionhelp to pass parameter Pin
neo_bags5-Feb-07 0:21
memberneo_bags5-Feb-07 0:21 
AnswerRe: help to pass parameter Pin
vnkt_ravi25-Feb-07 1:52
membervnkt_ravi25-Feb-07 1:52 
QuestionParameterFields CurrentValue already has a value Pin
Coding1234564-Feb-07 20:26
memberCoding1234564-Feb-07 20:26 
GeneralParameter not passing! [modified] Pin
bazpaul25-Jan-07 3:31
memberbazpaul25-Jan-07 3:31 
GeneralRe: Parameter not passing! Pin
MAlcala15-Aug-07 21:20
memberMAlcala15-Aug-07 21:20 
Generalhelp me with this error "Buffer too small for string or missing null byte" Pin
osbeng4-Jan-07 4:57
memberosbeng4-Jan-07 4:57 
QuestionHow to pass the value in parameter Pin
Member #36841943-Jan-07 23:17
memberMember #36841943-Jan-07 23:17 
AnswerRe: How to pass the value in parameter Pin
Danish Alam4-Jan-07 0:07
memberDanish Alam4-Jan-07 0:07 
AnswerRe: How to pass the value in parameter Pin
asm20014-Jan-07 1:54
memberasm20014-Jan-07 1:54 
Generalcrystal report :every record displayed in new page Pin
Member #341340829-Dec-06 19:37
memberMember #341340829-Dec-06 19:37 
GeneralFriend Pin
shakti538519-Dec-06 1:35
membershakti538519-Dec-06 1:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160730.1 | Last Updated 6 Mar 2005
Article Copyright 2005 by Danish Alam
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid