Skip to content

System.NullReferenceException: 'Object reference not set to an instance of an object.' on FireInfoMessageEventOnUserErrors = true #2502

@h17117

Description

@h17117

Describe the bug

Info message event is broken by version upgrade from 5.1.5 to 5.2.0

Exception message: System.NullReferenceException: 'Object reference not set to an instance of an object.'

Stack trace:
   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()
   at ConsoleApp1.Program.Main(String[] args) in C:\Users\Master\source\repos\Example1\ConsoleApp1\Program.cs:line 19

To reproduce

Complete code:

using Microsoft.Data.SqlClient;

namespace ConsoleApp1
{
    internal class Program
    {
        static void Main(string[] args)
        {
            using (SqlConnection sqlConnection = new SqlConnection("integrated security=true; encrypt=false"))
            {
                sqlConnection.InfoMessage += (s, e) =>
                {
                    Console.WriteLine(e.Message);
                };
                sqlConnection.FireInfoMessageEventOnUserErrors = true;
                sqlConnection.Open();
                using (SqlCommand c = new SqlCommand("backup database master", sqlConnection))
                {
                    c.ExecuteNonQuery();
                }
            }
        }
    }
}

Expected behavior

Info message to be fired instead of Exception.

Further technical details

Microsoft.Data.SqlClient version: (5.2.0)
.NET target: (Core 8, 4.6.2, 4.8)
SQL Server version:
Operating system: Windows

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Closed

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions