473,748 Members | 2,328 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Convert.ToDateT ime

I have datetime variable:

Datetime tsEndTime;

Should I use (DateTime):
tsEndTime=(Date Time)rdr.GetVal ue(15)

or is better to use:

tsEndTime=Conve rt.ToDateTime(r dr.GetValue(15) )

What is the difference?

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

I think you should use:

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

Of the two versions you mention, the first

tsEndTime=(Date Time) rdr.GetValue(15 );

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

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

Datetime tsEndTime;

Should I use (DateTime):
tsEndTime=(Date Time)rdr.GetVal ue(15)

or is better to use:

tsEndTime=Conve rt.ToDateTime(r dr.GetValue(15) )

What is the difference?

Thanks,S

Nov 17 '05 #2
"Octavio Hernandez" <do****@danysof t.com> wrote in message
news:eO******** ******@TK2MSFTN GP14.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.to Int16(rdr.GetVa lue(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****@danysof t.com> wrote in message
news:eO******** ******@TK2MSFTN GP14.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=(Date Time) rdr.GetValue(15 );

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

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

Datetime tsEndTime;

Should I use (DateTime):
tsEndTime=(Date Time)rdr.GetVal ue(15)

or is better to use:

tsEndTime=Conve rt.ToDateTime(r dr.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.to Int16(rdr.GetVa lue(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****@danysof t.com> wrote in message
news:eO******** ******@TK2MSFTN GP14.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=(Date Time) rdr.GetValue(15 );

is more efficient, as the cast does not incur the penalty of the
(unnecessar y) call to Convert.ToDateT ime in the second version.

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

Datetime tsEndTime;

Should I use (DateTime):
tsEndTime=(D ateTime)rdr.Get Value(15)

or is better to use:

tsEndTime=Co nvert.ToDateTim e(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.ToDateT ime() 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*********@iw are.si> escribió en el mensaje
news:Ec******** ***********@new s.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.to Int16(rdr.GetVa lue(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****@danysof t.com> wrote in message
news:eO******** ******@TK2MSFTN GP14.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=(Date Time) rdr.GetValue(15 );

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

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

Datetime tsEndTime;

Should I use (DateTime):
tsEndTime=(Date Time)rdr.GetVal ue(15)

or is better to use:

tsEndTime=Conve rt.ToDateTime(r dr.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
1409
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 really before endTimeTextBox.Text something like this:
2
5087
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 server) The remote server has regional settings set to "en-UK" and date to
19
8765
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: Convert.ToDateTime(string) or Cdate(string), I get an error message: String was not recognized as a valid DateTime. What should I do? Use replace function and mid function on string and at the end convert it to date? Thank you for your answer, Simon
3
5962
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 to set an appointment in Outlook with a reminder. I need this reminder to remind the recipient 14 days in advance. I'm having problems with the ReminderMinutesBeforeStart. If I simply
2
2985
by: SimonZ | last post by:
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
5
24100
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) /*******************CODE*****************/ System.Globalization.CultureInfo myCI = new System.Globalization.CultureInfo("en-US", true); xwriter.WriteElementString(""xxx", System.DateTime.Parse(Convert.ToDateTime("16/2/2006").ToShortDateString(), myCI).ToString()); /*******************************************/
14
7966
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 Sep 2005"); Console.WriteLine(dt1.Year);
4
35894
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 use than another? Thanks, Tom
4
3019
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 dev server and the code works on many other machines. Convert.ToDateTime("") I get the string in the format MM/DD/YYYY (06/26/2007) The code breaks and I get the "String was not recognized as a valid DateTime" error message unless I manually change the input string to
0
9528
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9359
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9310
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8235
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6072
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4592
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3298
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2774
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2206
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.