Skip to content

SqlCommand.Cancel throws NullReferenceException when Connection is null #4327

Closed
1 of 1 issue completed

Description

@smophat7

Describe the bug

After transitioning from v6.1.5 to v7.0.0, SqlCommand.Cancel now throws a NullReferenceException if SqlCommand.Connection is null.

Exception:

System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.Data.SqlClient.SqlCommand.Cancel()
   at Program.<Main>$(String[] args) in /<path-to-project>/Program.cs:line 6

The line that causes the exception is a log statement near the beginning of the Cancel method. This PR included a change from Connection?.ClientConnectionId (source) to _activeConnection.ClientConnectionId (source), omitting the null-conditional operator, which was correctly retained in the Clone method's analogous log statement.

To reproduce

See this repo for a complete reproduction of the issue.

In summary, this following code behaves differently when using v7.0.0 compared to v6.1.5:

using Microsoft.Data.SqlClient;

SqlCommand command = new();
try
{
    command.Cancel();
    Console.WriteLine("Success!");
}
catch (Exception e)
{
    Console.WriteLine(e);
}

Expected behavior

Expected in v7.0.0: SqlCommand.Cancel() does not throw and is essentially a no-op
Actual in v7.0.0: SqlCommand.Cancel() throws a NullReferenceException.

Further technical details

Microsoft.Data.SqlClient version: v7.0.0
.NET target: .NET 10.0
SQL Server version: N/A
Operating system: Fedora Linux 44 (KDE Plasma Desktop Edition) - Linux version 7.0.6-200.fc44.x86_64

Additional context
The incorrect behavior is confirmed to still be present in v7.1.0-preview1.26124.5 (latest at time of writing).

Sub-issues

Sub-issues

1 of 1 Issues completed
Loading 1 sub-issues

Activity

  1. github-actions commented on May 30, 2026

    @github-actions

    🔍 Triage Summary

    Check Result
    Issue type Bug
    Environment All required environment details provided for investigation
    Area Area\Engineering (SqlCommand core behavior)
    Duplicates None found
    Regression Likely regression from v6.1.5 → v7.0.0 introduced by PR #3655

    Analysis

    SqlCommand.Cancel() throws a NullReferenceException when Connection is null (i.e., the command was never associated with a connection). The root cause has been precisely identified by the reporter: PR #3655 changed a null-conditional access Connection?.ClientConnectionId to _activeConnection.ClientConnectionId in the logging statement at the top of the Cancel method, dropping the null guard. The same null-conditional was correctly preserved in the Clone method's analogous log statement. This is a straightforward P1 regression — Cancel() on an unassociated command was a documented no-op prior to v7.0.0 and the fix is a one-line change. The issue is confirmed to also affect v7.1.0-preview1.

    Next Steps

    • Assign to Copilot coding agent to fix SqlCommand.Cancel() in src/Microsoft.Data.SqlClient/src/ — restore the null-conditional operator on _activeConnection in the log statement (change _activeConnection.ClientConnectionId back to _activeConnection?.ClientConnectionId).
    • Add a unit test covering SqlCommand.Cancel() with a null Connection to prevent future regressions.
    • Verify the fix covers any other locations in Cancel() that may dereference _activeConnection without a null check.

    Note: This triage summary is auto-generated by an AI agent. The analysis and suggestions above have not been verified by a human maintainer. Please treat as preliminary guidance only.

    Generated by SqlClient Issue Auto-Triage for issue #4327 · ● 1.6M · ◷

  2. added
    Regression 💥
    on May 30, 2026
  3. moved this from To triage to Backlog in SqlClient Boardon May 30, 2026
  4. added
    Repro Available ✔️
    on Jun 9, 2026
  5. added this to the 7.0.2 milestone on Jun 9, 2026
  6. self-assigned this
    on Jun 9, 2026
  7. linked a pull request that will close this issueFix SqlCommand.Cancell null ref exception #4372on Jun 17, 2026
  8. moved this from Backlog to In progress in SqlClient Boardon Jun 17, 2026
  9. moved this from In progress to Done in SqlClient Boardon Jun 18, 2026
  10. moved this from Done to In progress in SqlClient Boardon Jun 18, 2026
  11. modified the milestones: 7.0.2, 7.1.0-preview2 on Jun 18, 2026
  12. linked a pull request that will close this issueFix SqlCommand.Cancell null ref exception #4372on Jun 18, 2026
  13. moved this from In progress to Done in SqlClient Boardon Jul 16, 2026
  14. moved this from Done to In validation in SqlClient Boardon Jul 16, 2026
  15. moved this from In validation to Done in SqlClient Boardon Jul 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Regression 💥Repro Available ✔️

Type

Projects

Relationships

None yet

Development

No branches or pull requests

Issue actions