Click here to Skip to main content
12,395,192 members (66,341 online)
Rate this:
 
Please Sign up or sign in to vote.
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 3:09am
Comments
Zoltán Zörgő 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
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160721.1 | Last Updated 2 Jan 2013
Copyright © CodeProject, 1999-2016
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