简体   繁体   中英

How can i know which parameter is causing the FormatException (“Input string was not in a correct format”) on an OracleCommand Insert?

I have a problem in which I have a huge insert with tons of parameters.

I am using the OracleCommand object to add my parameters prior on executing the query.

Later I execute ExecuteNonQuery() method and it gives the FormatException ("Input string was not in a correct format").

My question is, is there a way I can know which parameter is causing the Exception?

My code looks something like this

using (OracleConnection conn = new OracleConnection(_connString))

{

      conn.open();

      using(var cmd = conn.CreateCommand())

      {
          cmd.CommandType = CommandType.Text;

          cmd.CommandText = insert (..., ... ,...) into ..... values (nameOfParameter, ..., .... ,...);

          cmd.Parameters.Clear();


          cmd.Parameters.Add("nameOfParameter", OracleDBType.SomeTypeOfData, valueOfParameter, ParameterDirection.Input)

          //Lots of Parameter Adding

          cmd.ExecuteNonQuery();
      }


}

You need to check every single parameter you are passing to your method.

if (parameter1 == null)
    logger.debug("parameter1 is null");
if (parameter2 == null)
    logger.debug("parameter2 is null");
// ...
// You may do something else rather than only logging.

You can enclose your code in a

try { 
     // entire db transaction code goes here
} catch(Exception e) {
    // you can log the exception and (cross finger) will give you more details 
}

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