Invalid cast from ‘System.String’ to ‘System.TimeSpan….

May 19, 2007 at 3:46 am 1 comment

I have already mentioned in one of my previous blog articles, I am working on a web application which has been converted from 1.1 to 2.0. And because of the many minor changes made in the 2.0 framework, I keep encountering many exceptions!!!

One such exception was raised while making use of Convert.ChangeType method.

When i tried to convert a string to timespan an InvalidCastException was raised. Reason??? In this framework, the method does not support this type of a conversion….

There is the option of using Parse method or TryParse method in TimeSpan class… (TryParse is a newly added method which is very much similar to the Parse method but this doesn’t throw an exception when the conversion fails).

Another thing to keep in mind while using this method is that it doesn’t handle nullables.


Entry filed under: .net, .net 2.0, C#, exceptions.

Localization (a brief introduction) Why Precompile???

1 Comment Add your own

  • 1. Martin  |  July 14, 2011 at 6:41 am

    Annoying innit!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed

May 2007
« Apr   Jun »

%d bloggers like this: