Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
madelson opened this issue Mar 14, 2022 · 1 comment · Fixed by #1547
Closed

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

madelson opened this issue Mar 14, 2022 · 1 comment · Fixed by #1547

Comments

@madelson
Copy link
Contributor

madelson commented Mar 14, 2022

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

@JRahnama
Copy link
Contributor

@madelson thanks for bringing this up. We will discuss this internally and will get back to you soon.

ErikEJ added a commit to ErikEJ/SqlClient that referenced this issue Mar 15, 2022
…tion

(to help conversions from System.Data.SqlClient)

fixes dotnet#1546
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants