File tree Expand file tree Collapse file tree
src/Microsoft.Data.SqlClient
src/Microsoft/Data/SqlClient Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -1068,7 +1068,7 @@ public override void Cancel()
10681068 "SqlCommand.Cancel | API | Correlation | " +
10691069 $ "Object Id { ObjectID } , " +
10701070 $ "Activity Id { ActivityCorrelator . Current } , " +
1071- $ "Client Connection Id { _activeConnection . ClientConnectionId } , " +
1071+ $ "Client Connection Id { _activeConnection ? . ClientConnectionId } , " +
10721072 $ "Command Text '{ CommandText } '") ;
10731073
10741074 SqlStatistics statistics = null ;
Original file line number Diff line number Diff line change @@ -562,6 +562,20 @@ public void ParameterCollectionTest()
562562 }
563563 }
564564
565+ /// <summary>
566+ /// Verifies that SqlCommand.Cancel() is a no-op when Connection is null,
567+ /// rather than throwing a NullReferenceException. Regression test for #4327.
568+ /// </summary>
569+ [ Fact ]
570+ public void Cancel_WithNullConnection_DoesNotThrow ( )
571+ {
572+ using SqlCommand cmd = new SqlCommand ( ) ;
573+ Assert . Null ( cmd . Connection ) ;
574+
575+ // Should be a no-op, not throw NullReferenceException
576+ cmd . Cancel ( ) ;
577+ }
578+
565579 private static SqlConnection GetNonConnectingConnection ( ) =>
566580 new SqlConnection ( "Initial Catalog=a;Server=b;User ID=c;Password=d" ) ;
567581 }
You can’t perform that action at this time.
0 commit comments