Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / December 2005

Tip: Looking for answers? Try searching our database.

Datetime convert

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
SimonZ - 22 Dec 2005 08:53 GMT
Hi,

can someone explain me, when to use:
(DateTime)DataBinder.Eval(Container.DataItem, "dateField")

OR

Convert.ToDateTime(DataBinder.Eval(Container.DataItem, "dateField"))

Sometimes (DateTime) works sometimes not, on the other hand
Convert.ToDateTime() always works.

The same problem is with (int16) and Convert.ToInt16

Thanks,S
Jon Skeet [C# MVP] - 22 Dec 2005 09:16 GMT
> can someone explain me, when to use:
> (DateTime)DataBinder.Eval(Container.DataItem, "dateField")
[quoted text clipped - 7 lines]
>
> The same problem is with (int16) and Convert.ToInt16

When you cast, the runtime type of the object must be exactly correct
(leaving conversion operators aside for the moment as they're not
relevant in your example).

When you use Convert.ToDateTime(object), the object just has to be of
some type which implements IConvertible and does the appropriate thing
with IConvertible.ToDateTime.

In other words, it depends what DataBinder.Eval actually returns. If it
returns a string, for instance, Convert.ToDateTime will work (because
System.String implements IConvertible and tries to parse the string as
a date/time) but casting won't.

Does that help?

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Ignacio Machin ( .NET/ C# MVP ) - 22 Dec 2005 13:59 GMT
Hi,

An addition to the OP comments, Convert.ToDateTime does not always work,
there are a number (most of them) that raise exception , IIRC only when
received a string it does the conversion.

Signature

Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

>> can someone explain me, when to use:
>> (DateTime)DataBinder.Eval(Container.DataItem, "dateField")
[quoted text clipped - 22 lines]
>
> Does that help?

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.