This project has moved. For the latest updates, please go here.

Argument Out Of Range Exception!

Topics: usage (how to ...)
Nov 26, 2008 at 4:47 AM
Edited Nov 26, 2008 at 3:10 PM
While running this piece of code I get an "ArgumentOutOfRangeException was unhandled by user code" error.
The error occurs when executing the "Manage.Set(...)" command.  The error is not produced everytime
this command is executed.  It may work 4 times in a row, but then crashes.  The error also includes
"Index was out of range.  Must be non-negative and less than the size of the collection.  Parameter name: index."

    IPAddress ip = IPAddress.Parse("");
OctetString octetPassword = new OctetString("password");
IPEndPoint ipPoint = new IPEndPoint(ip, 161);
Variable test = new Variable(new ObjectIdentifier(new uint[] { 1, 3, 6, 1, 2, 1, 1, 1, 0 }),
new OctetString("TestSet"));
List<Variable> listTest = new List<Variable>() { test };
Manager.Set(VersionCode.V2, ipPoint, octetPassword, listTest, 5000);
catch (SharpOperationException ex)

What am I doing wrong?  Why am I getting this error?
I can increase the number of times this segment of code is successfully executed by 
setting a breakpiont on the same line as the "Manager.Set(...)" command.

Nov 28, 2008 at 11:23 PM
Well, as it does not always crash, I have the following action plan for you.

1. Go and setup Microsoft Network Monitor 3.2,

2. Learn how to use it here,

3. Start capture.

4. Launch your application and reproduce the exception.

5. Stop capture.

6. Send me the captured network traffic to analyze. mailto

Well, once I get the root cause, I will get back to you.


Dec 13, 2008 at 5:08 PM
Edited Dec 13, 2008 at 5:10 PM
I have taken your advise in part.  I collected packet data using Microsoft Network Monitor and I have
found out that an error is being generated on the equipment side.  It is sending back a PDU ErrorStatus 05
in the SNMP V1 packet.

I am going to attempt to update the equipement's firmware prior to proceeding troubleshooting this problem.

I have also noticed that it doesn't seem to matter whether I set the SNMP version to V1 or V2
when using the Manager.Set command, Microsoft Network Analyzer identifies it as SNMP V1.

Dec 14, 2008 at 4:53 AM
Well, it is a nice test case for #SNMP. Let's see what would happen once you fix the firmware. Meanwhile, I will see if there is anything wrong with #SNMP SET side like you describe in the end.

Thanks for your report,

Dec 14, 2008 at 5:03 AM
Well, now I can confirm on my side that if SET command is set to be sent in v2c, the correct packet is constructed by #SNMP. I tested a minute ago with Wireshark and your sample code. Please re-evaluate that on your side when you have time.


Jan 6, 2009 at 2:35 PM
I have updated the firmware to no avail.  When running the code above, I intermittently get the same error.

System.ArgumentOutOfRangeException:    {"Index was out of range. Must be non-negative and less than the size of the collection.\r\nParameter name: index"}
ActualValue:    null
Data:     {System.Collections.ListDictionaryInternal}
    [System.Collections.ListDictionaryInternal]:     {System.Collections.ListDictionaryInternal}
    Count:    1
    IsFixedSize:    false
    IsReadOnly:    false
    IsSynchronized:    false
Jan 6, 2009 at 7:13 PM
Edited Jan 6, 2009 at 9:28 PM
Sense my last post I have added the sharpsnmplib source code project to my solution.
After doing so, I have identified the line of code that generates the error:

static void Set(VersionCode version, IPEndPoint endpoint, OctetString community, IList<Variable> variables, int timeout)
if (version == VersionCode.V3)
throw new ArgumentException("you can only use SNMP v1 or v2 in this version");

 SetRequestMessage message = new SetRequestMessage(version, community, variables);
GetResponseMessage response = message.GetResponse(timeout, endpoint);

if (response.ErrorStatus != ErrorCode.NoError)
throw SharpErrorException.Create("error in response",    
                            response.Variables[response.ErrorIndex - 1].Id); <<<<< This line of code generates the error. 

The error occurs because "response.ErrorIndex" equals 0, making the array index equal to -1.
The response.Variables[ ] array is empty when this error occurs, therefore any index would
generate an error.



Jan 10, 2009 at 2:23 AM

Well, thanks for the new information. It is quite helpful so that I can know more about the agent firmware. So one question here,

What request you were sending? Is it an empty request with no variable bindings? I guess that would be impossible, isn't it? So why response.Variables.Count == 0? We may need to check Network Monitor log in this case to confirm is that real SNMP packet contains no variable bindings.

According to this book (, even if the agent meets an error when generating the response, the response should contain variables from the request. Because when I implemented SET like above I was referring to that book, the behavior of throwing such an exception was by design. At least, Microsoft Windows SNMP agent conforms to the book so there is no exception on my test box.

So here we come to a conclusion, if the agent response really contains no variable bindings, that behavior is a little bit "abnormal" to me. 

Meanwhile, I will spare some time later on the RFC once again. 


Marked as answer by lextm on 10/6/2013 at 9:28 PM