使用链接参数传递Crystal报表中的参数

时间:2017-05-22 07:44:36

标签: vb.net crystal-reports

我更改了标题: 文件临时错误:链接参数

上的操作非法

要: 在Crystal报表中传递参数

我想我知道如何将参数从主报告传递到子报告,我做了类似的事情:

enter image description here

我现在可以查看水晶报告预览报告的样子,一切正常。

但是,当我需要将报告调用到VB.Net时,我使用此代码。

Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Imports System.Data.SqlClient
Imports System.Data


Public Class rptXbar

    Private Sub rptXbar_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadXbar(PartNoRep, DimItmRep)

    End Sub

    Sub LoadXbar(ByVal PartNo As String, ByVal DimItem As String)
        Dim sqlcom As New SqlCommand
        Dim dt As New DataTable
        Dim dta As New SqlDataAdapter
        Dim crConnectionInfo As New ConnectionInfo
        Dim crtableLogoninfo As New TableLogOnInfo
        Dim repX As New XbarRchart
        Try
            repX.Load(System.AppDomain.CurrentDomain.BaseDirectory() & "XbarRchart.rpt")
            With sqlcom
                Me.Cursor = Cursors.WaitCursor
                conn.Open()
                .Connection = conn
                .CommandTimeout = 100
                .CommandType = CommandType.StoredProcedure
                .CommandText = "dbo.XBarRChart"
                .Parameters.AddWithValue("@PartNo", PartNo)
                .Parameters.AddWithValue("@DimItem", DimItem)
                .ExecuteNonQuery()
            End With
            dt.Clear()
            dta = New SqlDataAdapter(sqlcom)
            dta.Fill(dt)
            repX.SetDatabaseLogon(userid, password, dtsrc, initcat)
            repX.SetParameterValue("@PartNo", PartNo)
            repX.SetParameterValue("@DimItem", DimItem)

            repX.SetParameterValue("@PartNo", PartNo, "MIPIRHeader.rpt")
            repX.SetParameterValue("@DimItem", DimItem, "MIPIRHeader.rpt")

            repX.SetParameterValue("@PartNo", PartNo, "MIPIRDetails.rpt")
            repX.SetParameterValue("@DimItem", DimItem, "MIPIRDetails.rpt")

            repX.SetDataSource(dt)
            Me.CrystalReportViewer1.ReportSource = repX
            Me.CrystalReportViewer1.Refresh()
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            Me.Cursor = Cursors.Default
        End Try
        conn.Close()
    End Sub
End Class

会弹出一个错误,它会说:

enter image description here

如果删除链接,参数将要求输入,如果我删除此代码

        repX.SetParameterValue("@PartNo", PartNo, "MIPIRHeader.rpt")
        repX.SetParameterValue("@DimItem", DimItem, "MIPIRHeader.rpt")

        repX.SetParameterValue("@PartNo", PartNo, "MIPIRDetails.rpt")
        repX.SetParameterValue("@DimItem", DimItem, "MIPIRDetails.rpt")

它还会询问参数,对于如何解决这个问题有什么建议吗?

1 个答案:

答案 0 :(得分:0)

我终于找到了答案!

Sub LoadXbar(ByVal PartNo As String, ByVal DimItem As String)
        Dim sqlcom As New SqlCommand
        Dim dt As New DataTable
        Dim dta As New SqlDataAdapter
        Dim crConnectionInfo As New ConnectionInfo
        Dim crtableLogoninfo As New TableLogOnInfo
        Dim repX As New XbarRchart
        Try
            repX.Load(System.AppDomain.CurrentDomain.BaseDirectory() & "XbarRchart.rpt")
            With sqlcom
                Me.Cursor = Cursors.WaitCursor
                conn.Open()
                .Connection = conn
                .CommandTimeout = 100
                .CommandType = CommandType.StoredProcedure
                .CommandText = "dbo.XBarRChart"
                .Parameters.AddWithValue("@PartNo", PartNo)
                .Parameters.AddWithValue("@DimItem", DimItem)
                .ExecuteNonQuery()
            End With
            dt.Clear()
            dta = New SqlDataAdapter(sqlcom)
            dta.Fill(dt)
            repX.SetDatabaseLogon(userid, password, dtsrc, initcat)
            repX.SetParameterValue("@PartNo", PartNo)
            repX.SetParameterValue("@DimItem", DimItem)

            repX.SetParameterValue("@PartNo", PartNo, "MIPIRHeader.rpt")
            repX.SetParameterValue("@DimItem", DimItem, "MIPIRHeader.rpt")

            repX.SetParameterValue("@PartNo", PartNo, "MIPIRDetails.rpt")
            repX.SetParameterValue("@DimItem", DimItem, "MIPIRDetails.rpt")

            repX.SetDataSource(dt)
            Me.CrystalReportViewer1.ReportSource = repX
            Me.CrystalReportViewer1.Refresh()
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            Me.Cursor = Cursors.Default
        End Try
        conn.Close()
    End Sub

这是我填充主报表数据和子报表的代码,如果您注意到repX.SetDataSource(dt)低于我填充子报表的代码,我将代码更改为以下内容:

Sub LoadXbar(ByVal PartNo As String, ByVal DimItem As String)
        Dim sqlcom As New SqlCommand
        Dim dt As New DataTable
        Dim dta As New SqlDataAdapter
        Dim crConnectionInfo As New ConnectionInfo
        Dim crtableLogoninfo As New TableLogOnInfo
        Dim repX As New XbarRchart
        Try
            repX.Load(System.AppDomain.CurrentDomain.BaseDirectory() & "XbarRchart.rpt")
            With sqlcom
                Me.Cursor = Cursors.WaitCursor
                conn.Open()
                .Connection = conn
                .CommandTimeout = 100
                .CommandType = CommandType.StoredProcedure
                .CommandText = "dbo.XBarRChart"
                .Parameters.AddWithValue("@PartNo", PartNo)
                .Parameters.AddWithValue("@DimItem", DimItem)
                .ExecuteNonQuery()
            End With
            dt.Clear()
            dta = New SqlDataAdapter(sqlcom)
            dta.Fill(dt)
            repX.SetDatabaseLogon(userid, password, dtsrc, initcat)
            'New Location
            repX.SetDataSource(dt)
            repX.SetParameterValue("@PartNo", PartNo)
            repX.SetParameterValue("@DimItem", DimItem)

            repX.SetParameterValue("@PartNo", PartNo, "MIPIRHeader.rpt")
            repX.SetParameterValue("@DimItem", DimItem, "MIPIRHeader.rpt")

            repX.SetParameterValue("@PartNo", PartNo, "MIPIRDetails.rpt")
            repX.SetParameterValue("@DimItem", DimItem, "MIPIRDetails.rpt")


            Me.CrystalReportViewer1.ReportSource = repX
            Me.CrystalReportViewer1.Refresh()
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            Me.Cursor = Cursors.Default
        End Try
        conn.Close()
    End Sub

我的代码工作了!所以在将命令放在VB.net中时也要小心,你可能把它放错了。