Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
when Date field is entered minimum date like 01/01/0001 and click on Search then it is displaying error message "The conversion of a varchar data type to a datetime data type resulted in an out-of-range value
Posted

You should use TryParse to see if it's possible to parse users input.

C#
string datestring = "01/01/0001";
DateTime date;
if (DateTime.TryParse(datestring, out date))
{
    // Date parsed ok. All good.
}
else
{
    // Could not parse datestring or the date is out of range. 
    // Tell the user to try other input
}
 
Share this answer
 
Its not problem of your query. i want to tell you that it is by DB.
The date range allowed in Sql

January 1, 1753, through December 31, 9999

and if you using datatype as smalldatetime then

January 1, 1900 through June 6, 2079

Put date within this your query will run.
 
Share this answer
 
Comments
CHill60 26-Apr-13 9:41am    
The OP didn't mention using a database at all - he refers to "Date field is entered "
TnTinMn 26-Apr-13 14:25pm    
In the title - "The conversion of a varchar data type to a datetime data type resulted in an out-of-range value"

That is the error message received when a default parameter is used along with the AddWithValue method with a string value of "01/01/0001" and the destination field is datetime.
Hi uday

string date = "01/01/0001";
DateTime now = Convert.ToDateTime(date);
 
Share this answer
 
Hi guyz, we can use below query before inserting it in to the database.

SET DATEFORMAT dmy INSERT INTO myTable VALUES ('14/1/2011 18:00:40')
 
Share this answer
 
Comments
Deepu S Nair 16-Feb-15 8:19am    
Answering old questions adds nothing and is likely to attract
downvoting.

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