Error Specified cast is not valid

I use code as below. (sort data column Planned Start)

dt_Result.AsEnumerable().OrderBy(Function(row) row.Field(Of DateTime)("Planned Start")).CopyToDataTable()

But show error Specified cast is not valid.

My data in excel as below.
image

Please guide me for solve it.

Thank you

Hi @Stef_99

Try the below syntax:

dt_Result.AsEnumerable().OrderBy(Function(row) DateTime.ParseExact(row.Field(Of String)("Planned Start"), "d/M/yyyy HH:mm", System.Globalization.CultureInfo.InvariantCulture)).CopyToDataTable()

Regards

Hi,

To isolate cause, can you check content of the datatable at Locals panel when error occurs in debug run?

Is there any invalid data including empty? And also check format style of the date time data.

Regards,

@vrdabberu If I have 2 format (d/m/yyyy HH:MM , dd/mm/yyyy HH:MM:SS)
How to edit your code ?

Thank you

Hi @Stef_99

Try this:

dt_Result.AsEnumerable().OrderBy(Function(row) DateTime.ParseExact(row.Field(Of String)("Planned Start"), {"d/M/yyyy HH:mm","dd/MM/yyyy HH:mm:ss"}, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None)).CopyToDataTable()

Regards

1 Like

@vrdabberu It error as below.

Hi @Stef_99

Can you share the syntax you have written

Regards

@vrdabberu I found it.

Thankyou .

1 Like

You’re welcome @Stef_99

Regards

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.