Click here to Skip to main content
15,918,041 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I am fetching data from database in datetable's object called dt.
I want to convert that string in to dateTimePicker 's format .
I have try below code but its not working .

dt is object of DataTable

dtpPlanDate.Value = DateTime.ParseExact(dt.Rows[0]["PlanSDate"].ToString(), "yyyy-MM-dd HH:mm:ss,fff", System.Globalization.CultureInfo.InvariantCulture);


it gives me this error "String was not recognized as a valid DateTime."

I also tried other format as below
dtpPlanDate.Value = Convert.ToDateTime(dt.Rows[0]["PlanSDate"].ToString());

it gives me error
Could not determine the order of year, month, and date from 'mm/dd/yyyy'.

I also tried one more format

dtpPlanDate.Value =DateTime.Parse(dt.Rows[0]["PlanSDate"].ToString());


it gives me below error

Could not determine the order of year, month, and date from 'mm/dd/yyyy'.



please let me know right format
Posted
Comments
Sergey Alexandrovich Kryukov 29-Feb-12 3:06am    
Just run it under debugger and see what's in input data, does it fit assumed format or not. Put some effort. There are no any secrets which could help you. Or make a code sample and post it. "Improve question" is above.
--SA

try this
DateTime dt = Convert.ToDateTime(DateTime.Now, System.Globalization.CultureInfo.CreateSpecificCulture("en-us").DateTimeFormat); 



you can give culture like en-us(united states),en-IN(india) like this
 
Share this answer
 
Comments
kp_livewire 29-Feb-12 2:33am    
I have write

dateTimePicker1.Value = Convert.ToDateTime(DateTime.Now, System.Globalization.CultureInfo.CreateSpecificCulture(dt.Rows[0]["SpouseDOB"].ToString()).DateTimeFormat);


Error,

The name '46/17/2012 4:46:01 PM' contains characters that are not valid for a Culture or Region.
Parameter name: name
kp_livewire 29-Feb-12 2:36am    
Dear Thanks , Can you please let me know .soon . you can drop mail on kinrankumarvpatel@gmail.com
You can use the DateTime.TryParse method[^] to ensure that the input you pass in is a valid datetime.
 
Share this answer
 
C#
IFormatProvider provider = new System.Globalization.CultureInfo("en-CA", true);
DateTime fDate = DateTime.Parse(urTxtBoxId.Value, provider, System.Globalization.DateTimeStyles.NoCurrentDateDefault);

and while passing to database use
C#
fDate.ToString() overloads 
e.g.fDate.ToString("MMM d yyyy");
 
Share this answer
 
in your comment
>The name '46/17/2012 4:46:01 PM' contains characters that are not valid for a >Culture or Region.
>Parameter name: name

'46/17/2012' isn't a valid date.
make your data correctly,and try again to confirm where problem is.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900