I got the answer to this problem. It was actually occurring due to improper datatype conversion at the SQL Server end. By mistake i was passing ex.innerException into a field of datatype nvarchar. This is why the exception was occurring.
Hence the final point here is: if you try to pass some parameter from any language to SQL server, you must make sure that the datatypes are matching or interoprable with each other.