以编程方式为子报表VB.net添加多个数据集

时间:2017-02-14 06:14:43

标签: vb.net sql-server-2008 stored-procedures crystal-reports-2010

我在显示带有2个子报告的水晶报告时遇到了问题。两个子报表都从存储过程中提取数据。

所有在设计时都工作正常。但是在运行时,Crystal Reports出现错误,表示找不到表(第二个表),报表中没有显示任何内容。

这是我想要解决的类似问题 - Crystal Report for Income and Expense wth opening and closing balance

这是我的代码 - 两个存储过程都有一个datetime类型的参数:

    Dim DOF As Date = #4/1/2016#
    DOF = Format(DOF, "dd/MM/yyyy")

    Dim DOT As Date = #4/1/2016# ' Now.Date
    DOT = Format(DOT, "dd/MM/yyyy")

    Dim DS As Data.DataSet
    Dim DA As SqlDataAdapter
    Dim s_Sql As String
    Dim s_sql2 As String
    Dim cmd As SqlCommand

    s_Sql = "sp_DailyIncome"
    cmd = New SqlCommand(s_Sql, o_con)
    cmd.CommandType = CommandType.StoredProcedure
    cmd.Parameters.Add(New SqlParameter("@FDate", Format(DOF, "MM/dd/yyyy")))

    DA = New SqlDataAdapter
    DA.SelectCommand = cmd
    DS = New Data.DataSet
    DA.Fill(DS, s_Sql)
    cmd.Dispose()

    s_sql2 = "sp_DailyExpense"
    o_Cmd = New SqlCommand(s_sql2, o_con)
    o_Cmd.CommandType = CommandType.StoredProcedure
    o_Cmd.Parameters.Add(New SqlParameter("@FDate", Format(DOF, "MM/dd/yyyy")))


    DA.SelectCommand = o_Cmd
    DA.Fill(DS, s_sql2)


    Dim rptDoc As CrystalDecisions.CrystalReports.Engine.ReportDocument
    rptDoc = New rptDailyCashReport
    rptDoc.DataDefinition.FormulaFields("SchoolName").Text = "'" & holdSchoolName & "'"
    rptDoc.SetParameterValue("@FDate", DOF)

    rptDoc.Subreports.Item(0).SetDataSource(DS.Tables(0))
    rptDoc.Subreports.Item(1).SetDataSource(DS.Tables(1))

    Dim objForm As New ShowReport
    objForm.ViewReport(rptDoc, , "")
    objForm.ShowDialog()

这是错误: Error in Runtime

1 个答案:

答案 0 :(得分:0)

这是我打开子报告的方式。

Dim mainReport As <Name of your report with crpt extension> 
Dim subReport As CrystalDecisions.CrystalReports.Engine.ReportDocument
Dim subReport2 As CrystalDecisions.CrystalReports.Engine.ReportDocument 
subReport = mainReport.OpenSubReport(<name of your subreport as defined in your main report>)
subReport2 = mainReport.OpenSubReport(<name of your second subreport as defined in your main report>)
mainReport.SetDataSource(<your main report's datatable>)
subReport.SetDataSource(<your datatable>)
subReport2.SetDataSource(<your datatable 2>)

reportViewer.ReportSource = mainReport

或来自您的代码

Dim rptDoc As New rptDailyCashReport
Dim subReport1 As CrystalDecisions.CrystalReports.Engine.ReportDocument
Dim subReport2 As CrystalDecisions.CrystalReports.Engine.ReportDocument

subReport1 = rptDoc.OpenSubReport(<name of your sub report 1>) '<-- !! NAME IS CASE SENSITIVE
subReport2 = rptDoc.OpenSubReport(<name of your second sub report>)

subReport1.SetDataSource(DS.Tables(0))
subReport2.SetDataSource(DS.Tables(1))

' place this at the last part, before showing your report
rptDoc.DataDefinition.FormulaFields("SchoolName").Text = "'" & holdSchoolName & "'"
rptDoc.SetParameterValue("@FDate", DOF)

Dim objForm As New ShowReport
objForm.ViewReport(rptDoc, , "")
objForm.ShowDialog()