Skip to content

5.2.0: using sqlConnection.FireInfoMessageEventOnUserErrors = true generates "Object reference not set to an instance of an object." exception #2388

@BertDeHaes

Description

@BertDeHaes

Describe the bug

When using sqlConnection.FireInfoMessageEventOnUserErrors = true, a "print" sql command always throws an exception

Exception message:
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 SqlClientBug.Program.Main(String[] args) in E:\NAS2\COBEMAR\2022\dotNet\SqlClientBug\Program.cs:line 31

To reproduce

Include a complete code listing (or project/solution) that we can run to reproduce the issue.

Partial code listings, or multiple fragments of code, will slow down our response or cause us to push the issue back to you to provide code to reproduce the issue.

namespace SqlClientBug
{
   internal class Program
    {
        static void Main(string[] args)
        {
            SqlConnectionStringBuilder sb = new();

            sb.TrustServerCertificate = true;
            sb.DataSource = ".";
            sb.InitialCatalog = "tempdb";
            sb.IntegratedSecurity = true;
            sb.ApplicationName = "SqlClientBug";
            
            using SqlConnection sqlConnection = new(sb.ConnectionString);
            sqlConnection.FireInfoMessageEventOnUserErrors = true;
            sqlConnection.Open();
            using SqlCommand cmd = sqlConnection.CreateCommand();
            cmd.CommandType = System.Data.CommandType.Text;
            cmd.CommandText = "print 'OK';";
            try
            {
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine(ex.Message);
                Console.Error.WriteLine("--------------------");
                Console.Error.WriteLine(ex.StackTrace);
            }
        }
    }
}

Expected behavior

As in previous versions: no Exception

Further technical details

Microsoft.Data.SqlClient version: 5.2.0
.NET target: .NET 8.0
SQL Server version: SQL Server 2022
Operating system: Windows 11

Additional context
No problem when using Microsoft.Data.SqlClient version 5.1.5

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status

    Closed

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions