Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Set value of datetimepicker to value from database on form load

Status
Not open for further replies.

ljwilson

Programmer
May 1, 2008
65
US
I am struggling with this. I have a datetimepicker. I have code to get data from database on form load. All that works just fine. What won't work is for the datetimepickers to be set to the date I get back from the DB. I can do a MessageBox.Show on the variable that holds the date coming back from the DB and it looks like this:

1/14/2010

But the datetimepicker control still shows todays date (the default date). I can't for the life of me get it to show the date coming back from the DB. Here is what I got:

Code:
If Not IsDBNull(dsProtocolInformation.Tables(0).Rows(0).Item("App_Date")) Then
                        Me.App_DateDateTimePicker.Value = dsProtocolInformation.Tables(0).Rows(0).Item("App_Date")
                        ' FIXME
                        MessageBox.Show(mycount.ToString + " | " + dsProtocolInformation.Tables(0).Rows(0).Item("App_Date"))
                    End If

Here is how the datetimepicker is formatted:
ddd MM/dd/yyyyyy

I have tried changing the format to "Short" but still have same issue.

Any ideas?
 
Is the date an actual date field or is it a string. If it is a string the you might have to do something like.

Me.App_DateDateTimePicker.Value = [red]"#" &[/red] dsProtocolInformation.Tables(0).Rows(0).Item("App_Date") [red]& "#"[/red]

or maybe

Me.App_DateDateTimePicker.Value = cType(dsProtocolInformation.Tables(0).Rows(0).Item("App_Date"), Date)


That will force it to be a date and should allow the picker to use it as long as it is a correct date format. You might have to format it first.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
That didn't work. I tried something else:

Code:
If Not IsDBNull(dsProtocolInformation.Tables(0).Rows(0).Item("App_Date")) Then
                                                Me.App_DateDateTimePicker.Value = CType(dsProtocolInformation.Tables(0).Rows(0).Item("App_Date"), Date)
                        ' FIXME
                        MessageBox.Show(Me.App_DateDateTimePicker.Value)
                    End If

That messagebox shows the correct date but the field on the form still shows the default date. If I go to another form and then go back to this form, it works correctly. It is only on the first load of the form that it won't update the field even though it thinks the value of that field is the correct date.

Thanks!

LJ
 
My two cents. Sounds like the value might be changing after the code you listed. I would check the value of this control at the end of the form load. If it's what you think it should be, some other event is changing it after the load. You might have to step through the code one line at a time. Or, I would disable all other code one sub or function at a time until you find the culprit.

Auguy
Northwest Ohio
 
The problem turned out to be this:

Code:
Me.App_DateDateTimePicker.Text = ""

Even though this property setting was happening BEFORE the value setting, it apparently breaks the datapicker somehow.

Once I removed that line, it works like a charm.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top