Timespan Problem

  • Thread starter Thread starter jack
  • Start date Start date
J

jack

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
 
jack said:
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...
 
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);
 
jack said:
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);
 
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...
 
what is the difference between int.parce and toint32() function
(converters)
just wanted to know
Thanks for the help
 
jack said:
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.
 
Back
Top