471,596 Members | 879 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,596 software developers and data experts.

Datetime convert

Hi,

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

OR

Convert.ToDateTime(DataBinder.Eval(Container.DataI tem, "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


Dec 22 '05 #1
2 2866
SimonZ <si*********@studio-moderna.com> wrote:
can someone explain me, when to use:
(DateTime)DataBinder.Eval(Container.DataItem, "dateField")

OR

Convert.ToDateTime(DataBinder.Eval(Container.DataI tem, "dateField"))

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

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?

--
Jon Skeet - <sk***@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
Dec 22 '05 #2
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.

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
SimonZ <si*********@studio-moderna.com> wrote:
can someone explain me, when to use:
(DateTime)DataBinder.Eval(Container.DataItem, "dateField")

OR

Convert.ToDateTime(DataBinder.Eval(Container.DataI tem, "dateField"))

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

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?

--
Jon Skeet - <sk***@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

Dec 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

38 posts views Thread by nobody | last post: by
3 posts views Thread by Mark | last post: by
26 posts views Thread by Reny J Joseph Thuthikattu | last post: by
5 posts views Thread by iulian.ilea | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by Anwar ali | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.