-
Notifications
You must be signed in to change notification settings - Fork 314
Closed
Description
Describe the bug
Using Microsoft.Data.SqlClient 5.2.0, executing a query on a connection with an InfoMessage
event handler and the FireInfoMessageEventOnUserErrors
property set to true results in an immediate NullReferenceException being thrown.
Exception message:
Object reference not set to an instance of an object.
Stack trace:
System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.Data.SqlClient.SqlCommand.GetCurrentBatchCommand()
at Microsoft.Data.SqlClient.TdsParser.FireInfoMessageEvent(SqlConnection connection, SqlCommand command, TdsParserStateObject stateObj, SqlError error)
at Microsoft.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
at Microsoft.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean isAsync, Int32 timeout, Boolean asyncWrite)
at Microsoft.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, Boolean sendToPipe, Int32 timeout, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry, String methodName)
at Microsoft.Data.SqlClient.SqlCommand.ExecuteNonQuery()
To reproduce
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.InfoMessage += (sender, e) =>
{
foreach (SqlError error in e.Errors)
{
Console.WriteLine("Received from SQL Server: " + error.Message);
}
};
connection.FireInfoMessageEventOnUserErrors = true;
try
{
connection.Open();
using (SqlCommand command = new SqlCommand())
{
command.Connection = connection;
command.CommandType = CommandType.Text;
command.CommandText = @"
PRINT 'Starting process...';
PRINT 'This is an informational message.';
SELECT 1/0;
PRINT 'Ending process...';";
command.ExecuteNonQuery();
}
}
catch (SqlException ex)
{
Console.WriteLine("SQL Exception: " + ex.Message);
}
}
Expected behavior
Received from SQL Server: Starting process...
Received from SQL Server: This is an informational message.
Received from SQL Server: Divide by zero error encountered.
Received from SQL Server: Ending process...
Further technical details
Microsoft.Data.SqlClient version: 5.2.0
.NET target: .NET 6
SQL Server version: SQL Server 2019 (RTM-CU25)
Operating system: Windows 10
Additional context
When I tried executing the same exact code on version 5.1.4, I get the expected behavior.
Metadata
Metadata
Assignees
Labels
No labels