From c2bca3624b46847922d095454ad893d45172a3f6 Mon Sep 17 00:00:00 2001 From: Davoud Date: Thu, 16 May 2024 11:51:09 -0700 Subject: [PATCH] [5.2.1] | Fix SqlConnection.FireInfoMessageEventOnUserErrors when set to true throws an exception (#2399) --- .../Microsoft/Data/SqlClient/SqlCommand.cs | 2 +- .../Microsoft/Data/SqlClient/SqlCommand.cs | 2 +- .../SQL/ConnectivityTests/ConnectivityTest.cs | 24 +++++++++++++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlCommand.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlCommand.cs index 39dbb917e0..d01c686d04 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlCommand.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlCommand.cs @@ -6776,7 +6776,7 @@ internal SqlBatchCommand GetCurrentBatchCommand() } else { - return _rpcArrayOf1[0].batchCommand; + return _rpcArrayOf1?[0].batchCommand; } } diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlCommand.cs b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlCommand.cs index 0ac6ed775e..94cd927c1d 100644 --- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlCommand.cs +++ b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlCommand.cs @@ -7487,7 +7487,7 @@ internal SqlBatchCommand GetCurrentBatchCommand() } else { - return _rpcArrayOf1[0].batchCommand; + return _rpcArrayOf1?[0].batchCommand; } } diff --git a/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/ConnectivityTests/ConnectivityTest.cs b/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/ConnectivityTests/ConnectivityTest.cs index 60260fe79f..bf26f84018 100644 --- a/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/ConnectivityTests/ConnectivityTest.cs +++ b/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/ConnectivityTests/ConnectivityTest.cs @@ -474,5 +474,29 @@ public static void SqlPasswordConnectionTest() using SqlConnection sqlConnection = new(b.ConnectionString); sqlConnection.Open(); } + + [ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.IsNotAzureSynapse))] + public static void ConnectionFireInfoMessageEventOnUserErrorsShouldSucceed() + { + using (var connection = new SqlConnection(DataTestUtility.TCPConnectionString)) + { + string command = "print"; + string commandParam = "OK"; + + connection.FireInfoMessageEventOnUserErrors = true; + + connection.InfoMessage += (sender, args) => + { + Assert.Equal(commandParam, args.Message); + }; + + connection.Open(); + + using SqlCommand cmd = connection.CreateCommand(); + cmd.CommandType = System.Data.CommandType.Text; + cmd.CommandText = $"{command} '{commandParam}'"; + cmd.ExecuteNonQuery(); + } + } } }