locked
Datetime problems... RRS feed

  • Question

  • Hi, I am trying to set up a scheduling type of function in a web application using c#. Basically, the user picks a date on a calendar and then selects an option in a dropdownlist. The dropdownlist is populated like:

     

    00:30

    01:30

    02:00

    and so on....

     

    I then take that data and insert it into a sql database as 1 column. I first tried parsing to strings etc but ran into a whole heap of problems. Now i'm simply truying:

     

    // Get the date and times selected

    dtSelected = Calendar1.SelectedDate;

    dtSelected = DateTime.Parse(DropDownList1.SelectedItem.Text);

     

    This seems to reset the date part back to its default. So for example, when the screen loads i tell the calendar to display today:

     

    // Initial page load events

    if (!IsPostBack)

    {

    // Set the calendar and label for currently selected date

    Calendar1.SelectedDate = DateTime.Today;

    Label1.Text = Calendar1.SelectedDate.ToShortDateString();

    dtSelected = Calendar1.SelectedDate;

    }

     

    If the user selects another date, then the date doesnt change when submitted.

     

    What is the easiest way to do this? Have a calendar, and a dropdown list of times. Then create a datetime type and assign a date to it, and then add just the time onto the end?

     

    Thanks!

    Sunday, August 26, 2007 6:54 PM

Answers

  • You need to make a combination of two entry controls. Here is two ways how to do it:

    1.  DateTime dateSelected = Calendar1.SelectedDate;

         TimeSpan time = TimeSpan.Parse(DropDownList1.SelectedItem.Text);
         DateTime dateForInsert = dateSelected.Add(time);

    2. 
    DateTime dateForInsert = DateTime.ParseExact(Calendar1.SelectedDate.ToString("MM/dd/yyyy ") + DropDownList1.SelectedItem.Text, "MM/dd/yyyy HH:mm", CultureInfo.InvariantCulture);


    In SQL Server 2008 which comes out in February 2008, you will have posibility of puting two entries in separate columns. So you will have column for date only and column for time only parts.

    Sunday, August 26, 2007 9:19 PM

All replies

  • You need to make a combination of two entry controls. Here is two ways how to do it:

    1.  DateTime dateSelected = Calendar1.SelectedDate;

         TimeSpan time = TimeSpan.Parse(DropDownList1.SelectedItem.Text);
         DateTime dateForInsert = dateSelected.Add(time);

    2. 
    DateTime dateForInsert = DateTime.ParseExact(Calendar1.SelectedDate.ToString("MM/dd/yyyy ") + DropDownList1.SelectedItem.Text, "MM/dd/yyyy HH:mm", CultureInfo.InvariantCulture);


    In SQL Server 2008 which comes out in February 2008, you will have posibility of puting two entries in separate columns. So you will have column for date only and column for time only parts.

    Sunday, August 26, 2007 9:19 PM
  • Thanks for that, I've always wondered why sql server doesnt have a date and a time column stored seperatly. But thanks, that worked!

    Monday, August 27, 2007 7:37 PM