From c6cfde24e10f89970ecaadafb4a080b02fff6ad6 Mon Sep 17 00:00:00 2001 From: Erik Ejlskov Jensen Date: Tue, 15 Mar 2022 17:44:13 +0100 Subject: [PATCH 1/4] Improve error message when adding wrong type to to SqlParameterCollection (to help conversions from System.Data.SqlClient) fixes #1546 --- .../src/Microsoft/Data/Common/AdapterUtil.cs | 2 +- .../FunctionalTests/SqlParameterCollectionTest.cs | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/Common/AdapterUtil.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/Common/AdapterUtil.cs index 8db89b51b7..d7f6ce7cf0 100644 --- a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/Common/AdapterUtil.cs +++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/Common/AdapterUtil.cs @@ -784,7 +784,7 @@ internal static IndexOutOfRangeException CollectionIndexString(Type itemType, st => IndexOutOfRange(StringsHelper.GetString(Strings.ADP_CollectionIndexString, itemType.Name, propertyName, propertyValue, collection.Name)); internal static InvalidCastException CollectionInvalidType(Type collection, Type itemType, object invalidValue) - => InvalidCast(StringsHelper.GetString(Strings.ADP_CollectionInvalidType, collection.Name, itemType.Name, invalidValue.GetType().Name)); + => InvalidCast(StringsHelper.GetString(Strings.ADP_CollectionInvalidType, collection.Name, itemType.FullName, invalidValue.GetType().FullName)); internal static ArgumentException ConvertFailed(Type fromType, Type toType, Exception innerException) => ADP.Argument(StringsHelper.GetString(Strings.SqlConvert_ConvertFailed, fromType.FullName, toType.FullName), innerException); diff --git a/src/Microsoft.Data.SqlClient/tests/FunctionalTests/SqlParameterCollectionTest.cs b/src/Microsoft.Data.SqlClient/tests/FunctionalTests/SqlParameterCollectionTest.cs index e93dbcc196..76043dc350 100644 --- a/src/Microsoft.Data.SqlClient/tests/FunctionalTests/SqlParameterCollectionTest.cs +++ b/src/Microsoft.Data.SqlClient/tests/FunctionalTests/SqlParameterCollectionTest.cs @@ -110,5 +110,15 @@ public void CollectionValiateNull_Throws() Assert.Throws(() => collection.Add(null)); } + + [Fact] + public void CollectionAddInvalidType_Throws() + { + SqlCommand command = new SqlCommand(); + SqlParameterCollection collection = command.Parameters; + + InvalidCastException ex = Assert.Throws(() => collection.Add(new SqlCommand())); + Assert.Contains("Microsoft.Data.SqlClient.SqlCommand", ex.Message, StringComparison.OrdinalIgnoreCase); + } } } From 66762ef5fd3830bbee2ff032b9fea2262408efe7 Mon Sep 17 00:00:00 2001 From: Erik Ejlskov Jensen Date: Wed, 16 Mar 2022 07:28:46 +0100 Subject: [PATCH 2/4] Fix broken tests --- .../ManualTests/SQL/ParameterTest/ParametersTest.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/ParameterTest/ParametersTest.cs b/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/ParameterTest/ParametersTest.cs index fe0f1eb4a9..0910a07f5b 100644 --- a/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/ParameterTest/ParametersTest.cs +++ b/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/ParameterTest/ParametersTest.cs @@ -38,7 +38,7 @@ public static void CodeCoverageSqlClient() DataTestUtility.AssertThrowsWrapper(() => failValue = opc["@p1"].ParameterName, "A SqlParameter with ParameterName '@p1' is not contained by this SqlParameterCollection."); } - DataTestUtility.AssertThrowsWrapper(() => opc.Add(null), "The SqlParameterCollection only accepts non-null SqlParameter type objects."); + DataTestUtility.AssertThrowsWrapper(() => opc.Add(null), "The SqlParameterCollection only accepts non-null Microsoft.Data.SqlClient.SqlParameter type objects."); opc.Add((object)new SqlParameter()); IEnumerator enm = opc.GetEnumerator(); @@ -63,7 +63,7 @@ public static void CodeCoverageSqlClient() DataTestUtility.AssertThrowsWrapper(() => new SqlCommand().Parameters.Add(p), "The SqlParameter is already contained by another SqlParameterCollection."); - DataTestUtility.AssertThrowsWrapper(() => opc.Remove(null), "The SqlParameterCollection only accepts non-null SqlParameter type objects."); + DataTestUtility.AssertThrowsWrapper(() => opc.Remove(null), "The SqlParameterCollection only accepts non-null Microsoft.Data.SqlClient.SqlParameter type objects."); string pname = p.ParameterName; p.ParameterName = pname; @@ -95,11 +95,11 @@ public static void CodeCoverageSqlClient() new SqlCommand().Parameters.CopyTo(new object[0], 0); Assert.False(new SqlCommand().Parameters.GetEnumerator().MoveNext(), "FAILED: Expected MoveNext to be false"); - DataTestUtility.AssertThrowsWrapper(() => new SqlCommand().Parameters.Add(0), "The SqlParameterCollection only accepts non-null SqlParameter type objects, not Int32 objects."); + DataTestUtility.AssertThrowsWrapper(() => new SqlCommand().Parameters.Add(0), "The SqlParameterCollection only accepts non-null Microsoft.Data.SqlClient.SqlParameter type objects, not System.Int32 objects."); - DataTestUtility.AssertThrowsWrapper(() => new SqlCommand().Parameters.Insert(0, 0), "The SqlParameterCollection only accepts non-null SqlParameter type objects, not Int32 objects."); + DataTestUtility.AssertThrowsWrapper(() => new SqlCommand().Parameters.Insert(0, 0), "The SqlParameterCollection only accepts non-null Microsoft.Data.SqlClient.SqlParameter type objects, not System.Int32 objects."); - DataTestUtility.AssertThrowsWrapper(() => new SqlCommand().Parameters.Remove(0), "The SqlParameterCollection only accepts non-null SqlParameter type objects, not Int32 objects."); + DataTestUtility.AssertThrowsWrapper(() => new SqlCommand().Parameters.Remove(0), "The SqlParameterCollection only accepts non-null Microsoft.Data.SqlClient.SqlParameter type objects, not System.Int32 objects."); DataTestUtility.AssertThrowsWrapper(() => new SqlCommand().Parameters.Remove(new SqlParameter()), "Attempted to remove an SqlParameter that is not contained by this SqlParameterCollection."); } From 53e08132985bd1127292106201c8e0414c44bdcf Mon Sep 17 00:00:00 2001 From: Erik Ejlskov Jensen Date: Wed, 16 Mar 2022 19:58:06 +0100 Subject: [PATCH 3/4] fix test fail --- .../tests/ManualTests/SQL/ParameterTest/ParametersTest.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/ParameterTest/ParametersTest.cs b/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/ParameterTest/ParametersTest.cs index 0910a07f5b..8338b3f8c0 100644 --- a/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/ParameterTest/ParametersTest.cs +++ b/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/ParameterTest/ParametersTest.cs @@ -38,7 +38,7 @@ public static void CodeCoverageSqlClient() DataTestUtility.AssertThrowsWrapper(() => failValue = opc["@p1"].ParameterName, "A SqlParameter with ParameterName '@p1' is not contained by this SqlParameterCollection."); } - DataTestUtility.AssertThrowsWrapper(() => opc.Add(null), "The SqlParameterCollection only accepts non-null Microsoft.Data.SqlClient.SqlParameter type objects."); + DataTestUtility.AssertThrowsWrapper(() => opc.Add(null), "The SqlParameterCollection only accepts non-null SqlParameter type objects."); opc.Add((object)new SqlParameter()); IEnumerator enm = opc.GetEnumerator(); @@ -63,7 +63,7 @@ public static void CodeCoverageSqlClient() DataTestUtility.AssertThrowsWrapper(() => new SqlCommand().Parameters.Add(p), "The SqlParameter is already contained by another SqlParameterCollection."); - DataTestUtility.AssertThrowsWrapper(() => opc.Remove(null), "The SqlParameterCollection only accepts non-null Microsoft.Data.SqlClient.SqlParameter type objects."); + DataTestUtility.AssertThrowsWrapper(() => opc.Remove(null), "The SqlParameterCollection only accepts non-null SqlParameter type objects."); string pname = p.ParameterName; p.ParameterName = pname; From 21ddf8101c5ce5c9fd378ed68f35e0dba8a99266 Mon Sep 17 00:00:00 2001 From: Erik Ejlskov Jensen Date: Wed, 16 Mar 2022 21:23:12 +0100 Subject: [PATCH 4/4] Add Assert --- .../tests/ManualTests/SQL/ParameterTest/ParametersTest.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/ParameterTest/ParametersTest.cs b/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/ParameterTest/ParametersTest.cs index 8338b3f8c0..a213a8e148 100644 --- a/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/ParameterTest/ParametersTest.cs +++ b/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/ParameterTest/ParametersTest.cs @@ -36,6 +36,8 @@ public static void CodeCoverageSqlClient() DataTestUtility.AssertThrowsWrapper(() => failValue = opc[0].ParameterName, "Invalid index 0 for this SqlParameterCollection with Count=0."); DataTestUtility.AssertThrowsWrapper(() => failValue = opc["@p1"].ParameterName, "A SqlParameter with ParameterName '@p1' is not contained by this SqlParameterCollection."); + + DataTestUtility.AssertThrowsWrapper(() => opc["@p1"] = null, "A SqlParameter with ParameterName '@p1' is not contained by this SqlParameterCollection."); } DataTestUtility.AssertThrowsWrapper(() => opc.Add(null), "The SqlParameterCollection only accepts non-null SqlParameter type objects.");