user1554178
user1554178

Reputation: 1

Filtering by date and time C#

I'm creating reports in C# ASP.Net, whereby in the reports I can filter data created between two different dates (e.g Start date: 15th July and End date: 17th July) but when it comes to filtering data created on a particular day (e.g StartDate: 15th July and End Date: 15th July) the query doesn't retrieve anything...

//Code from OutletDistributor.aspx.cs
ReportManager master = ObjectFactory.GetInstance<ReportManager>();
ReportResponse responce = new ReportResponse();
if (ddDistributor == Guid.Empty)
  responce = master.GetAllOutletDistributor(sDate, EDate);
else
  responce = master.GetOutletDistributor(sDate, EDate, ddDistributor);
ReportViewer1.Reset();
ReportViewer1.LocalReport.DataSources.Clear();
ReportViewer1.ProcessingMode = ProcessingMode.Local;
var stream = Assembly.GetAssembly(typeof(SampleReport)).GetManifestResourceStream(responce.ReportResource);
ReportDataSource reportDataSource = new ReportDataSource(responce.ReportDataSet, responce.ReportDataSource);
stream = RdlcReportHelper.TranslateReport(stream);
ReportViewer1.LocalReport.DataSources.Add(reportDataSource);
ReportViewer1.LocalReport.LoadReportDefinition(stream);
ReportViewer1.LocalReport.DisplayName = ResourceHelper.GetText(null, "hq.rpt.outletdist.heading");
ReportViewer1.LocalReport.Refresh();

//Code from ReportManager.cs
//Filter All Outlet Distributor
public ReportResponse GetAllOutletDistributor(DateTime StartDate, DateTime EndDate) {
  ReportResponse report = new ReportResponse();
  report.ReportResource = "Distributr.HQ.Lib.Reports.RcdlFiles.OutletDistributor.rdlc";
  List<OutletReportItem> Report = _outletReport.GetAllOutlets()
    .Where(p => p.CreationDate >= StartDate && p.CreationDate <= EndDate).ToList();
  Report.ForEach(n => report.ReportDataSource.Add(n));
  report.ReportDataSet = "dsOutletDistributor";
  return report;
}

Upvotes: 0

Views: 1441

Answers (3)

Sanket R. Patil
Sanket R. Patil

Reputation: 311

In this condition where your date is same use "=" (instead of difference) in your query

Upvotes: 0

cat916
cat916

Reputation: 1361

Actually, your code List<OutletReportItem> Report = _outletReport.GetAllOutlets().Where(p => p.CreationDate >= StartDate && p.CreationDate <= EndDate).ToList() when initialize the object DateTime so the default value of date with hours, minutes and seconds which are '00'. So I think you could use DateTime.Compare()

Upvotes: 0

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174299

I assume that StartDate and EndDate both contain a time component. Remove it using the Date property:

StartDate = StartDate.Date;
EndDate = EndDate.Date;

// your query goes here...

Upvotes: 1

Related Questions