我需要将日期范围参数从 vb.net 应用程序传递到水晶报表,但我不知道如何操作。对于任何想要帮助我的人,请根据以下情况进行判断:
我有一个从 Oracle 数据库中提取数据的水晶报表。在数据库中,我有一个学生表,每条记录都有出生日期、姓名。我需要在报告上仅显示出生日期在日期 X 和日期 Y 之间(包括两者)的记录。日期 X 和日期 Y 是打开报告时在 vb.net 应用程序上传递的日期。 我正在使用 Crystal Reports 9、Visual Studio 2008 和 Oracle 9i。
有人请帮助我,我该如何实现这个目标?
假设您的 Crystal Report 绑定到 SQL 命令,请说:
SELECT name, surname FROM students WHERE birthdate BETWEEN {?@pDate1} AND {?@pDate2}
从VB.NET,您需要传递如下参数:
Private rptSummary As New ReportDocument()
Protected Sub Page_Init(sender As Object, e As EventArgs)
Try
Dim pFields As New ParameterFields()
Dim pField_Date1 As New ParameterField()
Dim pField_Date2 As New ParameterField()
Dim pDiscreteValue_Date1 As New ParameterDiscreteValue()
Dim pDiscreteValue_Date2 As New ParameterDiscreteValue()
Dim ApplPath As String = Server.MapPath("MyReport.rpt")
rptSummary.FileName = ApplPath
pField_Date1.Name = "@pDate1"
pField_Date2.Name = "@pDate2"
Dim BirthDate1, BirthDate2
' Parameter: @pDate1
pDiscreteValue_Date1.Value = BirthDate1
pField_Date1.CurrentValues.Add(pDiscreteValue_Date1)
pFields.Add(pField_Date1)
' Parameter: @pDate2
pDiscreteValue_Date2.Value = BirthDate2
pField_Date2.CurrentValues.Add(pDiscreteValue_Date2)
pFields.Add(pFieldDate2)
CrystalReportViewer1.ParameterFieldInfo = pFields
rptSummary.SetParameterValue(0, pDiscreteValue_Date1)
rptSummary.SetParameterValue(1, pDiscreteValue_Date2)
CrystalReportViewer1.ReportSource = rptSummary
Catch ex As Exception
End Try
End Sub
此代码最初是在 ASP.NET 中使用 C# 编写的。我把它转换成VB.NET。如有差异请指正。
只需创建一个包含所需参数的水晶报表,并在 from 中创建按钮、日期时间选择器来表示起始日期和截止日期。
插入按钮点击代码。
对我来说完美工作........................
如有错误请修改..
谢谢Q
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim cryRpt1 As New ReportDocument
Dim str As String = System.AppDomain.CurrentDomain.BaseDirectory
str = str.Substring(0, str.Length - 10)
cryRpt1.Load(str & "daily_scrl_rep.rpt")
cryRpt1.SetParameterValue(0, DTPFrom.Text)
cryRpt1.SetParameterValue(1, DTPTO.Text)
CrystalReportViewer1.ReportSource = cryRpt1
CrystalReportViewer1.Refresh()
End Sub
只需 2 行就足够了
rptSummary.SetParameterValue("your report perameter name", your value 1st value)
rptSummary.SetParameterValue("your report perameter name", your value 2nd value)
就是这样。
string rang1 = Session["Rang1"].ToString();
string rang2 = Session["Rang2"].ToString(); ;
ReportDocument obj = new ReportDocument();
obj.Load(Server.MapPath("~/CType_CrystalReport.rpt"));
obj.SetDatabaseLogon("sa", "12qwaszx", "BTS-10", "BTS_ERP");
ParameterFields paraf = new ParameterFields();
ParameterField par = new ParameterField();
ParameterField par2 = new ParameterField();
par.ParameterFieldName = "CtCode";
ParameterDiscreteValue dcpara1 = new ParameterDiscreteValue();
ParameterDiscreteValue dcpara2 = new ParameterDiscreteValue();
par.Name = rang1;
par2.Name = rang2;
dcpara1.Value = rang1.ToString();
par.CurrentValues.Add(dcpara1);
paraf.Add(par);
dcpara2.Value = rang2.ToString();
par2.CurrentValues.Add(dcpara2);
paraf.Add(par2);
CrystalReportViewer1.ParameterFieldInfo = paraf;
obj.SetParameterValue("CtCode", dcpara1);
obj.SetParameterValue("CtCode", dcpara2);
CType_DataSet dsCustomers = GetData("select * from GL_CUSTTYPE where CT_CODE between '" + rang1.ToString() + "' AND '" + rang2.ToString() + "' ");
obj.SetDataSource(dsCustomers);
CrystalReportViewer1.ReportSource = obj;
CrystalReportViewer1.RefreshReport();
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Dim crtableLogoninfos As New CrystalDecisions.Shared.TableLogOnInfos()
Dim crtableLogoninfo As New CrystalDecisions.Shared.TableLogOnInfo()
Dim crConnectionInfo As New CrystalDecisions.Shared.ConnectionInfo()
Dim crit As String
Dim CrTables As Tables
Dim CrTable As Table
reportdocument.Load(Server.MapPath("~/Reports/IssueReport.rpt"))
CrTables = reportdocument.Database.Tables
For Each CrTable In CrTables
crtableLogoninfo = CrTable.LogOnInfo
crtableLogoninfo.ConnectionInfo = crConnectionInfo
CrTable.ApplyLogOnInfo(crtableLogoninfo)
CrTable.Location = crConnectionInfo.DatabaseName & ".dbo." & CrTable.Location.Substring(CrTable.Location.LastIndexOf(".") + 1)
Next
Crit = "{tablename.fieldname}>=#" & Format(CDate(txtfromdate.Text), "yyyy/MM/dd") & "#"
Crit = Crit & " and {tablename.fieldname} <=#" & Format(CDate(txtto.Text), "yyyy/MM/dd") & "#"
reportdocument.RecordSelectionFormula = Crit
CrystalReportViewer1.ReportSource = reportdocument
CrystalReportViewer1.RefreshReport()