Search code examples
c#crystal-reportsdatetimepicker

Filter data in crystal report using datetimepicker


I'm working on a report in Crystal Reports C# windows application.i fetch the data from localhost server to crystal report. I have 2 datetimepicker in my form.

datetimepicker1 = dtpfrom

datetimepicker2 - dtpto

enter image description here

Crystalreport

enter image description here

Now I want that, when selecting some date in from and the to datetimepicker the between data is show in the crystal report please help me to solve i don't know how to start. i'm searching through googling i didn't get any thing about between data in datetimepicker show in crystal report. Any ideas?

Here my try:

MySqlConnection conn = new MySqlConnection(MyCon);
            CrystalReport1 objcr = new CrystalReport1();
            MySqlDataAdapter da = new MySqlDataAdapter("select * from data where date>='" + DateTime.Parse(dtpfrom.Text) + "' and date<='" + DateTime.Parse(dtpto.Text) + "' ", conn);
            DataTable dt = new DataTable();
            da.Fill(dt);
            objcr.SetDataSource(dt);
            crystalReportViewer1.ReportSource = objcr;
            crystalReportViewer1.RefreshReport();

But i'm getting an error

An unhandled exception of type 'CrystalDecisions.CrystalReports.Engine.DataSourceException' occurred in CrystalDecisions.ReportAppServer.DataSetConversion.dll

Additional information: Failed to load database information.

On line:

objcr.SetDataSource(dt);

Solution

  • @divi

    filter data in report using datetimepicker is not done for your coding

    Do the following:

    create a new parameter fromdate, todate in Field Explorer -> Parameter Field(Right Click)-> New enter image description here

    like this make todate also

    enter image description here

    then right click the report.

    Select Report -> Selection Formula -> Record...

    Do following

    enter image description here

    then click save and close button in that form.

    Know this:

    Right Click the report -> Database -> Set Datasource Location...

    enter image description here

    Now Code

     ReportDocument report = new ReportDocument();
                report.Load("C:\\Users\\Desktop\\CrystalReport1.rpt");
    
                TableLogOnInfo Table = new TableLogOnInfo();
                ConnectionInfo Connection = new ConnectionInfo();
                Tables Tables;
    
                ParameterFieldDefinitions Parameters;
                ParameterFieldDefinition Parameter;
                ParameterValues Values = new ParameterValues();
                ParameterDiscreteValue DiscreteValue = new ParameterDiscreteValue();
    
                DiscreteValue.Value = dateTimePicker1.Text;
                Parameters = report.DataDefinition.ParameterFields;
                Parameter = Parameters["fromdate"];
                Values = Parameter.CurrentValues;
    
                Values.Clear();
                Values.Add(DiscreteValue);
                Parameter.ApplyCurrentValues(Values);
    
                DiscreteValue.Value = dateTimePicker2.Text;
                Parameters = report.DataDefinition.ParameterFields;
                Parameter = Parameters["todate"];
                Values = Parameter.CurrentValues;
    
                Values.Add(DiscreteValue);
                Parameter.ApplyCurrentValues(Values);
    
                Connection.ServerName = "Your servername in Set Datasource Location";
                Connection.DatabaseName = "Your databasename in Set Datasource Location";
                Connection.UserID = "your username";
                Connection.Password = "your password";
    
                Tables = report.Database.Tables;
                foreach (CrystalDecisions.CrystalReports.Engine.Table table in Tables)
                {
                    Table = table.LogOnInfo;
                    Table.ConnectionInfo = Connection;
                    table.ApplyLogOnInfo(Table);
                }
    
                crystalReportViewer1.ReportSource = report;
                crystalReportViewer1.Refresh();
    

    Hope This helps to solve your problem.