passing date range parameters from a vb.net windows application

点点圈 提交于 2019-12-25 00:05:39

问题


I need to pass date range parameters to a crystal report from a vb.net application, but I don't know how to. For anyone who may want to help me, please base yourself on the following scenario:

I have a crystal report that pulls data from an oracle database. In the database, I have a table of students, and each record has a birth date, name and surname. I need to display on the report only records where the birth date is between date X and date Y inclusive of both. Date X and Date Y are dates passed on the vb.net application when opening the report. I am using crystal reports 9, visual studio 2008 and oracle 9i.

Somebody please help me out, how do I achieve this?


回答1:


Assuming your Crystal Report is bound to an SQL Command, say:

SELECT name, surname FROM students WHERE birthdate BETWEEN {?@pDate1} AND {?@pDate2}

From VB.NET, you need to pass parameters as below:

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

This code is originally in ASP.NET with C#. I converted it to VB.NET. Please correct any discrepancies.




回答2:


Just create a crystal report with required parameters and create button, datetimepicker for from date and to date in the from.

insert the code for button click.

Working perfectly for me..............

If any mistakes modify..

Thank 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



回答3:


Just 2 Lines will be enough

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

Thats it.




回答4:


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



回答5:


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


来源:https://stackoverflow.com/questions/9157169/passing-date-range-parameters-from-a-vb-net-windows-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!