problem with DataTable.Select

J

Jeff

hey

..NET 2.0

I have a datatable which consist of 3 columns (int, date, value). This
DataTable have 3 rows, the values of the "date" ("date" column is of
datetime datatype) column is:
2007-09-15 00:00:00.000
2007-10-15 00:00:00.000
2007-11-15 00:00:00.000

The problem is that when I my code try to select an record, I get no
records.
This is the select I try:
DataTable.Select("date >= '01.11.2007 00:00:00' and date < '01.12..2007
00:00:00'");
this gives me 0 row, it should have given me 1 row

and this:
DataTable.Select("date >= '01.11.2007 00:00:00'");
gives me 3 rows, but it should have given me 1 row

what am I doing wrong here?
 
D

DeveloperX

hey

.NET 2.0

I have a datatable which consist of 3 columns (int, date, value). This
DataTable have 3 rows, the values of  the "date"  ("date" column is of
datetime datatype) column is:
2007-09-15 00:00:00.000
2007-10-15 00:00:00.000
2007-11-15 00:00:00.000

The problem is that when I my code try to select an record, I get no
records.
This is the select I try:
DataTable.Select("date >= '01.11.2007 00:00:00'  and date < '01.12..2007
00:00:00'");
this gives me 0 row, it should have given me 1 row

and this:
DataTable.Select("date >= '01.11.2007 00:00:00'");
gives me 3 rows, but it should have given me 1 row

what am I doing wrong here?

Well, this works for me:

DataSet data = new DataSet();
data.Tables.Add(new DataTable());
data.Tables[0].Columns.Add("ADate", Type.GetType("System.DateTime"));
data.Tables[0].Rows.Add(new object[]{DateTime.Parse("15/09/2007")});
data.Tables[0].Rows.Add(new object[]{DateTime.Parse("15/10/2007")});
data.Tables[0].Rows.Add(new object[]{DateTime.Parse("15/11/2007")});

DataRow[] rows = data.Tables[0].Select("ADate >= '01/11/2007' and
ADate < '01/12/2007'");
Console.WriteLine(rows.Length.ToString()); //returns 1
rows = data.Tables[0].Select("ADate > '01/11/2007'");
Console.WriteLine(rows.Length.ToString()); //returns 1


but


DataSet data = new DataSet();
data.Tables.Add(new DataTable());
data.Tables[0].Columns.Add("ADate", Type.GetType("System.DateTime"));
data.Tables[0].Rows.Add(new object[]{DateTime.Parse("15/09/2007")});
data.Tables[0].Rows.Add(new object[]{DateTime.Parse("15/10/2007")});
data.Tables[0].Rows.Add(new object[]{DateTime.Parse("15/11/2007")});

DataRow[] rows = data.Tables[0].Select("ADate >= '11/01/2007' and
ADate < '12/01/2007'");
Console.WriteLine(rows.Length.ToString()); //returns 0
rows = data.Tables[0].Select("ADate > '11/01/2007'");
Console.WriteLine(rows.Length.ToString()); //returns 3


gives the results you get. Your dates are being interpreted as MM/DD
not DD/MM.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,


Most probably those are not valid dates , could you try it without the time
component?
 
N

Nicholas Paldino [.NET/C# MVP]

Jeff,

It's pretty obvious that it is expecting a date in YYYY-MM-DD HH:mm:ss
format. I suggest you try formatting your date using that format, and not
the format based on the current culture (also, try using the date literal,
the pound sign, i.e. '#').
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top