Dates from Sql

  • Thread starter Thread starter tshad
  • Start date Start date
T

tshad

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
 
Or the following if you want a DateTime:
DateTime dtDateCreated = DateTime.Parse(user["DateCreated"].ToString());
 
Kevin said:
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
 
Mark White said:
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 said:
string datecreated = user["DateCreated"].ToString();
 
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());
 
tshad said:
Kevin said:
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.
 
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,
 
Jon Skeet said:
tshad said:
Kevin said:
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
 
tshad said:
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.
 
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
 
Cor Ligthert said:
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
 
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 said:
Cor Ligthert said:
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
 
Tshad,

Sorry in your sample

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


Cor

Cor Ligthert said:
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 said:
Cor Ligthert said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top