CreateParameterCopy CopyTo InvalidCastException

Topics: Data Access Application Block
Jul 15, 2014 at 2:39 AM
Greetings,
I am attempting to extend the Enterprise Library 6 DAAB to Teradata. I have a unit test which is failing due to the following error:

An exception of type 'System.InvalidCastException' occurred in Teradata.Client.Provider.dll but was not handled in user code. Additional information: Unable to cast object of type 'System.Data.IDataParameter[]' to type 'Teradata.Client.Provider.TdParameter[]'.

The error originates in a private static method called CreateParameterCopy within the ParameterCache class, which resides within the base DAAB block. The full method is provided below for context, but the error occurs with the line parameters.CopyTo(parameterArray, 0);
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);
        }
The object parameters is actually of type Teradata.Client.Provider.TdParameterCollection. Per the MSDN documentation for the CopyTo method: “The type of the source ICollection cannot be cast automatically to the type of the destination array.” Hence the exception.

I believe the problem can be remedied if the following code is substituted for CreateParameterCopy (I have tested this with a new class derived from ParameterCache):
    private static IDataParameter[] CreateParameterCopy(DbCommand command)
    {
        IDataParameterCollection parameters = (IDataParameterCollection)command.Parameters;
        IDataParameter[] parameterArray = new IDataParameter[parameters.Count];

        int counter = -1;
        foreach (var parameter in parameters)
        {
            parameterArray[++counter] = (IDataParameter)parameter;
        }
        return CachingMechanism.CloneParameters(parameterArray);
    }
Any chance a patch could be released?
Jul 15, 2014 at 6:24 AM
Data Access Application Block is open source and community contributions are invited; you could submit a Contributor License Agreement and submit a pull request.

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to