Yasskier
Yasskier

Reputation: 811

From database to datetime picker

I have a form with few datetime pickers, to be exact two pairs of dates and times.

enter image description here

Data from those pickers is combined and saved to database as DateTime in format

 16/05/2012 11:28:00 a.m.

Now, what I want to do is get value from database and put it into datetime pickers.

I've tried something like

string plannedDate =(dbFunctions.SQLReadColumn("task", "PlannedDate", TaskID));
        DateTime pDate = new DateTime(plannedDate.Substring(0,9);
        dtpDatePicker.Value=pDate;

where plannedDate contains string data in format as mentioned above, but I can't convert it to DateTime (second line incorrect). Is there an easy way to do it or do I have to cut the string into pieces for year, month etc?

Upvotes: 1

Views: 9273

Answers (3)

Try

dateTimePicker1.Value = Convert.ToDateTime( gridView1.GetRowCellValue(rowHandle, "CDATE").ToString());

Upvotes: 0

Thomas C. G. de Vilhena
Thomas C. G. de Vilhena

Reputation: 14595

I personally like the DateTime.TryParse method in situations you may receive an invalid string as an input but don't want a exception to be throw:

DateTime dateValue;
if (DateTime.TryParse(dateString, out dateValue))
{
    // Conversion succedded
}
else
{
    // Conversion failed
}

Upvotes: 3

Nikhil Agrawal
Nikhil Agrawal

Reputation: 48600

Do like this

dtpDatePicker.Value = Convert.ToDateTime(plannedDate.Substring(0, 10)); 

Similarly for Time

mytimepicker.Value = Convert.ToDateTime(plannedDate.Substring(10, 6));

Upvotes: 5

Related Questions