DateTime select problem

hi ppl, im using a datatable.select method to filter a datatable, i have a datetime column and i have 2 dateTimePickers.

Im doing the next:

string date;
date = String.Format("Fecha >= #{0}# AND Fecha < #{1}#", dtPickerDesde.Value.ToShortDateString(), dtPickerHasta.Value.AddDays(1).ToShortDateString());
rows = dtFacturas.Select(date);

//rows is my DataRows array

i dont know why it throw me exception with some dates and with some others it dont for example, when the string is like this:

Fecha >= #31/07/2006# AND Fecha < #01/08/2006#

thres no problem

but when its like this:

Fecha >= #31/01/2006# AND Fecha < #01/08/2006#

or this:

Fecha >= #28/02/2006# AND Fecha < #01/08/2006#

it throws an exception, and much more dates throw me exception but i dunno why, can some1 help me

mig16






Answer this question

DateTime select problem

  • Debugger Zero

    Put always datetime in the format MM/dd/yyyy

    Fecha >= #02/28/2006# AND Fecha < #08/01/2006#

    Andrea.


  • a_abdi406

    When working with DataSet tables or DataTable you should set yourself a rule, that always, after creating the instance you will set the Locale Property. If you don't do that, then Locale will take default InvariantCulture, which in most of cases can be different from current user setting. That is why you will have problems with using table datetime values, and also that is the case with floating point types.
    Set the Locale to CultureInfo.CurrentCulture immediatelly after creating DataSet or DataTable and then you can select rows from table in this way:

    DataRow[] rows = myTable.Select(string.Format("[DateColumnName] >= '{0}' and ("[DateColumnName] <= '{1}'", dateFrom, dateTo));

    What is happening here is that DateTime values dateFrom and dateTo are converted to string but the format of date is the same as one used for storring dates in myTable just because Locale is CurrentCulture.

  • Leadorn

    Could you provide more details on the exceptions that are thrown

    -Tom Meschter
    Software Dev, Visual C# IDE



  • baron5038

    Which line throws the exception

    date = String.Format("Fecha >= #{0}# AND Fecha < #{1}#", dtPickerDesde.Value.ToShortDateString(), dtPickerHasta.Value.AddDays(1).ToShortDateString());

    or

    rows = dtFacturas.Select(date);

    -Tom Meschter
    Software Dev, Visual C# IDE



  • GDigrego

    the message is the next:

    String was not recognized as a valid DateTime.

    mig16

  • DateTime select problem