Saturday, 5 October 2013

'Value was either too large or too small for an Int16' in SharePoint 2010 Task Edit Form

Received the below custom error, while trying to approve the task from Task Edit Form page.

In our application, suddenly we received this custom error. Before that, application working fine and approvers are able to open the form and able to Approve/Reject the task.

In the highlighted code statement, a 32-bit integer is attempted to be converted to 16-bit integer, whereas the variable is of type 32-bit int. This breaks the rendition of custom task edit form, logs a warning in ULS and redirect to error page.

Error logged in ULS (attached):
Message: Value was either too large or too small for an Int16.
                at System.Convert.ToInt16(Int32 value)    
                at System.Convert.ToInt16(Object value)    
                at Project.SP.Pages.Layouts.TaskEditForm.GetTaskListInformation()
                at Project.SP.Pages.Layouts.TaskEditForm.Page_Load(Object sender, EventArgs e)

Int16 variables can hold the values from 0 to 32767.
But The task that fails on custom edit form has associated workflow id 33248.

Workflow instances are assigned IDs incrementally (may not be necessarily be sequentially). It must have exceeded 32767 recently, hence the custom form with that code blow out loading the page.

Code snippet:

//Get the parent List item Id
   this.parentListItemId = Convert.ToInt16(this.taskListItem[SPBuiltInFieldId.WorkflowItemId]);

Code fix:
//Get the parent List item Id
  this.parentListItemId = Convert.ToInt32(this.taskListItem[SPBuiltInFieldId.WorkflowItemId]);

Int32 variables can hold max values is : 2,147,483,647