Click here to Skip to main content
15,920,438 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am new to using Crystal in VB.net. I have multiple parameters. Some of them have multiple values. I have viewed several sites with suggestions on how to pass these parameters from vb.net to the crystal report viewer and the report. Below is my code. I am not sure what is incorrect with it.

VB
Imports System.Data
    Imports CrystalDecisions.CrystalReports.Engine
    Imports CrystalDecisions.Shared
    Imports System.Data.OleDb

    Protected Sub cmdRunReport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdRunReport.Click
            Dim dtFromDate As Date
            Dim dtToDate As Date
            Dim strFromDate As String
            Dim strToDate As String
            Dim strDateRange As String
            Dim rptDoc As New ReportDocument

            Dim myPath As String
            Dim crUser As String
            Dim crPwd As String
            Dim crServer As String
            Dim crDatabase As String
            Dim bAll As String
            Dim iCnt As Integer

            txtMsg.Visible = False
            txtMsg.Text = Nothing

            crUser = ConfigurationManager.AppSettings("crUser").ToString
            crPwd = ConfigurationManager.AppSettings("crPwd").ToString
            crServer = ConfigurationManager.AppSettings("crServer").ToString
            crDatabase = ConfigurationManager.AppSettings("crDatabase").ToString

            If ddlReport.SelectedValue = -1 Then
                txtMsg.Text = "You must select a report to preview"
                txtMsg.Visible = True
                ddlReport.Focus()
                Exit Sub
            End If

            dtFromDate = Me.calFromDate.SelectedDate.Date
            If Not IsDate(dtFromDate) Then
                txtMsg.Text = "You must select a from date"
                txtMsg.Visible = True
                calFromDate.Focus()
                Exit Sub
            End If
            dtToDate = Me.calToDate.SelectedDate.Date
            If Not IsDate(dtToDate) Then
                txtMsg.Text = "You must select a to date"
                txtMsg.Visible = True
                calToDate.Focus()
                Exit Sub
            End If

            myPath = Nothing
            Select Case ddlReport.SelectedValue
                Case "106"
                    'Load the selected report file
                    myPath = Server.MapPath("Nav106.rpt")
            End Select

            Dim paramFields As ParameterFieldDefinitions
            Dim paramField1 As ParameterFieldDefinition
            Dim paramField2 As ParameterFieldDefinition
            Dim paramField3 As ParameterFieldDefinition
            Dim paramField4 As ParameterFieldDefinition
            Dim paramField5 As ParameterFieldDefinition
            Dim paramField6 As ParameterFieldDefinition
            Dim paramField7 As ParameterFieldDefinition
            Dim DiscreteVal As ParameterDiscreteValue
            Dim paramValues As ParameterValues

            'Load Report
            rptDoc.Load(myPath)

            paramFields = rptDoc.DataDefinition.ParameterFields

            strDateRange = "From " & dtFromDate.Date & " to " & dtToDate.Date
            paramField1 = paramFields.Item("DateRange")
            paramValues = paramField1.CurrentValues
            DiscreteVal = New ParameterDiscreteValue
            DiscreteVal.Value = strDateRange
            paramValues.Add(DiscreteVal)
            paramField1.ApplyCurrentValues(paramValues)

            Dim FromDate As String = Format(dtFromDate, "yyyyMMdd")
            strFromDate = FromDate
            paramField2 = paramFields.Item("FromDate")
            paramValues = paramField2.CurrentValues
            DiscreteVal = New ParameterDiscreteValue
            DiscreteVal.Value = strFromDate
            paramValues.Add(DiscreteVal)
            paramField2.ApplyCurrentValues(paramValues)

            Dim ToDate As String = Format(dtToDate, "yyyyMMdd")
            strToDate = ToDate

            paramField3 = paramFields.Item("ToDate")
            paramValues = paramField3.CurrentValues
            DiscreteVal = New ParameterDiscreteValue
            DiscreteVal.Value = strToDate
            paramValues.Add(DiscreteVal)
            paramField3.ApplyCurrentValues(paramValues)

            paramField4 = paramFields.Item("Locs")
            paramValues = paramField4.CurrentValues
            Select Case ddlReport.SelectedValue
                Case 106
                    bAll = False
                    iCnt = 0
                    For Each item As ListItem In lbLocs.Items
                        If item.Value = -1 Then
                            If item.Selected Then
                                bAll = True
                            End If
                        Else
                            If bAll = True Then
                                If iCnt = 0 Then
                                    iCnt = 1
                                End If
                                DiscreteVal = New ParameterDiscreteValue
                                DiscreteVal.Value = item.Value
                                paramValues.Add(DiscreteVal)
                            Else
                                If item.Selected Then
                                    If iCnt = 0 Then
                                        iCnt = 1
                                    End If
                                    DiscreteVal = New ParameterDiscreteValue
                                    DiscreteVal.Value = item.Value
                                    paramValues.Add(DiscreteVal)
                                End If
                            End If
                        End If
                    Next
            End Select
            paramField4.ApplyCurrentValues(paramValues)

            paramField5 = paramFields.Item("Agents")
            paramValues = paramField5.CurrentValues
            Select Case ddlReport.SelectedValue
                Case 106
                    bAll = False
                    iCnt = 0
                    For Each item As ListItem In lbAgents.Items
                        If item.Value.ToString = "-1" Then
                            If item.Selected Then
                                bAll = True
                            End If
                        Else
                            If bAll = True Then
                                If iCnt = 0 Then
                                    iCnt = 1
                                End If
                                DiscreteVal = New ParameterDiscreteValue
                                DiscreteVal.Value = item.Value
                                paramValues.Add(DiscreteVal)
                            Else
                                If item.Selected Then
                                    If iCnt = 0 Then
                                        iCnt = 1
                                    End If
                                    DiscreteVal = New ParameterDiscreteValue
                                    DiscreteVal.Value = item.Value
                                    paramValues.Add(DiscreteVal)
                                End If
                            End If
                        End If
                    Next
            End Select
            paramField5.ApplyCurrentValues(paramValues)

            paramField6 = paramFields.Item("Status")
            paramValues = paramField6.CurrentValues
            Select Case ddlReport.SelectedValue
                Case 106
                    bAll = False
                    iCnt = 0
                    For Each item As ListItem In lbStatus.Items
                        If item.Value.ToString = "-1" Then
                            If item.Selected Then
                                bAll = True
                            End If
                        Else
                            If bAll = True Then
                                If iCnt = 0 Then
                                    iCnt = 1
                                End If
                                DiscreteVal = New ParameterDiscreteValue
                                DiscreteVal.Value = item.Value
                                paramValues.Add(DiscreteVal)
                            Else
                                If item.Selected Then
                                    If iCnt = 0 Then
                                        iCnt = 1
                                    End If
                                    DiscreteVal = New ParameterDiscreteValue
                                    DiscreteVal.Value = item.Value
                                    paramValues.Add(DiscreteVal)
                                End If
                            End If
                        End If
                    Next
            End Select
            paramField6.ApplyCurrentValues(paramValues)

            paramField7 = paramFields.Item("LeadType")
            paramValues = paramField7.CurrentValues
            Select Case ddlReport.SelectedValue
                Case 106
                    bAll = False
                    iCnt = 0
                    For Each item As ListItem In lbLeadType.Items
                        If item.Value.ToString = "-1" Then
                            If item.Selected Then
                                bAll = True
                            End If
                        Else
                            If bAll = True Then
                                If iCnt = 0 Then
                                    iCnt = 1
                                End If
                                DiscreteVal = New ParameterDiscreteValue
                                DiscreteVal.Value = item.Value
                                paramValues.Add(DiscreteVal)
                            Else
                                If item.Selected Then
                                    If iCnt = 0 Then
                                        iCnt = 1
                                    End If
                                    DiscreteVal = New ParameterDiscreteValue
                                    DiscreteVal.Value = item.Value
                                    paramValues.Add(DiscreteVal)
                                End If
                            End If
                        End If
                    Next
            End Select
            paramField7.ApplyCurrentValues(paramValues)


            'Set DataBase Login Info
            rptDoc.SetDatabaseLogon(crUser, crPwd, crServer, crDatabase)

            ' Set the Crytal Report Viewer control's source to the report document
            crvReports.ReportSource = rptDoc

            'Provide Parameter Values
            crvReports.EnableParameterPrompt = False
            crvReports.DisplayGroupTree = False

            crvReports.RefreshReport()

            rptValues.Visible = False
            rptViewer.Visible = True
        End Sub


Thanks for your help in advance!!
Eddi Rae
Posted
Comments
[no name] 15-Dec-12 10:39am    
Can you specify the error you are having with above code ?

I found the issue. I had the dataset built incorrectly. Once I rebuilt it, everything worked properly.
Thanks for your response.
Eddi Rae
 
Share this answer
 
it is not working :(
it is not working :(it is not working :(
it is not working :(
it is not working :(
it is not working :(
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900