Introduction
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")
objForm.show()
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
Dim intCounter As Integer
Dim intCounter1 As Integer
Dim objReport As New _
CrystalDecisions.CrystalReports.Engine.ReportDocument
Dim ConInfo As New CrystalDecisions.Shared.TableLogOnInfo
Dim paraValue As New CrystalDecisions.Shared.ParameterDiscreteValue
Dim currValue As CrystalDecisions.Shared.ParameterValues
Dim mySubReportObject As _
CrystalDecisions.CrystalReports.Engine.SubreportObject
Dim mySubRepDoc As New _
CrystalDecisions.CrystalReports.Engine.ReportDocument
Dim strParValPair() As String
Dim strVal() As String
Dim index As Integer
Try
objReport.Load(sReportName)
intCounter = objReport.DataDefinition.ParameterFields.Count
If intCounter = 1 Then
If InStr(objReport.DataDefinition.ParameterFields(_
0).ParameterFieldName, ".", CompareMethod.Text) > 0 Then
intCounter = 0
End If
End If
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 = _
objReport.DataDefinition.ParameterFields(_
strVal(0)).CurrentValues
currValue.Add(paraValue)
objReport.DataDefinition.ParameterFields(_
strVal(0)).ApplyCurrentValues(currValue)
End If
Next
End If
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
objReport.Database.Tables(intCounter).ApplyLogOnInfo(ConInfo)
Next
For index = 0 To objReport.ReportDefinition.Sections.Count - 1
For intCounter = 0 To _
objReport.ReportDefinition.Sections(_
index).ReportObjects.Count - 1
With objReport.ReportDefinition.Sections(index)
If .ReportObjects(intCounter).Kind = _
CrystalDecisions.Shared.ReportObjectKind.SubreportObject Then
mySubReportObject = CType(.ReportObjects(intCounter), _
CrystalDecisions.CrystalReports.Engine.SubreportObject)
mySubRepDoc = _
mySubReportObject.OpenSubreport(mySubReportObject.SubreportName)
For intCounter1 = 0 To mySubRepDoc.Database.Tables.Count - 1
mySubRepDoc.Database.Tables(_
intCounter1).ApplyLogOnInfo(_
ConInfo)sp;
mySubRepDoc.Database.Tables(_
intCounter1).ApplyLogOnInfo(ConInfo)
Next
End If
End With
Next
Next
If sSelectionFormula.Length > 0 Then
objReport.RecordSelectionFormula = sSelectionFormula
End If
rptViewer.ReportSource = Nothing
rptViewer.ReportSource = objReport
rptViewer.Show()
Return True
Catch ex As System.Exception
MsgBox(ex.Message)
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.