How to pass parameters from VB.Net to Crystal Reports

To pass parameters from VB.Net to Crystal Reports

1.use SortedList for storing parameters and values
2. ParameterFieldDefinitions class is used to assign the values to crystal report parameters
3. The below function accepts four arguments , path is crystal report (rpt file path) , parameter field , value for the parameters and report viewer control

common function written

Dim ParamFldDefn As ParameterFieldDefinitions
Dim myReportDocument As ReportDocument

Public Function ShowReport(ByVal path As String, ByVal sParamFld As String, ByVal sParamVal As String, ByVal sReportViewer As CrystalDecisions.Web.CrystalReportViewer) As ReportDocument
Dim tmpSortList As New SortedList
Dim ParamLength As Int16 = 0, iLoop As Int16 = 0
Dim ParamFld() As String, ParamFldVal() As String

myReportDocument = New ReportDocument

myReportDocument.SetDatabaseLogon(user, pwd, Servername, Database)
Dim Logoninfo As New TableLogOnInfo()
Logoninfo.ConnectionInfo.ServerName = Servername
Logoninfo.ConnectionInfo.DatabaseName = Database
Logoninfo.ConnectionInfo.UserID = user
Logoninfo.ConnectionInfo.Password = pwd



ParamFld = sParamFld.Split("*")
ParamFldVal = sParamVal.Split("*")
If ParamFld.Length <> ParamFldVal.Length Then
Err.Raise(1, , "Parameter Fleid should be equal to Parameter Value")

End If
For iLoop = 0 To UBound(ParamFld)
tmpSortList.Add(ParamFld(iLoop), ParamFldVal(iLoop))
ParamFldDefn = myReportDocument.DataDefinition.ParameterFields
Dim tmpCount As Integer = 0
sReportViewer.ReportSource = myReportDocument
sReportViewer.EnableParameterPrompt = False
For tmpCount = 0 To tmpSortList.Count - 1
myReportDocument.SetParameterValue(ParamFldDefn(tmpCount).Name, tmpSortList(ParamFldDefn(tmpCount).Name).ToString())
Return myReportDocument

Catch ex As Exception
' lblErr.Text = ex.Message
Return Nothing
End Try
End Function

SetDatabaseLogon method to assign the database details retrieve from webconfig

Dim Database As String = System.Configuration.ConfigurationManager.AppSettings("DataBase").ToString()
Dim Servername As String = System.Configuration.ConfigurationManager.AppSettings("Server").ToString()
Dim user As String = System.Configuration.ConfigurationManager.AppSettings("UserName").ToString()
Dim pwd As String = System.Configuration.ConfigurationManager.AppSettings("Pwd").ToString()

To call the above function

path = Server.MapPath("Reports.rpt")
ParamFld = "@AgentCode*@FrmTranDate*@FrmToDate*@Flag*@Product*AgtName*DisplayFr*DisplayTo"
ParamVal = AgtCode.ToString() + "*" + TfrmDate + "*" + TToDate + "*" + flag.ToString() + "*" + ProductCode + "*" + AgtName + "*" + frmDate + "*" + TDate
Dim tmp As ReportDocument = clsCommon.ShowReport(path, ParamFld, ParamVal, cvReport)


No responses found. Be the first to comment...

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: