emilios
emilios

Reputation: 375

Conversion of a varchar data type to a datetime data type resulted in an out-of-range value

I have the following SQL Server error in ASP.net C#:

The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.

My SQL query is:

string query = @"update Conference set Conference_name='" 
    + name + "',AIMS='" + AIMS + "',Scope='" + scope 
    + "',Submission_Deadline='" + submissionDay 
    + "',Notification_Deadline='" + notificationDay 
    + "',Camera_Ready_submission='" + cameraReadyDay 
    + "',Early_authors_registration='" + earlyAuthorsDay 
    + "',Start_Date='" + startDay + "',End_Date='" + endDay 
    + "',Location='" + location 
    + "' where Conference_ID= '" + id+"'";

Upvotes: 0

Views: 1534

Answers (4)

kochobay
kochobay

Reputation: 392

This is related with c# ToString() behaviour. Due to "culture" of your application, ToString() method was defaulted to a value.

"2011 jan 13" can be stringified "2011-01-13" or "2011-13-01" due to your app culture setting.

And this may result in error when trying to find 13. month of year.

To overcome this issue, i recommend to use SqlCommand and set parameters...

please see http://www.knowdotnet.com/articles/dynamicsqlparameters.html

Example can be easily converted to c#.

Upvotes: 0

sfuqua
sfuqua

Reputation: 5853

Perhaps it is time to add some validation. May I suggest the RangeValidator, with type set to Date?

Upvotes: 1

PaulStock
PaulStock

Reputation: 11263

I think your query is OK, it's your data that is bad. One of the date fields (startDay, EndDay, NotificationDay) does not look like a valid date.

Upvotes: 1

kmcc049
kmcc049

Reputation: 2801

one of your values for a date, probably start or end date is not able to be converted to a datetime. Would help to see the data.

Upvotes: 0

Related Questions