DataTable Select

  • Thread starter Thread starter VK
  • Start date Start date
V

VK

Hello,

I have a dataset which has one datatable in it. The dt
has over 3000 rows in it. Now I would like to get the rows
where the StartDate is 15 Feb 2005, so I did the following:

ds.Tables(0).Select("StartDate = '15/02/2005'")

However this returns me only 3 rows. I have tried other
possiblities like:

ds.Tables(0).Select("StartDate = '15 Feb 2005'")
ds.Tables(0).Select("StartDate = #15/02/2005#")
ds.Tables(0).Select("StartDate = '15/2/2005'")

But all of them returns only 3 rows. I know that there are
more then 3 rows for that date. I cheked the data from the
dbase and compared the rows with the dt and found the
dates, but I am not getting the expected result when I use
Select.

Can somebody help me please?

Thanks
 
Thanks for the reply

I tried:

ds.tables(0).Select("StartDate > #15/02/2005# and
StartDate < #16/02/2005#")

which returned:

Run-time exception thrown : System.FormatException -
String was not recognized as a valid DateTime.

I tried:

ds.tables(0).Select("StartDate > #2/15/2005# and StartDate
< #2/16/2005#")

which returned 0 rows

Also tried:

ds.tables(0).Select("StartDate > '15/02/2005' and
StartDate < '16/02/2005'")

which returns also 0 rows.

When I check a row, which has the date of 15 in it, then
it returns it as:

#2/15/2005#

btw: I am doing all these tests in the quick watch.

Any ideas?
 
VK:
The following code works for me:

DataTable dt = new DataTable();
dt.Columns.Add("Id", Type.GetType("System.Int32"));
dt.Columns.Add("Date", Type.GetType("System.DateTime"));
DataRow dr = null;

dr = dt.NewRow();
dr[0] = 1;
dr[1] = DateTime.Now;
dt.Rows.Add(dr);

dr = dt.NewRow();
dr[0] = 2;
dr[1] = DateTime.Now.AddHours(1);
dt.Rows.Add(dr);

dr = dt.NewRow();
dr[0] = 3;
dr[1] = DateTime.Now.AddMinutes(121) ;
dt.Rows.Add(dr);

dr = dt.NewRow();
dr[0] = 5;
dr[1] = DateTime.Now.AddHours(4).AddMinutes(34);
dt.Rows.Add(dr);

dr = dt.NewRow();
dr[0] = 6;
dr[1] = DateTime.Now.AddDays(1);
dt.Rows.Add(dr);

dr = dt.NewRow();
dr[0] = 7;
dr[1] = DateTime.Now.AddDays(5);
dt.Rows.Add(dr);


DataRow[] rows = dt.Select("Date > #10/05/2005# AND Date < #11/05/2005#");


Rows has the expected length of 4..The problem could certainly be your date
format. It needs to be entered in the correct culture format...you can see
this by just looking at what dateTime.Now looks like in your watch....aside
from that, I'm thinking maybe your data isn't what you expect?

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
 
Run-time exception thrown : System.FormatException -
String was not recognized as a valid DateTime.

I had this Error days ago, the problem is the Culture if you have
Formated your date to us standard and your webserver is de or other
language you have an problem.

You have to set the ds.Locale = new System.Globalization.CultureInfo(xx)

Then your dates have the same Format.

Greez The Filzmeister
 
Back
Top