473,382 Members | 1,726 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 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 2968
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Nikola | last post by:
Hi all, I have a problem converting datetime to integer (and than back to datetime). Depending whether the time is AM or PM, same date is converted to two different integer representations,...
16
by: PK9 | last post by:
I have a string variable that holds the equivalent of a DateTime value. I pulled this datetime from the database and I want to strip off the time portion before displaying to the user. I am...
38
by: nobody | last post by:
I know that given a FormatString and a DateTime you can use DateTime.ToString(...) to convert the DateTime to a String. My question is how can you turn that around? Given a String and a...
3
by: Mark | last post by:
Hey everybody. Is there an easy way in vb.net to convert the local time of a machine to US CST ? My machines are distributed throughout and the server is requesting we send the time of the txns...
26
by: Reny J Joseph Thuthikattu | last post by:
Hi, I have a variabe in the format of 'DD-MON-YYYY HH:MI AM' .I want to add a miniute to it.How can i do that? by manipulation i want to make '01-JUNE-2004 11:59 PM' to '02-JUNE-2004 12:00 AM'...
11
by: Tim | last post by:
Hi, I am trying to do something simple. Convert a string date to datetime but it is not working and is giving me a baffling error! System.Convert.ToDateTime("Jan 30, 2006")...
5
by: iulian.ilea | last post by:
Is correct to have a varchar field and insert dates of type dd/mm/yyyy into it? I choose this method because I have an application that runs on more than one server. So, if I used a datetime field...
7
by: groups | last post by:
This is my first foray into writing a generic method and maybe I've bitten off more than I can chew. My intent is to have a generic method that accepts a value name and that value will be...
2
by: Mike | last post by:
Hi, I have DateTime filed in db table. I'd like to search my db based on DateTime filed, actually to list all rows with particular date, for e.g. 16.11.2007... I send string to method for....
1
Manikgisl
by: Manikgisl | last post by:
But the problem is we have dates in Varchar instead Datetime While Converting Varchar To Datetime All four formats are unable to Convert ie select Convert(Datetime,'18-11-2008 2:35:19...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.