1
Vote

DAAB ParameterCache CreateParameterCopy InvalidCastException

description

Reposting this concern here in compliance with the Enterprise Library Project Guidelines.

The method CreateParameterCopy within the DAAB class ParameterCache throws an InvalidCastException when attempting to copy Teradata parameters (TdParameterCollection). The issue arises with the CopyTo method, which per the MSDN documentation: “The type of the source ICollection cannot be cast automatically to the type of the destination array.”
private static IDataParameter[] CreateParameterCopy(DbCommand command)
        {
            IDataParameterCollection parameters = (IDataParameterCollection)command.Parameters;
            IDataParameter[] parameterArray = new IDataParameter[parameters.Count];
            parameters.CopyTo(parameterArray, 0);

            return CachingMechanism.CloneParameters(parameterArray);
        }
It appears that a foreach loop is instead required. I'll try my hand at submitting a pull request, but have never done this before so I might get lost along the way. :)

comments

kriewall wrote Jul 23, 2014 at 5:04 PM

In case someone can get to this before me (it appears that my company's approval process for open source contributions is incredibly long), the following method may be substituted for the method above. I have tested this with my library extension and it appears to work fine:
        private static IDataParameter[] CreateParameterCopy(DbCommand command)
        {
            IDataParameterCollection parameters = (IDataParameterCollection)command.Parameters;
            IDataParameter[] parameterArray = new IDataParameter[parameters.Count];

            for (int i = 0; i < parameters.Count; i++)
            {
                parameterArray[i] = (IDataParameter)parameters[i];
            }

            return CachingMechanism.CloneParameters(parameterArray);
        }