Click here to Skip to main content
15,921,716 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I developed a small windows form application with a datetime picker. When i click a button, the date from datetime picker gets stored to sql database. But dates from 01/02/2012 till 12/02/2012 gets stored. But, when 13/02/2012 is entered an error is shown
"The conversion of a varchar data type to a datetime data type resulted in an out-of-range value".
Posted
Updated 1-Feb-12 3:15am
v2
Comments
ZurdoDev 1-Feb-12 9:08am    
What language is the SQL db. If english and american collation 13/02/2012 is invalid because the first number is a month. Sounds like that is what is happening.
gokulkmp 1-Feb-12 9:47am    
When i changed the system date format, the dates got saved.

You need to check your date time format (could be based on your or the server's regional settings).
Dates from 01 to 12 may be getting stored incorrect as well. Month is getting stored as the first two characters and not the middle ones as would be expected.
 
Share this answer
 
Comments
gokulkmp 1-Feb-12 9:49am    
When i changed the system date format, all dates got saved to database.
Don't convert the DateTime to a string, like you are doing at the moment. Use a Parametrized query, and pass the DateTime directly. That way, you do not have to worry about localised date format (currently dd/MM/yyyy on your PC) versus ISO (yyyy-MM-dd) on SQL Server

C#
SqlCommand cmd = new SqlCommand("INSERT INTO myTable (dateColumn) VALUES (@DT)", con);
cmd.Parameters.AddWithValue("@DT", myDateTimePicker.Value);
 
Share this answer
 
Comments
gokulkmp 1-Feb-12 9:48am    
Thank you, I will try this.

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