Hi i keep getting an exception when i'm trying to update table values within a database. the exception is below but i dont change from datetime to varchar it always stay as datetime.

is there any other reasons why this is happening ?

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

objtyp.dtForm  = Convert.ToDateTime(reader["dtFrom"]);

public DateTime dtForm

string SQL = "UPDATE TABLE_NAME SET dtFrom = '" + objtyp.dtForm + "' WHERE ObjectTypeID = 1";



7 Years
Discussion Span
Last Post by ChrisHunter

You can see from the exception, that your conversation is not ok.
Try to do:

DateTime dateDate = Convert.ToDateTime(reader["dtFrom"]); //column "dtFrom" has to be type of DateTime!

but as it seems from your excetion, your column "dtFrom" is a type of varchar (so string, and no DateTime).
So you can do:

string strDate = (string)reader["dtFrom"];
//if you want some further "date" changing (from whole date-time value to only date for example) you can do:
DateTime dateDate = Convert.ToDateTime(strDate);
strDate = String.Format("{0:MM.dd.yyyy}", dateDate);
strDate = dateDate.ToShortDateString();

The point is that you have wrong type of columns in the dataBase. If you have dates, times, the type has to be a "DateTime", and notihng else (its very desired). And if we skip this issue, and go back to yours here, conversations arent good enought, and mostly becuase of wrong column types.

Hope this explains your problem - but my code above shoud salve them all anyway - regarldess on wring column types.


Edited by Mitja Bonca: n/a


The Value of dtForm of the column is definatly DateTime. For some reason the column values are formatted like so '000-00-00 00:00:00.000', but will only accept '000-00-00' when updating the column values.

I don't know what the issue is, if it was a format issue it wouldn't allow such a value to be stored in the first place would it ?

I'm okay just storing '000-00-00' for now but would like to know the solution if possible.

Thank you for your help so far.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.