471,319 Members | 2,675 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Timespan Problem

Hi all i have just begin csharp have a problem in saving time in
database

I have to comboboxex in form one containing hours from 0 to 23
and another containig minutes from 0 to 59
the problem is saving it in to the database

I want to save only the time in to the database but i dont know how to
convert it
wether to use timespan to use datetime datatype
please help

Dec 13 '05 #1
7 5444
jack <ga**********@gmail.com> wrote:
Hi all i have just begin csharp have a problem in saving time in
database

I have to comboboxex in form one containing hours from 0 to 23
and another containig minutes from 0 to 59
the problem is saving it in to the database

I want to save only the time in to the database but i dont know how to
convert it
wether to use timespan to use datetime datatype


It sounds like DateTime would be a better fit for you. I don't know
whether most databases even have a TimeSpan concept. Of course, you
could just store hours*60+minutes as an integer...

--
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 13 '05 #2
I have made it
This is what i found perfectly working
ha ha ..
int year = Convert.ToInt32 ( System.DateTime.Now.Year.ToString ());
int month= Convert.ToInt32 ( System.DateTime.Now.Month.ToString
());
int day = Convert.ToInt32 ( System.DateTime.Now.Day.ToString ());
int hour = Convert.ToInt32 (cmbHour.SelectedItem.Text.ToString ());
int min = Convert.ToInt32 (cmbMin.SelectedItem.Text.ToString ());

DateTime mydate = new DateTime(year,month , day , hour , min ,00);

Dec 13 '05 #3
jack <ga**********@gmail.com> wrote:
I have made it
This is what i found perfectly working
ha ha ..
int year = Convert.ToInt32 ( System.DateTime.Now.Year.ToString ());
int month= Convert.ToInt32 ( System.DateTime.Now.Month.ToString
());
int day = Convert.ToInt32 ( System.DateTime.Now.Day.ToString ());
int hour = Convert.ToInt32 (cmbHour.SelectedItem.Text.ToString ());
int min = Convert.ToInt32 (cmbMin.SelectedItem.Text.ToString ());

DateTime mydate = new DateTime(year,month , day , hour , min ,00);


Why are you converting the year, month and day from an int to a string
and then back again? Similarly, why are you calling ToString() on
SelectedItem.Text (which already returns a string)?

Why not just do:

int hour = int.Parse (cmbHour.SelectedItem.Text);
int min = int.Parse (cmbMin.SelectedItem.Text);

DateTime myDate = new DateTime (DateTime.Now.Year,
DateTime.Now.Month,
DateTime.Now.Day,
hour,
min,
0);

Alternatively:

DateTime myDate = DateTime.Today.AddHours (hour).AddMinutes(minutes);

--
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 13 '05 #4
o o
Thanks this is good , as i said i m a i have just started learning
thanks for the short cut

Thanks once again...

Dec 14 '05 #5
what is the difference between int.parce and toint32() function
(converters)
just wanted to know
Thanks for the help

Dec 14 '05 #6
jack <ga**********@gmail.com> wrote:
what is the difference between int.parce and toint32() function
(converters)
just wanted to know


int.Parse will throw an exception if you give it a null reference.
Convert.ToInt32(string) will return 0.

--
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 14 '05 #7
Thanks for the reply Jon Skeet

I had just experimented this
Thanks once again

Dec 16 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Dirk Reske | last post: by
2 posts views Thread by Jeff Shantz | last post: by
2 posts views Thread by DWalker | last post: by
2 posts views Thread by ucasesoftware | last post: by
2 posts views Thread by Rob | last post: by
5 posts views Thread by prasanta.bhowmik | last post: by
2 posts views Thread by shapper | last post: by
reply views Thread by rosydwin | last post: by

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.