You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Null reference exception when an event handler for SqlConnection's InfoMessage is added and the FireInfoMessageEventOnUserErrors property is set to true
#2476
Closed
onethoughtman opened this issue
May 1, 2024
· 1 comment
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(SqlConnectionconnection=newSqlConnection(connectionString)){connection.InfoMessage+=(sender,e)=>{foreach(SqlErrorerrorine.Errors){Console.WriteLine("Received from SQL Server: "+error.Message);}};connection.FireInfoMessageEventOnUserErrors=true;try{connection.Open();using(SqlCommandcommand=newSqlCommand()){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(SqlExceptionex){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.
The text was updated successfully, but these errors were encountered:
Describe the bug
Using Microsoft.Data.SqlClient 5.2.0, executing a query on a connection with an
InfoMessage
event handler and theFireInfoMessageEventOnUserErrors
property set to true results in an immediate NullReferenceException being thrown.To reproduce
Expected behavior
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.
The text was updated successfully, but these errors were encountered: