"参数不正确"在水晶报告查看器中

时间:2014-04-02 08:09:50

标签: sql-server vb.net crystal-reports

我正在动态创建一个水晶报告。当我运行程序并单击按钮时,我得到 "参数不正确" 信息框。我该如何解决这个问题?

Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.ReportSource
Imports CrystalDecisions.Shared
Imports CrystalDecisions.Windows.Forms
Imports System.Data.SqlClient
Imports System.Data.OleDb

Public Class Form1

Dim objRpt As CrystalReport1
Dim con As New SqlConnection
Private Function CreateSelectQueryAndParameters() As String

    Dim paramFields As ParameterFields
    Dim paramField As ParameterField
    Dim paramDiscreteValue As ParameterDiscreteValue

    paramFields = New ParameterFields

    Dim query As String = "SELECT "

    Dim columnNo As Integer = 0

    If CheckBox1.Checked Then

        columnNo = columnNo + 1

        query = query.Insert(query.Length, "pcode as Column" + columnNo.ToString())

        paramField = New ParameterField()
        paramField.Name = "col" + columnNo.ToString()
        paramDiscreteValue = New ParameterDiscreteValue()
        paramDiscreteValue.Value = "Property Code"
        paramField.CurrentValues.Add(paramDiscreteValue)

        paramFields.Add(paramField)
    End If

    If CheckBox2.Checked Then

        columnNo = columnNo + 1
        If query.Contains("Column") Then

            query = query.Insert(query.Length, ", ")
        End If

        query = query.Insert(query.Length, "pname as Column" + columnNo.ToString())

        paramField = New ParameterField()
        paramField.Name = "col" + columnNo.ToString()
        paramDiscreteValue = New ParameterDiscreteValue()
        paramDiscreteValue.Value = "Property Name"
        paramField.CurrentValues.Add(paramDiscreteValue)
        paramFields.Add(paramField)
    End If
    For i As Integer = columnNo To 2

        columnNo = columnNo + 1

        paramField = New ParameterField()
        paramField.Name = "col" + columnNo.ToString()
        paramDiscreteValue = New ParameterDiscreteValue()
        paramDiscreteValue.Value = ""
        paramField.CurrentValues.Add(paramDiscreteValue)

        paramFields.Add(paramField)
    Next

    CrystalReportViewer1.ParameterFieldInfo = paramFields

    query += " FROM propdb"
    TextBox1.Text = query

    Return query

End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim cnn As SqlConnection
    Dim connString As String
    Dim query As String
    connString = "Data Source=RANJITHA-PC;Initial Catalog=hrmdb;Integrated Security=True"
    cnn = New SqlConnection(connString)
    cnn.Open()
    query = CreateSelectQueryAndParameters()
    If Not query.Contains("Column") Then
        MessageBox.Show("No selection to display!")
        Return
    End If

    Dim adepter As New SqlDataAdapter(query, cnn)
    Dim Ds As New DataSet2
    adepter.Fill(Ds, "propdb")
    MsgBox(Ds.Tables(0).Rows.Count)
    cnn.Close()

    objRpt = New CrystalReport1()
    objRpt.SetDataSource(Ds.Tables(0))

    Me.CrystalReportViewer1.ReportSource = objRpt

    Me.CrystalReportViewer1.Refresh()
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub End Class

1 个答案:

答案 0 :(得分:0)

您的for-loop似乎有问题,应该更改为

 For i As Integer = columnNo+1 To 2             

        paramField = New ParameterField()           
        paramField.Name = "col" + i.ToString()
        paramDiscreteValue = New ParameterDiscreteValue()
        paramDiscreteValue.Value = ""
        paramField.CurrentValues.Add(paramDiscreteValue)

        paramFields.Add(paramField)
 Next

CrystalReportViewer1.ParameterFieldInfo = paramFields中放一个休息品脱并检查参数名称