473,473 Members | 2,039 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Dates from Sql

I have a dataset with a date in it. The type is smalldatetime on Sql Server
and I am just doing a "SELECT * from x" to get it.

All my other fields are fine, but this one gives me an error of:

Specified cast is not valid.

The data that is coming back is:

2005-05-09 17:52:59.923

My C# line is:

dateCreated = (string)user["DateCreated"];

I also tried:

dateCreated = (DateTime)user["DateCreated"];

How am I supposed to set this to get a date that I haven't converted to a
VarChar?

Thanks,

Tom
Nov 17 '05 #1
13 1885
string datecreated = user["DateCreated"].ToString();

Nov 17 '05 #2
Or the following if you want a DateTime:
DateTime dtDateCreated = DateTime.Parse(user["DateCreated"].ToString());

"Kevin" <ke***@inatrice.co.za> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
string datecreated = user["DateCreated"].ToString();

Nov 17 '05 #3
"Kevin" <ke***@inatrice.co.za> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
string datecreated = user["DateCreated"].ToString();


That works and is how I would do it on a web page to a field on the page.

But if I am going to carry the date around in an object so that I would
access it something like "x.DateCreated", is it better to carry it as a
string or as DateTime?

Thanks,

Tom
Nov 17 '05 #4
That's really up to you.
I would probable carry it around as a string if I'm not going to do any
calculations so to speak on the date itself. But you could easily cast
back and forth.

Mark was perfectly correct.
You could also use Convert.ToDateTime(user["DateCreated"].ToString());

Nov 17 '05 #5
"Mark White" <ma*******@yahoo.com> wrote in message
news:OT**************@TK2MSFTNGP15.phx.gbl...
Or the following if you want a DateTime:
DateTime dtDateCreated = DateTime.Parse(user["DateCreated"].ToString());
That would work.

The question I asked in the other post:

Is it better to carry a date in an object as a string or as a DateTime?

In Sql, you would obviously carry it as smalldatetime or datetime and
convert it to the format you want when you query it.

Would it be the same for objects/classes?

Thanks,

Tom
"Kevin" <ke***@inatrice.co.za> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
string datecreated = user["DateCreated"].ToString();


Nov 17 '05 #6
tshad <ts**********@ftsolutions.com> wrote:
"Kevin" <ke***@inatrice.co.za> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
string datecreated = user["DateCreated"].ToString();


That works and is how I would do it on a web page to a field on the page.

But if I am going to carry the date around in an object so that I would
access it something like "x.DateCreated", is it better to carry it as a
string or as DateTime?


If you ever need to manipulate it as a DateTime, I'd use it as that. If
you *only* need to fetch it from one source as a string and pass it to
another as a string, without validation, it would be reasonable to
leave it as a string.

--
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
Nov 17 '05 #7
Hi,
Is it better to carry a date in an object as a string or as a DateTime?


It depends, if you will always treat the date as a string, will never save
it back to the DB and will always use it in the same format then use a
string, will save you conversion.

If you need to perform any date operation or you need to show it in
different format over the course of the app ( "12/29/05" , "Dec 31 2005" ,
etc ) it's better if you keep it as a date.
cheers,

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

Nov 17 '05 #8
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
tshad <ts**********@ftsolutions.com> wrote:
"Kevin" <ke***@inatrice.co.za> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
> string datecreated = user["DateCreated"].ToString();
That works and is how I would do it on a web page to a field on the page.

But if I am going to carry the date around in an object so that I would
access it something like "x.DateCreated", is it better to carry it as a
string or as DateTime?


If you ever need to manipulate it as a DateTime, I'd use it as that. If
you *only* need to fetch it from one source as a string and pass it to
another as a string, without validation, it would be reasonable to
leave it as a string.


That, of course, is the question.

Since I am setting up an object that can be used anywhere, I would have no
way of knowing how it is going to be used. I am just setting up an object
to be used.

Of course, you can easily cast it to a DateTime later and put it in the
format you want.

Thanks,

Tom
--
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

Nov 17 '05 #9
tshad <ts**********@ftsolutions.com> wrote:
That, of course, is the question.

Since I am setting up an object that can be used anywhere, I would have no
way of knowing how it is going to be used. I am just setting up an object
to be used.
In that case, I'd use a DateTime. That way you don't need to specify
things like the format of the string.
Of course, you can easily cast it to a DateTime later and put it in the
format you want.


Well, you can parse it, to be precise. You can't actually cast from
string to DateTime.

--
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
Nov 17 '05 #10
Tshad,

System.Net.DateTime, and SQL server DateTime and SQL server smallDateTime
use the same principle it are ticks in units. (which are different in Net
and SQL).

They have however other starting points
Net uses the first day of the Christian calendar
DateTime in SQL server is starts at the introduction of the Georgian
Calendar in the British empire (1753)
SmallDateTime starts at the first century that the computer was used (1900)

(The parse an convert will set them to the right dates).

I would try to use inside a Net program forever only to work with Net
DateTimes. By instance in Canada you will than avoid problems that you will
have when you use strings, because by instance in that countries two
different string representations are used for Date and times. Those are set
correct by using the ToString functions in Net programs to the ones that the
clients use when you want to represent them.

I hope this gives some ideas,

Cor

Nov 17 '05 #11
"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:em**************@TK2MSFTNGP10.phx.gbl...
Tshad,

System.Net.DateTime, and SQL server DateTime and SQL server smallDateTime
use the same principle it are ticks in units. (which are different in Net
and SQL).

They have however other starting points
Net uses the first day of the Christian calendar
DateTime in SQL server is starts at the introduction of the Georgian
Calendar in the British empire (1753)
SmallDateTime starts at the first century that the computer was used
(1900)

(The parse an convert will set them to the right dates).

I would try to use inside a Net program forever only to work with Net
DateTimes. By instance in Canada you will than avoid problems that you
will have when you use strings, because by instance in that countries two
different string representations are used for Date and times. Those are
set correct by using the ToString functions in Net programs to the ones
that the clients use when you want to represent them.

I hope this gives some ideas,


I'm a little confused here. I am still using dates in Sql not just in .Net.

Are you saying then that I should do which (assuming that somedate is
smalldatetime or datetime on Sql Server):

Select someDate from table1
string dateCreated = user["someDate"].ToString();

or

Select someDate from table1
DateTime dateCreated= DateTime.Parse(user["someDate"].ToString());

or some other way.

Thanks,

Tom
Nov 17 '05 #12
Thad,

Does this piece of code using the Northwind database help you?

SqlConnection conn = new SqlConnection
("Server = YourServer; DataBase = NorthWind;Integrated Security =
sspi;");
string SqlString =
"Select BirthDate from Employees Where EmployeeId = 1";
SqlCommand cmd = new SqlCommand(SqlString,conn);
conn.Open();
DateTime dt = (DateTime)cmd.ExecuteScalar();
MessageBox.Show(dt.ToString());
conn.Close();

I hope this helps,

Cor

"tshad" <ts**********@ftsolutions.com> schreef in bericht
news:%2****************@TK2MSFTNGP14.phx.gbl...
"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:em**************@TK2MSFTNGP10.phx.gbl...
Tshad,

System.Net.DateTime, and SQL server DateTime and SQL server smallDateTime
use the same principle it are ticks in units. (which are different in Net
and SQL).

They have however other starting points
Net uses the first day of the Christian calendar
DateTime in SQL server is starts at the introduction of the Georgian
Calendar in the British empire (1753)
SmallDateTime starts at the first century that the computer was used
(1900)

(The parse an convert will set them to the right dates).

I would try to use inside a Net program forever only to work with Net
DateTimes. By instance in Canada you will than avoid problems that you
will have when you use strings, because by instance in that countries two
different string representations are used for Date and times. Those are
set correct by using the ToString functions in Net programs to the ones
that the clients use when you want to represent them.

I hope this gives some ideas,


I'm a little confused here. I am still using dates in Sql not just in
.Net.

Are you saying then that I should do which (assuming that somedate is
smalldatetime or datetime on Sql Server):

Select someDate from table1
string dateCreated = user["someDate"].ToString();

or

Select someDate from table1
DateTime dateCreated= DateTime.Parse(user["someDate"].ToString());

or some other way.

Thanks,

Tom

Nov 17 '05 #13
Tshad,

Sorry in your sample

DateTime dateCreated= (DateTime) user["someDate"];
Cor

"Cor Ligthert [MVP]" <no************@planet.nl> schreef in bericht
news:eq**************@TK2MSFTNGP10.phx.gbl...
Thad,

Does this piece of code using the Northwind database help you?

SqlConnection conn = new SqlConnection
("Server = YourServer; DataBase = NorthWind;Integrated Security =
sspi;");
string SqlString =
"Select BirthDate from Employees Where EmployeeId = 1";
SqlCommand cmd = new SqlCommand(SqlString,conn);
conn.Open();
DateTime dt = (DateTime)cmd.ExecuteScalar();
MessageBox.Show(dt.ToString());
conn.Close();

I hope this helps,

Cor

"tshad" <ts**********@ftsolutions.com> schreef in bericht
news:%2****************@TK2MSFTNGP14.phx.gbl...
"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:em**************@TK2MSFTNGP10.phx.gbl...
Tshad,

System.Net.DateTime, and SQL server DateTime and SQL server
smallDateTime use the same principle it are ticks in units. (which are
different in Net and SQL).

They have however other starting points
Net uses the first day of the Christian calendar
DateTime in SQL server is starts at the introduction of the Georgian
Calendar in the British empire (1753)
SmallDateTime starts at the first century that the computer was used
(1900)

(The parse an convert will set them to the right dates).

I would try to use inside a Net program forever only to work with Net
DateTimes. By instance in Canada you will than avoid problems that you
will have when you use strings, because by instance in that countries
two different string representations are used for Date and times. Those
are set correct by using the ToString functions in Net programs to the
ones that the clients use when you want to represent them.

I hope this gives some ideas,


I'm a little confused here. I am still using dates in Sql not just in
.Net.

Are you saying then that I should do which (assuming that somedate is
smalldatetime or datetime on Sql Server):

Select someDate from table1
string dateCreated = user["someDate"].ToString();

or

Select someDate from table1
DateTime dateCreated= DateTime.Parse(user["someDate"].ToString());

or some other way.

Thanks,

Tom


Nov 17 '05 #14

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

Similar topics

8
by: Riley | last post by:
The date fields being saved by a VB program were being saved as #2003-11-22#. For reasons unknown to me these dates began to be saved as "11/22/2003" All of these dates were made dates with the...
7
by: Alistair | last post by:
diary_date = request.form("diary_date") - (from a populated drop down list) strSQL = "SELECT saz_title, saz_text from saz_details where saz_date =#" & diary_date & "#" a response.write...
5
by: PW | last post by:
<rant> Sorry guys, but I just have to whinge. Dates in ASP are a total pain in the butt! I seem to get caught out so many times. I realise its my own fault, but going from the posts in this...
10
by: Colin Steadman | last post by:
I'm a stupid ASP programmer and I dont do Javascript (except for very simple tasks anyway), and I'm in a bit of a predicament. I've used a javascript table sorting script from here: ...
1
by: Don Sealer | last post by:
I have a report that includes 5 different subreports. I'd like to be able to open this report using a date function (Start Date and End Date). I'd like all five subreports to show the data from...
2
by: Rachel Suddeth | last post by:
Is there a way to have the non-selectable dates (those before MinDate and after MaxDate) draw differently so my users can see right away what dates aren't allowed? I'm not seeing it... ...
12
by: Dixie | last post by:
I am trying to calculate the number of workdays between two dates with regards to holidays as well. I have used Arvin Meyer's code on the Access Web, but as I am in Australia and my date format is...
1
by: pitfour.ferguson | last post by:
My dbase has the start date and end date of each visit. How can I ask Access to list the day of the week of the start (easy), end (easy) and, more importantly, the dates of the visit itself - ie...
7
by: evilcowstare via AccessMonster.com | last post by:
Hi, I have searched the forum for answers on this and to be honest as a novice I find it a bit confusing so apologies if it is simple. There are some searches that I want to apply to my database....
2
by: Jim Carlock | last post by:
(1) Does PHP provide any way to handle dates prior to 1980? I know there's problems with Microsoft Windows NT and all Windows NT operating systems will allow a date prior to 1980 to be placed...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.