Skip to content

NullReferenceException with Microsoft.Data.SqlClient 5.2.0 and with event handler for Backup.PercentComplete #162

Open

Description

@geo-var

In our app we reference both NuGet-Packages Microsoft.SqlServer.SqlManagementObjects and Microsoft.Data.SqlClient.
We use SMO for many DB-related tasks, incl. Backup of the app databases.
We refresh the NuGet references regularly, actually we use Microsoft.SqlServer.SqlManagementObjects 171.30.0 and Microsoft.Data.SqlClient 5.1.5.
Everything worked fine until we updated Microsoft.Data.SqlClient to the actual version 5.2.0. Now our backup code throws NullReferenceException. As a workaround we switched back to Microsoft.Data.SqlClient 5.1.5.
We were able to reproduce the issue with the sample from https://learn.microsoft.com/en-us/sql/relational-databases/server-management-objects-smo/tasks/backing-up-and-restoring-databases-and-transaction-logs?view=sql-server-ver16#backing-up-databases-and-transaction-logs-in-visual-c
The only difference is the added event handler for Backup.PercentComplete. With it, the sample throws NullReferenceException when calling Backup.SqlBackup() and using SqlClient 5.2.0. The same code works fine with SqlClient 5.1.5 or earlier.

The sample is attached to the issue.
MS_SMO_BackupBug.zip

Activity

  1. JRahnama commented on Apr 22, 2024

    @JRahnama

    geo-var
    Based on below stack

    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 async, Int32 timeout, Boolean asyncWrite)

    this issue is addressed on SqlClient side by PR #2399. Hotfix release v5.2.1 is in our plan, date yet TBD.

  2. geo-var commented on Apr 23, 2024

    @geo-var
    Author

    Thanks a lot,
    we will test the hotfix when released and will let you know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions