Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: SQL-Server-2008
the below statement shows
p1 = new SqlParameter("@empenddate", SqlDbType.Date);
      //p1.Value = DateTime.Parse(txtenddate.Text);
       p1.Value = Convert.ToDateTime(txtenddate.Text);//error
       cmd.Parameters.Add(p1);
Posted 2-Jan-13 4:09am
Comments
Zoltán Zörgő at 2-Jan-13 9:11am
   
And what exactly was in txtenddate.Text at that time? And what is the locale of the system that runs the code?

1 solution

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

Solution 1

Instead of using the Convert class, I'd suggest using DateTime.Parse[^] wrapped in a try..catch block.
 
While the error may still remain since it's dependent on the input, this would give you more information about the problem.
 
Also a good practice is to validate the input first. You can use for example TryParse[^] to test if the input is convertible to datetime.
  Permalink  

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 8,478
1 OriginalGriff 6,516
2 Peter Leow 3,587
3 Zoltán Zörgő 3,406
4 Richard MacCutchan 2,397


Advertise | Privacy | Mobile
Web04 | 2.8.150123.1 | Last Updated 2 Jan 2013
Copyright © CodeProject, 1999-2015
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