Monday, December 14, 2009

how to Pass Parameter values for Crystal Report in VB.Net?

Written one Common Function:

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
        Try

            myReportDocument = New ReportDocument
            myReportDocument.Load(path)
            myReportDocument.SetDatabaseLogon(System.Configuration.ConfigurationManager.AppSettings("UserName").ToString(), System.Configuration.ConfigurationManager.AppSettings("Pwd").ToString())
            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))
            Next
            ParamFldDefn = myReportDocument.DataDefinition.ParameterFields
            ParamFldDefn.Reset()
            Dim tmpCount As Integer = 0
            sReportViewer.ReportSource = myReportDocument
            'sReportViewer.DataBind()
            sReportViewer.EnableParameterPrompt = False
            For tmpCount = 0 To tmpSortList.Count - 1
                myReportDocument.SetParameterValue(ParamFldDefn(tmpCount).Name, tmpSortList(ParamFldDefn(tmpCount).Name).ToString())
            Next
            Return myReportDocument
            'fnToolbarSetting()


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

Calling the above function:



path = Server.MapPath(".\AgtCancelingList.rpt")
            ParamFld = "@AgentCode*@FrmTranDate*@FrmToDate*AgentName*DisplyFr*DisplyTo"
            ParamVal = AgtCode.ToString() + "*" + TfrmDate + "*" + TToDate + "*" + AgtCode + "-" + AgtName + "*" + frmDate + "*" + TDate
            Dim tmp As ReportDocument = clsCommon.ShowReport(path, ParamFld, ParamVal, CRViewer)
@AgentCode - Store Procedure Parameter Field
AgentName - Crystal Report Parameter Field.

No comments:

Post a Comment