问题
I have facing problem while showing crystal report with 2 subreports. Both subreports pulling data from stored procedure.
All working fine on design time. But at runtime, Crystal Reports has an error saying table not found (second table) and nothing is displayed in reports.
This is the similar problem that I want to solve - Crystal Report for Income and Expense wth opening and closing balance
This is my code - both stored procedure has a single parameter of datetime
type:
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()
and this is the error:
回答1:
this is how I open a subreport.
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
or from your code
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()
来源:https://stackoverflow.com/questions/42219126/add-multiple-dataset-programatically-for-subreports-vb-net