0
votes

I am using share point 2010, and using LINQ which is generated by SPMETAL.EXE. I am facing a weird issue which is mentioned below by sequence:

1: Inserted a new item (XX) in list (Listname) 2: On insertion a Workflow get starts and the item (XX) status becomes "In Progress" in ALL Items window. 3: If I access this row with LINQ, it threw an exception "Specified cast not valid" [NullReferenceException: Object reference not set to an instance of an object.] Microsoft.Office.Server.WebControls.MetaDataNavTree.OnUnload(EventArgs e) +40 System.Web.UI.Control.UnloadRecursive(Boolean dispose) +153 System.Web.UI.Control.UnloadRecursive(Boolean dispose) +306 System.Web.UI.Control.UnloadRecursive(Boolean dispose) +306 System.Web.UI.Control.UnloadRecursive(Boolean dispose) +306 System.Web.UI.Control.UnloadRecursive(Boolean dispose) +306 System.Web.UI.Control.UnloadRecursive(Boolean dispose) +306 System.Web.UI.Control.UnloadRecursive(Boolean dispose) +306 System.Web.UI.Control.UnloadRecursive(Boolean dispose) +306 System.Web.UI.Control.UnloadRecursive(Boolean dispose) +306 System.Web.UI.Control.UnloadRecursive(Boolean dispose) +306 System.Web.UI.Page.UnloadRecursive(Boolean dispose) +23 System.Web.UI.Page.ProcessRequestCleanup() +54 System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +11045194 System.Web.UI.Page.ProcessRequest() +91 System.Web.UI.Page.ProcessRequest(HttpContext context) +240 ASP.FORM_ASPX__331913002.ProcessRequest(HttpContext context) in c:\Windows\Microsoft.NET\Framework64\v2.0.50727\Temporary ASP.NET Files\root\ccd70a06\451bebbc\App_Web_form.aspx_-331913002.nurxfpml.0.cs:0 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +599 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +171

4: While If I access items on which their is no entry for work flow (i.e. when I didnt start my work flow from work flow settings) It works fine.

THE ABOVE MENTIONED ISSUE IS NOT WITH OBJECT MODEL APPROACH. THAT WORKS FINE. I THINK ISSUE IS WITH LINQ

PLEASE HELP TO GET RID OF THIS ISSUE. AS I CANT SWITCH FROM LINQ.

2

2 Answers

0
votes

I had the same problem, and this is what I did:

  1. Generate the code using SPMetal (with or without the Workflow attached: does not matter)

  2. Edit the generated code to change the type of the field that represents the workflow status column from the list, to object. In my case, the changed code looks like:

[System.Runtime.Serialization.DataMemberAttribute()] private object _designCompany_SetCompanyAndFullName;

    [Microsoft.SharePoint.Linq.ColumnAttribute(Name = "SetCompa", Storage = "_designCompany_SetCompanyAndFullName", ReadOnly = true, FieldType = "WorkflowStatus")]
    public System.Nullable<int> DesignCompany_SetCompanyAndFullName
    {
        get
        {
            return null; //removed all code - but this may be unnecessary
        }
        set
        {
            //removed all code - but this may be unnecessary
        }
    }
  1. Compile and deploy.

The error 'specified cast..' should not occur anymore.

The reason for this is that (I believe) SPMetal has a bug and generates System.Nullable type fields in the generated code to represent workflow status columns. I checked out the code for Microsoft.Sharepoint.Linq.dll in Reflector, to find that it uses a dynamically generated method to set the field value directly and just before setting the field it tries to cast the value from object to whatever the field type is.

MSDN specifies that the type mapping for the 'WorkflowStatus' type in Sharepoint is 'object' in .NET : http://msdn.microsoft.com/en-us/library/ee536245.aspx

Now, atleast I don't get this error anymore.

  • Krishna
0
votes

You could also change the type of your property to Object. That way you could read the real values from the SharePoint list. The values ARE Nullable integers, and you can find the codes on this blog: http://geekswithblogs.net/simonh/archive/2013/04/12/sharepoint-2010-workflow-status-values.aspx