Lahib
Lahib

Reputation: 1365

DataTable select method with Dates not showing right result

I have a Datatable with rows that i need to remove and insert into another Datatable. I am selecting the rows where column SLA < than todays date (SLA < '#date#'). When i do this the data in my new Datatable is not correct. I am getting rows that have dates that are later than todays date. I dont know what i am dowing wrong. The SLA column date syntax is dd-MM-yyyy HH:mm.

Here is my code:

public SLAWarningHandler()
{
}
public DataTable SLAWarningData(DataTable queryData)
{
    DataTable data = queryData;
    try
    {
        DateTime date = DateTime.Now;
        DataRow[] rows = data.Select("SLA < '#" + date + "#'");
        DataTable actual = new DataTable();
        actual = data.Clone();
        foreach (DataRow row in rows)
        {
            actual.ImportRow(row);
        }
    }
    catch (Exception exception)
    {
        throw exception;
    }
    return data;
}

I have tried to convert the date parameter to a string like so:

date.ToString("dd-MM-yyyy HH:mm")

but that didnt help.

Can anyone point out my issue?

thanks!

Upvotes: 2

Views: 1038

Answers (2)

dotnetstep
dotnetstep

Reputation: 17485

You problem is that you return wrong thing. You have to return Actual but you return data that is original record set.

public DataTable SLAWarningData(DataTable queryData)

    {
        DataTable actual = null;
        DataTable data = queryData;
        try
        {
            DateTime date = DateTime.Now;
            DataRow[] rows = data.Select("SLA < '#" + date + "#'");

            actual = data.Clone();
            foreach (DataRow row in rows)
            {
                actual.ImportRow(row);
            }
        }
        catch (Exception exception)
        {
            throw exception;
        }
        return actual;
    }

Upvotes: 1

dario
dario

Reputation: 5269

Use the standard ISO format (yyyyMMdd hh:mm:ss):

DataRow[] rows = data.Select(string.Format("SLA < '{0}'", date.ToString("yyyyMMdd hh:mm:ss"));

Or:

DataRow[] rows = data.Select(string.Format("SLA < '{0}'", date.ToString(DateTimeFormatInfo.InvariantInfo));

Upvotes: 0

Related Questions