Sort a datatable based on column name which gives incorrect input string as error

Hi All,

Have to sort a data table based on date time column which is in the format yyyy-mm-dd hh:mm:ss

Also time appears as a next line in the same column. Any solution please

I have used the below code but its not working. Without time

(From row In dtResult Order By DateTime.ParseExact(row.Item(“Column control menuUpdatedSort”).ToString, “yyyy-mm-dd hh:mm:ss”,
System.Globalization.CultureInfo.InvariantCulture) Descending Select row).CopyToDataTable

Thanks
Teenu

I tried out this code, but not working

(From row In dtResult Order By DateTime.ParseExact(row.Item(“Column control menuUpdatedSort”).ToString, (“yyyy-MM-dd HH:mm:ss”),
System.Globalization.CultureInfo.InvariantCulture) Descending Select row).CopyToDataTable

hi @ teenu
try this
(From row In filteredSchemaDT Order By DateTime.ParseExact(row.Item(“RECEIPTDATE”).ToString, “dd/MM/yyyy”,
System.Globalization.CultureInfo.InvariantCulture) Descending Select row).CopyToDataTable

I tried the same code
(From row In dtResult Order By DateTime.ParseExact(row.Item(“Column control menuUpdatedSort”).ToString, “yyyy-MM-dd HH:mm:ss”,
System.Globalization.CultureInfo.InvariantCulture) Descending Select row).CopyToDataTable

It gives me error “String was not recognized as a valid DateTime”

@Teenu

please show the Date time which is written in the excel any

There might be issue with datetime format

Is your column date similar to “yyyy-MM-dd HH:mm:ss” this format. And make sure in-between date and time is having only one space

I get the value in the column field of the data table as 2019-03-04 09:26:32

“yyyy-MM-dd hh:mm:ss” try this.

will try and let you know

No, this format also not working

When printing that column name value- I get as

2019-03-04 09:27:43
03-04 09:27
8mo

From row In datatable.Select Order By Convert.ToInt32(row(“Column1”)) Descending Select row).ToArray.CopyToDatatable()

(From row In dtResult.Select Order By Convert.ToInt32(row(“Column control menuUpdatedSort”)) Descending Select row).ToArray.CopyToDatatable()

I used the above code and it says “Input String in not correct format”

How to extract only DateTime value from the below value:

“2019-03-04 09:26:32\r\n\r\n03-04 09:26\r\n\r\n8mo”

How to extract only DateTime value from the below value:

“2019-03-04 09:26:32\r\n\r\n03-04 09:26\r\n\r\n8mo”

I think you have created a separate post for this issue. You can find answer there

System.Text.RegularExpressions.Regex.Match(“2019-03-04 09:26:32\r\n\r\n03-04 09:26\r\n\r\n8mo”, “[0-9]{4}-[0-9]{2}-[0-9]{2}\s[0-9]{2}:[0-9]{2}:[0-9]{2}”).Value

@Teenu try this

(From p In dataTable Order By DateTime.ParseExact(p.Item(“Date”).ToString.SubString(0,10), “dd/MM/yyyy”, System.Globalization.CultureInfo.InvariantCulture) Ascending Select p).CopyToDataTable… It worked for me.

Thank you Madhavi, I will try this and let you know
Thanks a lot for your time

“date” column is a String type here

Teja

Still it gives me error “String was not recognized as a valid DateTime”

(From row In dtResult Order By DateTime.ParseExact(row.Item(“Column control menuUpdatedSort”).ToString.SubString(0,10), “dd/MM/yyyy”, System.Globalization.CultureInfo.InvariantCulture) Ascending Select row).CopyToDataTable

where “Column control menuUpdatedSort” is the column name where the date has to be extracted which is of string data type