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 dateTimePicker.value from dataSet? 1

Status
Not open for further replies.

smartFly

Programmer
Apr 30, 2001
16
US
I have a dateTimePicker control on a Windows Form, I'd like to set the value equal to a dataSet field. Looks like I need to convert to DateTime, but I'm not sure how. The following errors with 'Cannot implicity convert string to datetime!':

dateTimePicker1.Value = dr["myDate"].ToString();

...and there's not a toDate() function?
 
The quoted myDate should have been bracketed, must've been stripped. dr is a DataRow object based on a DataSet created from an XML document. myDate is simply a field with a date value in mm/dd/yy format.

<myTable>
<myRecord>
<myField1 />
<myField2 />
<myDate>9/11/03</myDate>
</myRecord>
</myTable>

Thanks for the help!
 
Did you try:
Code:
dateTimePicker1.Value = dr(&quot;myDate&quot;);

-pete
 
Yes, trying that I get:

Cannot implicitly convert object to System.DateTime!

Appending .ToString() to the end I get:

Cannot implicitly convert string to System.DateTime!

dateTimePicker1.Value obviously needs a date, looks like I need to convert an object or a string to one but I don't know how.
 
How about:
Code:
dateTimePicker1.Value = (System.DateTime)dr(&quot;myDate&quot;);

-pete
 
Nah, it says 'specified cast is not valid'...
 
runnin out of ideas [lol]
Code:
dateTimePicker1.Value = new System.DateTime(dr(&quot;myDate&quot;));

-pete
 
not that either :-(

'Cannot convert from string to long!'
 
Oh boy, my bad, i just now realized that since your data source is xml it is just a string and not a DataTime from a database column, sorry. Had my head stuck in database access code all day and got a little myopic there.

You need to use the static DateTime.Parse(...) functions to parse that string into a DateTime object. You can try the first one that just takes a single string paramter but you may need to use one of the overloads that also takes a Format object.
[cheers]

-pete
 
Sweet!! Perfect, didn't know that existed, worked awesome, thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top