Freigeben über


{"value does not fall within the expected range."} error

When I use the object model to add item to MOSS 2007 project task list, I have some code like following:

const string SitePath = "https://litwareserver:5000";
const string ListName = "ProjectTask";

// enter object model through site collection.
SPSite siteCollection = new SPSite(SitePath);

// obtain reference to top-level site.
SPWeb site = siteCollection.RootWeb;
site.AllowUnsafeUpdates = true;     //needed since my code will be running as web service

SPList myList = site.Lists[ListName];
if (myList != null)
{
    SPListItem item = myList.Items.Add();

    item["Title"] = "test item";
    item["PercentageComplete"] = 0.2; // 20%  

    item.Update();
}

// clean up by calling Dispose.
site.Dispose();
siteCollection.RootWeb.Dispose();
siteCollection.Dispose();

I got the "value does not fall within the expected range." error

By looking at this error message, my first reaction was to check the percentage complete column, it has min (0) and max (1) defined, 0.2 shall be within the range, anyway, I tried 20, 0, 1 and lots of other values, same error;

Then I wanted to reproduce it with a custom list so I created a new list with a field with min/max values defined, I could add items with values inside the range and also outside the range.

I could not reproduce it with custom list. What could be the issue? What's the difference between my custom list and the project task list? Is it possible the field (% complete) in project task list was defined in the content type?

Then I created a content type that has a field with min/max values defined and attached this content type to a new list, removed the original item content type from the list, tested it again, I could add item to the new list with values inside or outside the defined range. So it's not because it's defined in content type ... this made me wonder what could be the problem???

I decided to print out the internal name and other properties of each fields and found out the internal name of field (% complete) is "percentComplete" instead of "percentageComplete" - I fixed the problem right away.

What I learned from this debugging process?

1. Do not assume anything.

2. Do not be fooled by error message.

3. Product error message should contain meaningful and specific information to help identifying the problem.

The error message should be the classic "index is out of range" or even better "Cannot find the field name '{0}'"