Resolving “Index was out of range” in SSMS

sqldba.blogMore of my SQL Server writing is at sqldba.blog, including current SSMS errors and fixes in the SSMS Complete Guide.

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)

Index Out of Range SQL Server Management Studio

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:

SQL
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:

SQL
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:

SQL Agent Job Steps
SQL Agent Job Step Order Change

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

Index Out of Range error SSMS

And here is the full error:

Index out of range 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!

Comments

2 responses to “Resolving “Index was out of range” in SSMS”

  1. Prashanth A Avatar
    Prashanth A

    Great ! Works ! I was getting same Error and your Suggestion is Cleared my issue. Thanks for your Post and helps to over come from issue

  2. Vijay Avatar
    Vijay

    This worked for me too! Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *