0

Am trying to manually pass list item ID as argument in user defined function as Int32 var type. When the list index assigned to splistitem it raise the specified argument error. While debugging it pass a valid integer argument (96, 97, etc). Also the value exist in TargetList.ID. Any idea? TIA.

  private void update_contradate(string _trflag, Int32 _listitemID){          

        using (SPSite oSiteCollection = new SPSite(SPContext.Current.Site.Url))
        {
            using (SPWeb oWebsiteRoot = oSiteCollection.OpenWeb("/")) // for root list use "/"
            {
                SPList oList = oWebsiteRoot.Lists["TargetList"];

                SPListItem oListItem = oList.Items[_listitemID]; //error 
               ...
            }
        }
    }
1

You are confusing your list item ID values which are 1-based integers with the SPListItemCollection index which are 0-based integers.

If you examine the output of the MSDN sample code here:

SPListItem.ID property

you will see SPListItemCollection index doesn't match the list item ID.

In addition, if you look here:
SPListItemCollection.Item property (Int32)

you will find the error you mention and the following remark:

An item’s index in the collection cannot be obtained by accessing the item’s ID property.

0

Well it was a wrong method am using instead of :

 SPListItem oListItem = oList.GetItemById[_listitemID]; //

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.