Skip to content

System.Data.OleDb: throw ArgumentOutOfRangeException from OleDbParameterCollection.RemoveAt(int) - #137041

Open
alaricd wants to merge 2 commits into
dotnet:mainfrom
pengdows:oledb-parametercollection-removeat-argumentoutofrange
Open

alaricd wants to merge 2 commits into
dotnet:mainfrom
pengdows:oledb-parametercollection-removeat-argumentoutofrange

Conversation

@alaricd

@alaricd alaricd commented Nov 5, 2026 •

Copy link
Copy Markdown

Fixes #137039

Problem

OleDbParameterCollection is an IList (through DbParameterCollection). The documentation of IList.RemoveAt lists ArgumentOutOfRangeException for an index that is not valid ("index is not a valid index in the IList"). OleDbParameterCollection.RemoveAt(int) instead threw IndexOutOfRangeException (through RangeCheck), which is not a subclass of it, so a catch (ArgumentOutOfRangeException) written against the documented contract did not catch it. Insert(-1, ...) on the same collection already throws ArgumentOutOfRangeException, so the collection was inconsistent with itself.

Change

RemoveAt(int) now throws ArgumentOutOfRangeException for an index outside 0..Count-1, with ParamName == "index", ActualValue set to the index, and the same message as before ("Invalid index {0} for this OleDbParameterCollection with Count={2}."). Nothing else changes: the indexer, RemoveAt(string) and the other members keep using RangeCheck/IndexOutOfRangeException, and tests pin that.

The scope is deliberately one method. The indexer getter has the same shape, but its documentation is a separate question and I would rather not widen a compatibility-sensitive change.

Compatibility

This is a behavior change: code that catches IndexOutOfRangeException around OleDbParameterCollection.RemoveAt(int) will no longer catch it. I believe the old behavior is inherited from .NET Framework, and SqlParameterCollection has the same pattern, so I raised it as an issue first (#137039) and am sending this as a draft until maintainers say whether they want it. If the answer is "by design", I will close this and propose a documentation note instead. The new tests are skipped on .NET Framework, which keeps the old exception type.

Tests

OleDbParameterCollectionTests is new and needs no OLE DB provider (a command and its parameters can be created without a connection), so it runs wherever the OleDb tests are built:

  • valid indexes (first, middle, last, only item) remove just that parameter and detach it;
  • invalid indexes (empty collection with 0/-1/int.MinValue/int.MaxValue; two items with 2/3/-1/int.MaxValue) throw ArgumentOutOfRangeException with the right ParamName and ActualValue and leave the collection unchanged;
  • the message names the collection and its count;
  • the same behavior through IList and DbParameterCollection;
  • unchanged behavior is pinned: RemoveAt(string) for a missing name and the indexer still throw IndexOutOfRangeException.

The first commit adds the tests and fails against the old code; the second makes them pass.

I have not run these tests locally, and CI has not run them yet. Building dotnet/runtime's library tests needs a CoreCLR build, which my environment cannot do (no C++ toolchain). The PR is now ready for review, but the full build and test pipelines still need a maintainer to start them on this fork-based PR. I checked the code by reading it against the surrounding sources only; please run the pipelines, and until a green run exists, please treat the tests as unverified and tell me if anything fails to compile.

How this was found

Found by pengdows.AdoNetApiTest.conformance, an xUnit conformance suite for ADO.NET providers that checks the documented System.Data.Common contract (forked from mysql-net/AdoNetApiTest). Its tests ParameterCollection_RemoveAt_zero_throws_when_empty and ParameterCollection_RemoveAt_negative_one_throws failed for System.Data.OleDb. The reproduction in #137039 needs no database.

…index

IList.RemoveAt documents ArgumentOutOfRangeException for an index that is not valid.
OleDbParameterCollection.RemoveAt(int) throws IndexOutOfRangeException, so the
invalid-index tests fail until the next commit. Valid-index behavior, the Int32
overload through IList and DbParameterCollection, and the behavior that must not
change (RemoveAt(string), the indexer) are pinned as well. The tests need no OLE DB
provider, and the new-behavior ones are skipped on .NET Framework, which keeps the
old exception type.
…eAt(int)

IList.RemoveAt documents ArgumentOutOfRangeException when the index is not valid.
OleDbParameterCollection.RemoveAt(int) threw IndexOutOfRangeException through
RangeCheck. It now throws ArgumentOutOfRangeException (ParamName "index", ActualValue
the index) with the existing "Invalid index {0} for this OleDbParameterCollection with
Count={2}." message. The indexer and the name-based members are unchanged.

Fixes dotnet#137039
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Nov 5, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
12 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-system-collections
See info in area-owners.md if you want to be subscribed.

This branch has not been deployed

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

Labels

area-System.Collections community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

System.Data.OleDb: OleDbParameterCollection.RemoveAt throws IndexOutOfRangeException, IList.RemoveAt documents ArgumentOutOfRangeException

1 participant