Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
Error is" String is not a valid datetime string" [ error occurs at following code: DateTime date = DateTime.ParseExact(txtdate.Text, "dd/MM/yyyy", null); ]

 

DateTime date = DateTime.ParseExact(txtdate.Text, "dd/MM/yyyy", null);
Convert.ToDateTime(date).ToShortDateString();
Posted 14-Jan-13 21:48pm
Edited 14-Jan-13 22:22pm
v2
Comments
OriginalGriff at 15-Jan-13 2:54am
   
What do you mean "does not work some time?" What does it do when it "doesn't work"?
varun150 at 15-Jan-13 3:05am
   
when i post date from the text box to database it gives an error i.e. string was not a valid datetime object something like this i don't remember it exactly.
but i refresh the page and post the same date again it shows no error
varun150 at 15-Jan-13 3:10am
   
error occurs at following code:
DateTime date = DateTime.ParseExact(txtdate.Text, "dd/MM/yyyy", null);
Sergey Alexandrovich Kryukov at 15-Jan-13 2:55am
   
No, please provide exact steps how to reproduce the problem.
—SA
varun150 at 15-Jan-13 3:14am
   
there's no exact step i think this error occurs sometime only,
[
when i post date from the text box to database it gives an error i.e. string was not a valid datetime object something like this i don't remember it exactly.
but i refresh the page and post the same date again it shows no error
]
varun150 at 15-Jan-13 3:14am
   
error occurs at following code:
DateTime date = DateTime.ParseExact(txtdate.Text, "dd/MM/yyyy", null);
Sergey Alexandrovich Kryukov at 15-Jan-13 10:41am
   
Of course, not any text can parse to some pattern, isn't it obvious?
You should have recorded the data for 'bad' case, show the code and the data...
—SA
varun150 at 19-Jan-13 6:53am
   
not every text but data from calander extender ajex toolkit in this ("dd/MM/yyyy")format should be parsed , tell me if i m wrong
Sergey Alexandrovich Kryukov at 19-Jan-13 17:32pm
   
Well, right, then use ParseExact/TryParseExact, to parse from the format known in advance. That's it.
—SA

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

I think you are a little confused - the second line of your code doesn't do anything:
DateTime date = DateTime.ParseExact(txtdate.Text, "dd/MM/yyyy", null);
Convert.ToDateTime(date).ToShortDateString();
But it is important to note that ToShortDateString does not return a specific, fixed format - particularly it does not return a string which is suitable for use with SQL. It returns a string which is dependant on the Culture that is set in teh machine, where SQL prefers to get 'yyyy-MM-dd' format dates if you hand them through as a string.
So two things:
First, hand your date through to SQL as a DateTime via a parametrized query (you should be using these at all times anyway to prevent an SQL injection attack which can accidentally or deliberately destroy your database). Don't convert dates to streings unless it s for presentation to the user!
 
Second, use TryParseExact instead - it reports a problem and allows you to log it and look at why it happened in better detail as well as report the problem to the user.
 
I'm wondering if the problem is to do with the DateTime format set on the Client machine - are you sure the date is in dd/MM/yyyy format? Have you checked?
  Permalink  
Comments
varun150 at 15-Jan-13 3:35am
   
thank you so much
OriginalGriff at 15-Jan-13 3:49am
   
You 're welcome!

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 545
1 OriginalGriff 285
2 Shweta N Mishra 244
3 Mathew Soji 225
4 BillWoodruff 200
0 OriginalGriff 6,153
1 Sergey Alexandrovich Kryukov 5,758
2 DamithSL 4,958
3 Manas Bhardwaj 4,539
4 Maciej Los 3,735


Advertise | Privacy | Mobile
Web01 | 2.8.1411019.1 | Last Updated 15 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100