Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Comments

@onethoughtman
Copy link

onethoughtman commented May 1, 2024

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.

@JRahnama
Copy link
Member

JRahnama commented May 2, 2024

@onethoughtman this issue is addressed by PR #2399.

Closing as a duplicate of #2388

@JRahnama JRahnama closed this as completed May 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants