DateTime conversion

Hi,

I am trying to load data from a text file and one of the column is Date string of format ... "20050331".

I have a class which reads a line from the file and returns a StringCollection. ANd then I make a new System.Data.DataRow with all these strings and add it to the DataTable in which the Column dataTypes are defined and then add the row to the Table.

These are the lines of code which does the above...
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
RowStrings is a String Collection...
NewRow is DataRow..

For Each RowString As String In RowStrings
                    Try
                       
                        NewRow(Count) = System.Convert.ChangeType(RowString, IndexedTable.Columns(Count).DataType)
                    Catch EX As Exception
                        ' Catch the exception
                    End Try

                    Count = Count + 1
 Next
IndexedTable.Rows.Add(NewRow)

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
But when it reads the string "20050331"  I get an exception as " String was not recognized as a valid DateTime".

I am not sure how to convert these strings to Datetime as the above class is very generic to rad any file with any number of columns....

Can someone give a n idea of how to read these strings as DateTime...

Thanks in advance..

K.
yarlapatiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

entrapnetCommented:
you need to set your string as follow into to turn into datatetime

1) mm/dd/yyyy
2) dd-mmm-yyyy (1-Jan-2004)
3) yyyy-mm-dd (2004-1-1)
entrapnetCommented:
*datatetime = datetime
entrapnetCommented:
base on ur data... i do recommended that you wrote a function like below

        Dim a As DateTime
        Dim b As String

        b = "20050331"
        a = Left(b, 4) + "-" + Mid(b, 5, 2) + "-" + Right(b, 2)
heintalusCommented:
You could try something like this, remember to add an imports statement to the top of your class for System.Globalization

For Each RowString As String In RowStrings
                    Try
                        If IndexedTable.Columns(Count).DataType = System.Type.GetType("System.DateTime") Then
                            NewRow(Count) = DateTime.ParseExact(RowString, "yyyyMMdd", new DateTimeFormatInfo());
                        Else
                            NewRow(Count) = System.Convert.ChangeType(RowString, IndexedTable.Columns(Count).DataType)
                        End If
                    Catch EX As Exception
                        ' Catch the exception
                    End Try

                    Count = Count + 1
 Next
IndexedTable.Rows.Add(NewRow)

HTH
Andy

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.