Click here to Skip to main content
Sign Up to vote bad
good
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 - 20:48
Edited 14 Jan '13 - 21:22

Comments
OriginalGriff - 15 Jan '13 - 2:54
What do you mean "does not work some time?" What does it do when it "doesn't work"?
varun150 - 15 Jan '13 - 3:05
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 - 15 Jan '13 - 3:10
error occurs at following code: DateTime date = DateTime.ParseExact(txtdate.Text, "dd/MM/yyyy", null);
Sergey Alexandrovich Kryukov - 15 Jan '13 - 2:55
No, please provide exact steps how to reproduce the problem. —SA
varun150 - 15 Jan '13 - 3:14
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 - 15 Jan '13 - 3:14
error occurs at following code: DateTime date = DateTime.ParseExact(txtdate.Text, "dd/MM/yyyy", null);
Sergey Alexandrovich Kryukov - 15 Jan '13 - 10:41
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 - 19 Jan '13 - 6:53
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 - 19 Jan '13 - 17:32
Well, right, then use ParseExact/TryParseExact, to parse from the format known in advance. That's it. —SA

1 solution

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 - 15 Jan '13 - 3:35
thank you so much
OriginalGriff - 15 Jan '13 - 3:49
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
Your Filters
Interested
Ignored
     
0 OriginalGriff 233
1 Sergey Alexandrovich Kryukov 208
2 Rohan Leuva 195
3 Abhinav S 168
4 Mahesh Bailwal 165
0 Sergey Alexandrovich Kryukov 8,474
1 OriginalGriff 6,714
2 CPallini 3,603
3 Rohan Leuva 2,853
4 Maciej Los 2,234


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