Search code examples
c#sqlvb.netexceptionexecutequery

Exception on ExecuteNonQuery method about unsuccessful value convertion


i am using OleDbCommand.ExecuteNonQuery() to insert data into the database:

ObjCommand = New OleDbCommand
ObjCommand.CommandType = CommandType.StoredProcedure
ObjCommand.CommandText = StrSQL

ObjCommand.Parameters.Add("field1", OleDbType.VarChar).Value = <something1>
ObjCommand.Parameters.Add("field", OleDbType.VarChar).Value = <something2>
(...)
(...)    
ObjCommand.Parameters.Add("field50", OleDbType.VarChar).Value = <something50>

ObjCommand.Connection = GetDBConnection(StrConnectionString)
ObjCommand.Connection.Open()


<some integer> = ObjCommand.ExecuteNonQuery()

And there is a conversion exception that only shows up in the last line:

error converting datatype varchar to smallint

I'd like to know if it's the normal behavior, and how do i know where the conversion problem occurs.

update:

ObjCommand.Parameters.Add("@IdMunFatoGerador", OleDbType.VarChar).Value 
                = ObjNFe.idMunFatoGerador

i've found this line through commenting every line and uncommenting some, this one gives me the above said exception.

ObjNFe.idMunFatoGerador is a string and gives me "error converting datatype varchar to smallint" too


Solution

  • I've finally found it.

    It was everything ok with the formats of the values. The problem was: one of the parameters was missing. I still didn't understand it completely, but the issue was that the missing parameter (smallint) was interpreted in the following one (varchar) and so the error i found was in the second one.

    In other words, field~35 was missing (haha)

    So the thing is: when mounting a command to a procedure, remember to always put the fields in the exact amount and order. =)

    Thank you guys!