Historical article
- Original title: Microsoft.SqlServer.Dts.Runtime.DtsRuntimeException: The element cannot be found in a collection
- Author: Brian Knight
- Original publication date: December 30, 2005
- Originally published on: White Knight Technology
- Preservation note: The technical article below is preserved as a historical SSIS resource. Its legacy diagnosis has not been removed or silently modernized.
Archive provenance: White Knight Technology Historical Archive
The SSIS/DTS RuntimeException “The element cannot be found in a collection” means code requested an item that is not present in the collection available at runtime. In a Script Task, the missing item is often a variable referenced through Dts.Variables.
For example, this line requires a variable with the qualified name User::FileName to be available to the Script Task:
string fileName = Dts.Variables["User::FileName"].Value.ToString();
Check the variable name and scope
First, compare the name in code with the package variable. Include the namespace, normally User::, and check spelling and capitalization. Renaming a variable in the package does not safely update every string literal inside an existing script.
Next, check scope. A variable is visible to the container or task where it is defined and to that object's descendants. A variable scoped to one Sequence Container or Foreach Loop is not available to a Script Task in a different branch. Move the variable to a common parent scope, or move the task under the scope that owns the variable.
If variables share the same short name in nested scopes, use the fully qualified name and verify which definition the task is intended to read.
Configure Script Task access
The Script Task does not expose every visible package variable to the script automatically. In the Script Task Editor, add variables that the script only reads to ReadOnlyVariables. Add variables whose values the script changes to ReadWriteVariables. Use their qualified names, such as:
User::FileName,User::ArchiveFolder
A variable placed only in ReadOnlyVariables must not be assigned by the script. A variable the script writes belongs in ReadWriteVariables. After changing either list, reopen and save the script if necessary so the Script Task project reflects the current configuration.
Debug the missing element
Use a short, repeatable check:
- Identify the exact
Dts.Variables["..."]access that throws. - Confirm that the variable exists with the same qualified name.
- Confirm that its scope contains the Script Task.
- Confirm that it appears in
ReadOnlyVariablesorReadWriteVariablesas required. - Set a breakpoint immediately before the access and inspect the available variables and their values.
You can also temporarily emit the value with Dts.Events.FireInformation after a successful lookup to verify which code path and iteration are running. If every variable check passes, inspect other named collection lookups near the failure, because the same exception can be raised when code requests a missing connection, parameter, column, or other collection member.
Avoid hiding the error with a broad catch block. The useful fix is to correct the missing name, scope, or access list so the Script Task receives the element it expects.
2026 Update
The legacy exception diagnosis remains useful, but current SSIS versions and deployment models should be tested before production use. Confirm variable access, script behavior, and package deployment in the supported environment rather than assuming SQL Server 2005-era behavior is unchanged.