从vb.net Windows应用程序传递日期范围参数

时间:2012-02-06 08:03:24

标签: vb.net crystal-reports

我需要将日期范围参数传递给vb.net应用程序的水晶报告,但我不知道如何操作。对于任何想要帮助我的人,请基于以下场景:

我有一个从oracle数据库中提取数据的水晶报告。在数据库中,我有一个学生表,每个记录都有一个出生日期,姓名和姓氏。我需要在报告上只显示出生日期在日期X和日期Y之间的记录,包括两者。日期X和日期Y是打开报告时在vb.net应用程序上传递的日期。 我正在使用水晶报告9,visual studio 2008和oracle 9i。

有人请帮助我,我该如何实现?

5 个答案:

答案 0 :(得分:0)

假设您的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

此代码最初是在带有C#的ASP.NET中。我把它转换为VB.NET。请更正任何差异。

答案 1 :(得分:0)

只需创建一个包含所需参数的水晶报告,然后创建按钮,datetimepicker,用于从日期和日期开始。

插入按钮点击代码。

完美地为我工作..............

如果有任何错误修改..

感谢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 :(得分:0)

只需2行即可

rptSummary.SetParameterValue("your report perameter name", your value 1st value)
rptSummary.SetParameterValue("your report perameter name", your value 2nd value)

多数民众赞成。

答案 3 :(得分:0)

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();

答案 4 :(得分:-1)

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()