472,334 Members | 1,523 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Convert.ToDateTime

I have datetime variable:

Datetime tsEndTime;

Should I use (DateTime):
tsEndTime=(DateTime)rdr.GetValue(15)

or is better to use:

tsEndTime=Convert.ToDateTime(rdr.GetValue(15))

What is the difference?

Thanks,S
Nov 17 '05 #1
5 18530
Simon,

I think you should use:

tsEndTime = rdr.GetDateTime(15); // no cast or conversion necessary

Of the two versions you mention, the first

tsEndTime=(DateTime) rdr.GetValue(15);

is more efficient, as the cast does not incur the penalty of the
(unnecessary) call to Convert.ToDateTime in the second version.

Regards - Octavio
"simon" <si*********@iware.si> escribió en el mensaje
news:n_*******************@news.siol.net...
I have datetime variable:

Datetime tsEndTime;

Should I use (DateTime):
tsEndTime=(DateTime)rdr.GetValue(15)

or is better to use:

tsEndTime=Convert.ToDateTime(rdr.GetValue(15))

What is the difference?

Thanks,S

Nov 17 '05 #2
"Octavio Hernandez" <do****@danysoft.com> wrote in message
news:eO**************@TK2MSFTNGP14.phx.gbl...

Since you're already referring to the column in question by its ordinal
number within the field collection, I'd use:
tsEndTime = rdr.GetDateTime(15);
Nov 17 '05 #3
Octavio,

thank you for your answer.

When I use =(DateTime) rdr.GetValue(15), it's not always the right result.
For example: If my stored procedure returns time: '14:20:00' then this cast
will return error.
If I use convert then it returns me dateTime format.

Another example:

My stored procedure returns small int for example 0.

If I use:

int16 varI;

varI=(int16)rdr.GetValue(10) I get an error.

If I use:

varI=convert.toInt16(rdr.GetValue(10)) then it works.

So, I decided that I use everywhere convert function, unless at string data.

Do you know maybe, why this difference?

Regards,S
"Octavio Hernandez" <do****@danysoft.com> wrote in message
news:eO**************@TK2MSFTNGP14.phx.gbl...
Simon,

I think you should use:

tsEndTime = rdr.GetDateTime(15); // no cast or conversion necessary

Of the two versions you mention, the first

tsEndTime=(DateTime) rdr.GetValue(15);

is more efficient, as the cast does not incur the penalty of the
(unnecessary) call to Convert.ToDateTime in the second version.

Regards - Octavio
"simon" <si*********@iware.si> escribió en el mensaje
news:n_*******************@news.siol.net...
I have datetime variable:

Datetime tsEndTime;

Should I use (DateTime):
tsEndTime=(DateTime)rdr.GetValue(15)

or is better to use:

tsEndTime=Convert.ToDateTime(rdr.GetValue(15))

What is the difference?

Thanks,S


Nov 17 '05 #4
I believe when you do a cast, it's just reinterpret the "binary value"
into the other type, so if it's invalid, it may throw an exception.

But in "Convert" operation, it probably is doing more than one case
under the covers. It could do something which cannot be casted directly.

So, I believe the Convert is more safe, while the case is more efficient.

HTH

simon wrote:
Octavio,

thank you for your answer.

When I use =(DateTime) rdr.GetValue(15), it's not always the right result.
For example: If my stored procedure returns time: '14:20:00' then this cast
will return error.
If I use convert then it returns me dateTime format.

Another example:

My stored procedure returns small int for example 0.

If I use:

int16 varI;

varI=(int16)rdr.GetValue(10) I get an error.

If I use:

varI=convert.toInt16(rdr.GetValue(10)) then it works.

So, I decided that I use everywhere convert function, unless at string data.

Do you know maybe, why this difference?

Regards,S
"Octavio Hernandez" <do****@danysoft.com> wrote in message
news:eO**************@TK2MSFTNGP14.phx.gbl...
Simon,

I think you should use:

tsEndTime = rdr.GetDateTime(15); // no cast or conversion necessary

Of the two versions you mention, the first

tsEndTime=(DateTime) rdr.GetValue(15);

is more efficient, as the cast does not incur the penalty of the
(unnecessary) call to Convert.ToDateTime in the second version.

Regards - Octavio
"simon" <si*********@iware.si> escribió en el mensaje
news:n_*******************@news.siol.net...
I have datetime variable:

Datetime tsEndTime;

Should I use (DateTime):
tsEndTime=(DateTime)rdr.GetValue(15)

or is better to use:

tsEndTime=Convert.ToDateTime(rdr.GetValue(15) )

What is the difference?

Thanks,S



Nov 17 '05 #5
Simon,

a) Writing rdr.GetValue(15) is the same as writing rdr[15] - at runtime,
this may be a string, a DateTime or an int, but at compile time you have an
Object, and you will need the cast (or the conversion) in order for the code
to compile.

b) If you know that the 16th field is a DateTime, you can use
rdr.GetDateTime(15) which returns directly a DateTime (no cast or conversion
necessary).

c) Does your stored procedure produce in the 16th field a SQL Server
DATETIME, or a string which represents a date (and/or a time)? If it is a
string, then the cast to (DateTime) will fail (because the object is a
string, not a DateTime); the call to Convert.ToDateTime() will succeed,
provided that the string really contains the representation of a date/time.

d) Regarding the Int16, if the SP returns a small int the following code:

Int16 varI = rdr.GetInt16(10);

should work.

In general, when using data readers I prefer to rely on the specific methods
(GetInt16, GetDateTime, etc.) whenever I know the structure of the data at
hand. Only if writing some kind of "generic" read I use GetValue():

Regarding the Convert class, I avoid using it and generally succeed on that.
It reminds me of the times of VB6 and the abuse of conversions between
types. But that's only a personal opinion.

Regards - Octavio

"simon" <si*********@iware.si> escribió en el mensaje
news:Ec*******************@news.siol.net...
Octavio,

thank you for your answer.

When I use =(DateTime) rdr.GetValue(15), it's not always the right result.
For example: If my stored procedure returns time: '14:20:00' then this
cast will return error.
If I use convert then it returns me dateTime format.

Another example:

My stored procedure returns small int for example 0.

If I use:

int16 varI;

varI=(int16)rdr.GetValue(10) I get an error.

If I use:

varI=convert.toInt16(rdr.GetValue(10)) then it works.

So, I decided that I use everywhere convert function, unless at string
data.

Do you know maybe, why this difference?

Regards,S
"Octavio Hernandez" <do****@danysoft.com> wrote in message
news:eO**************@TK2MSFTNGP14.phx.gbl...
Simon,

I think you should use:

tsEndTime = rdr.GetDateTime(15); // no cast or conversion necessary

Of the two versions you mention, the first

tsEndTime=(DateTime) rdr.GetValue(15);

is more efficient, as the cast does not incur the penalty of the
(unnecessary) call to Convert.ToDateTime in the second version.

Regards - Octavio
"simon" <si*********@iware.si> escribió en el mensaje
news:n_*******************@news.siol.net...
I have datetime variable:

Datetime tsEndTime;

Should I use (DateTime):
tsEndTime=(DateTime)rdr.GetValue(15)

or is better to use:

tsEndTime=Convert.ToDateTime(rdr.GetValue(15))

What is the difference?

Thanks,S



Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: | last post by:
I have two text boxes; startTimeTextBox.Text = "08:00"; endTimeTextBox.Text = "15:00"; and now I will check if startTimeTextBox.Text starts...
2
by: Franck | last post by:
Hi, 'm gettin mad about date conversion. Here is the point. Got and add-in for Excel which call functions from a web service (on a remote...
19
by: simon | last post by:
I get from the dateTimePicker the value: string="12/18/2003 11:52:28 AM" Now I need to convert this to dateTime. Any function I use:...
3
by: sparkle | last post by:
Hi, Does anybody know how to convert date to ticks from a dateTimePicker? What I'm using now isn't working. I'm trying to use a dateTimePicker...
2
by: SimonZ | last post by:
Hi, can someone explain me, when to use: (DateTime)DataBinder.Eval(Container.DataItem, "dateField") OR ...
5
by: rsanan | last post by:
How do I convert a datetime from en-GB to en-US format here is my code - (not working for the clients outside of US)...
14
by: Me | last post by:
Hi all I am getting a really bizzare error on when I convert a string into a datetime: The code is : DateTime dt1 = Convert.ToDateTime("10...
4
by: tshad | last post by:
Is there any difference between convert.ToDateTime and System.DateTime.Parse? I am using them both and they seem the same. Is one better to...
4
by: =?Utf-8?B?YW5kcmV3?= | last post by:
I am running an ASP.net program written in VB. At one point I try to convert a date string into a date time object... this string is from a central...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...

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.