Problem:
I'm working on an ASP.NET WebForms application where I need to filter records in a DataTable based on a date range. The date range is selected using FromMonth, ToMonth, FromYear, and ToYear dropdowns. The goal is to display the filtered data in a RadGrid, but my filtering logic is not working as expected. Sometimes, no data is displayed, or the results are incorrect.
Issue:
The filtering logic based on the PADate field is not working as expected. Even when I select a valid date range using the dropdowns, the grid either shows no data or the wrong results.
Debugging Steps I've Taken:
Verified that the
PADatefield in the database is in the correctDateTimeformat.Checked that the values from the dropdowns (
FromMonth,ToMonth,FromYear, andToYear) are correct.Ensured that the
PADatefield is correctly parsed as aDateTimevalue in the filtering logic.
Expected Outcome:
I want the grid to display only the records where the PADate falls within the selected date range (from FromMonth/FromYear to ToMonth/ToYear).
Code:
Here is the method I'm using to filter the data:
protected void PCApplicationStatus()
{
try
{
// Get the selected values from dropdowns
int fromMonth = int.Parse(FromMonth.SelectedValue);
int toMonth = int.Parse(ToMonth.SelectedValue);
int fromYear = int.Parse(FromYear.SelectedValue);
int toYear = int.Parse(ToYear.SelectedValue);
// Create date range
DateTime startDate = new DateTime(fromYear, fromMonth, 1);
DateTime endDate = new DateTime(toYear, toMonth, DateTime.DaysInMonth(toYear, toMonth));
// Validate that 'From' date is earlier than 'To' date
if (fromYear > toYear || (fromYear == toYear && fromMonth > toMonth))
{
throw new InvalidOperationException("The 'From' date must be earlier than the 'To' date.");
}
// Fetch data from database
AdDAL obj = new AdDAL();
DataSet ds = obj.ExecutePaymentClaimQuery();
if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
{
DataTable dt = ds.Tables[0];
DataTable filteredDt = dt.Clone(); // Clone the structure of the original table
// Filter rows based on the date range
foreach (DataRow dr in dt.Rows)
{
DateTime? paDate = dr["PADate"] is DBNull ? (DateTime?)null : Convert.ToDateTime(dr["PADate"]);
if (paDate.HasValue && paDate.Value >= startDate && paDate.Value <= endDate)
{
filteredDt.ImportRow(dr); // Import rows that fall within the date range
}
}
// Bind the filtered data to the grid
if (filteredDt.Rows.Count > 0)
{
Grid.DataSource = filteredDt;
}
else
{
Grid.DataSource = null; // No matching rows found
}
Grid.DataBind(); // Rebind the grid with the filtered data
}
else
{
// No data available
Grid.DataSource = null;
Grid.DataBind();
}
}
catch (Exception ex)
{
// Log the error
Console.WriteLine("Error: " + ex.ToString());
}
}




DateTime endDate = new DateTime(toYear, toMonth, DateTime.DaysInMonth(toYear, toMonth));'Don't do that. Get the first day of the next month, and then use a<parameter. That way things will work for things at 2pm on the last day.Grid.DataSource = null; // No matching rows foundWhy do that? Why not always just doGrid.DataSource = filteredDt;?