Skip to content

SqlParameterCollection.Add() gives confusing error message when mixing drivers #1546

@madelson

Description

@madelson

Describe the bug

Trying to add a System.Data.SqlClient.SqlParameter to a Microsoft.Data.SqlClient.SqlParameterCollection gives this error message:

The SqlParameterCollection only accepts non-null SqlParameter type objects, not SqlParameter objects.

This mistake can come up when migrating to the new driver or when using the new driver for ADO.NET Code and the old driver when working with EF6. If you're aware of the situation and history of the two drivers it is easy to intuit what this message means, but for the uninitiated this can be pretty confusing.

To reproduce

var command = new Microsoft.Data.SqlClient.SqlCommand();
command.Parameters.Add(new System.Data.SqlClient.SqlParameter());

Expected behavior

A message like the following would be more clear:

The SqlParameterCollection only accepts non-null Microsoft.Data.SqlClient.SqlParameter type objects, not System.Data.SqlClient.SqlParameter objects.

This seems trivial to fix; just changing

=> InvalidCast(StringsHelper.GetString(Strings.ADP_CollectionInvalidType, collection.Name, itemType.Name, invalidValue.GetType().Name));
to use Type.ToString()/Type.FullName instead of Type.Name for the expected and actual types. I'd be happy to submit a PR if there is interest.

Further technical details

Microsoft.Data.SqlClient version: 4.1.0
.NET target: net5.0
SQL Server version: n/a
Operating system: Windows 10

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions