简体   繁体   中英

Why converting datetime to string gets me the error : ORA-01843: not a valid month

I have the following column :

CheckingTime
------------
1 7/5/2011
2 
3 
4 5/8/2012

(the colomn format is date.) I'm using a datareader to read all colomns and rows in DB1 and insert them into DB 2 as follow :

while (dr.Read())
{
string finalratingdate = ((dr[19] != DBNull.Value) ? Convert.ToString(dr[19]) : "");
}

So when inserting it's supposed to check if there's a DBNull, if not, insert the value, if yes insert "", but the thing is I get the following error :

System.Data.OracleClient.OracleException: ORA-01843: not a valid month

I only get this error with datetimes variable. All other variables i'm able to convert them to string and insert them.

Do you have any idea ?

Thank you

I believe the exception is indicating that your column in the database is of type Date and it can't accommodate empty string. Instead of Empty String in your statement, you should initialize your variable with null , that will enable you to set null value in Date column. So your statement would be:

string finalratingdate = ((dr[19] != DBNull.Value) ? Convert.ToString(dr[19]) : null);

I am not really sure why are you converting a DateTime field to string for database, you should use Parameterized query and add the DataTime object as parameter instead of converting it to string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM