Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1068,7 +1068,7 @@ public override void Cancel()
"SqlCommand.Cancel | API | Correlation | " +
$"Object Id {ObjectID}, " +
$"Activity Id {ActivityCorrelator.Current}, " +
$"Client Connection Id {_activeConnection.ClientConnectionId}, " +
$"Client Connection Id {_activeConnection?.ClientConnectionId}, " +
$"Command Text '{CommandText}'");

SqlStatistics statistics = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,20 @@ public void ParameterCollectionTest()
}
}

/// <summary>
/// Verifies that SqlCommand.Cancel() is a no-op when Connection is null,
/// rather than throwing a NullReferenceException. Regression test for #4327.
/// </summary>
[Fact]
public void Cancel_WithNullConnection_DoesNotThrow()
{
using SqlCommand cmd = new SqlCommand();
Assert.Null(cmd.Connection);

// Should be a no-op, not throw NullReferenceException
cmd.Cancel();
}

private static SqlConnection GetNonConnectingConnection() =>
new SqlConnection("Initial Catalog=a;Server=b;User ID=c;Password=d");
}
Expand Down
Loading