If SQL Server Management Studio (SSMS) shows Index was out of range while you are working on something like a SQL Agent job, update SSMS to the latest release first. The (mscorlib) at the end of the message tells you it is a .NET exception inside SSMS, not an error returned by SQL Server, so the fix is on your side of the connection.
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index (mscorlib)

In my experience, I’ve seen this come up several times when trying to change the order of steps in a SQL Agent Job in SSMS.
Resolving “Index was out of range” Issue
To resolve this issue, update to the latest version of SSMS.
The current release is SSMS 22. It installs and updates through the Visual Studio Installer, and it installs side by side with older versions, so you can keep your old SSMS while you check whether the error has gone. You can see which version you have under Help > About.
My guide to installing and updating SSMS on sqldba.blog covers the download and updates with some useful SSMS tips from a DBA. Otherwise, the official page is Microsoft Learn: Install SSMS.
If It Still Happens: Change the Step Order Another Way
When the job step change is what triggers the error, you do not need the Move step buttons to get the result. If the aim is to make a step run first, change the job’s start step instead: in Job Properties on the Steps page, pick it in the Start step list, or run sp_update_job:
EXEC msdb.dbo.sp_update_job
@job_name = N'Your Job Name',
@start_step_id = 3;The start step only changes where the job begins. What runs next is still decided by each step’s On success and On failure actions, so if step 3 is set to quit reporting success, steps 1 and 2 never run. Check the flow before and after with this query, which decodes the action numbers:
SELECT j.name AS job_name,
j.start_step_id,
s.step_id,
s.step_name,
CASE s.on_success_action
WHEN 1 THEN 'Quit reporting success'
WHEN 2 THEN 'Quit reporting failure'
WHEN 3 THEN 'Go to next step'
WHEN 4 THEN 'Go to step ' + CAST(s.on_success_step_id AS varchar(10))
END AS on_success,
CASE s.on_fail_action
WHEN 1 THEN 'Quit reporting success'
WHEN 2 THEN 'Quit reporting failure'
WHEN 3 THEN 'Go to next step'
WHEN 4 THEN 'Go to step ' + CAST(s.on_fail_step_id AS varchar(10))
END AS on_failure
FROM msdb.dbo.sysjobs AS j
JOIN msdb.dbo.sysjobsteps AS s ON s.job_id = j.job_id
WHERE j.name = N'Your Job Name'
ORDER BY s.step_id;For a real reorder, script the job out (right-click it, Script Job as > DROP And CREATE To), put the sp_add_jobstep calls in the order you want with matching @step_id values, and run the script. Microsoft’s Handle Multiple Job Steps page explains how step flow works, and if you need to script many jobs at once, my Generate Agent Job Script script on sqldba.blog writes one block per job, steps and start step included.
Full Error Message & Follow-Up
This error might happen for you even if you are using the latest version of SSMS. I’m not sure what the root cause is, but the first troubleshooting step is to update SSMS as explained.
I tried to change the SQL Agent job step here from step 3 to 1:


I got this error message when I hit the button above:

And here is the full error:

Searching for this error online often led me to a lot of unrelated information about .NET development. If updating SSMS resolved it for you, or if you’ve done that and are still having the issue, please feel free to add a comment below!
Leave a Reply