George
George

Reputation: 1191

conversion from string yyyyMMdd to type integer is not valid

I have a dataset containing a datatable, and I enumerate all rows in that datatable. When trying to format a column in that row, I run into an exception. (Part of) the code is:

For Each dr As DataRow In ds.Tables("records").Rows
    file = dr("timestamp").ToString("yyyyMMdd") & "~.wav"
Next

This results in the following error message:

Conversion from string yyyyMMdd to type Integer is not valid. (translated from a Dutch error message to the English equivalent)

dr("timestamp").GetType.FullName results in "System.DateTime", so I don't understand why I run into this exception, as for example Now.ToString("yyyyMMdd") results in "20091002", and "Now" is of the same type as dr("timestamp"), "System.DateTime" that is.

Upvotes: 3

Views: 8857

Answers (3)

Simon Morgan
Simon Morgan

Reputation: 2248

I've encountered this issue too, although in my case the value in question was a nullable Date/DateTime.

As well as casting to a Date, as another answer has suggested, you can also call .Value to get a non-nullable value (after making sure it isn't actually null, of course).

Upvotes: 1

MLT
MLT

Reputation: 514

Have you made sure that you are not declaring the variable 'file' as an integer?

Upvotes: -1

Justin Wignall
Justin Wignall

Reputation: 3510

Try

For Each dr As DataRow In ds.Tables("records").Rows
    file = CDate(dr("timestamp")).ToString("yyyyMMdd") & "~.wav"
Next

Upvotes: 16

Related Questions