Jithu
Jithu

Reputation: 511

Datatable Select Function

Trying to select fields where 'Expected Closure Date' is less than todays date, and deleting them from the table. However getting error like Missing operand after 'closure' operator.

DateTime Tdy=DateTime.Now;
 var rows = dt.Select("Expected closure date < "+Tdy+"");
 foreach (var row in rows)
         row.Delete(); 

Upvotes: 1

Views: 1597

Answers (3)

Surjit Samra
Surjit Samra

Reputation: 4662

This is what you need , Tested

var rows = dt.Select(string.Format("[Expected closure date] < '{0}'", Tdy));

Here is full working sample

using System;
using System.Data;


namespace ConsoleApplication5
{
  class Program
  {    

    static void Main(string[] args)
    {
      DataTable dt = new DataTable();
      string col = "Expected closure date";
      dt.Columns.Add(col,typeof(DateTime));

      dt.Rows.Add(DateTime.Now.AddDays(-1) );

      Console.WriteLine("Total rows in dt " + dt.Rows.Count);

      DateTime Tdy = DateTime.Now;
      var rows = dt.Select(string.Format("[Expected closure date] <= '{0}'", Tdy));
      foreach (var row in rows)
        row.Delete();
      Console.WriteLine("Total rows in dt " + dt.Rows.Count);

      Console.ReadLine();

    }
  }
}

Here is output

Total rows in dt 1
Total rows in dt 0

Upvotes: 1

SLaks
SLaks

Reputation: 888203

You need to wrap the date in octothorpes:

dt.Select("myDateTimecolumn < #" + tdy.ToString("MM/dd/yyyy") + "#");

Upvotes: 1

Royi Namir
Royi Namir

Reputation: 148744

DateTime Tdy=DateTime.Now;
 var rows = dt.Select("myDateTimecolumn<'"+DateTime.Now.ToString("yyyyMMdd")+"'");
 foreach (var row in rows)
         row.Delete();

Upvotes: 1

Related Questions