none
DateTime? - "The given key was not present in the dictionary"

    Question

  • Hi, I am getting "The given key was not present in the dictionary" when returning a Nullable DateTime (DateTime?) in a composite type to client. Exception detail:

     

    System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.
       at System.ThrowHelper.ThrowKeyNotFoundException()
       at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
       at Microsoft.Tools.TestClient.DataContractAnalyzer.BuildVariable(IDictionary`2 variables, StringObjectPair link)
       at Microsoft.Tools.TestClient.DataContractAnalyzer.BuildVariables(Object result, IDictionary`2 outValues)
       at Microsoft.Tools.TestClient.ServiceExecutor.Execute(ServiceInvocationInputs inputValues)
       at Microsoft.Tools.TestClient.ServiceExecutor.Execute(ServiceInvocationInputs inputValues)
       at Microsoft.Tools.TestClient.ServiceExecutor.ExecuteInClientDomain(ServiceInvocationInputs inputs)
       at Microsoft.Tools.TestClient.UI.MainForm.invokeServiceWorker_DoWork(Object sender, DoWorkEventArgs e)
       at System.ComponentModel.BackgroundWorker.OnDoWork(DoWorkEventArgs e)
       at System.ComponentModel.BackgroundWorker.WorkerThreadStart(Object argument)

    But If I change DateTime? to DateTime then its working fine. I am using basicHttpBinding.

    Interestingly my composite type has 2 nullable DateTime? fields I am getting this error on first field "CreatedOn" but the second field "ModifiedOn" is working fine (even incase of null value).

     

    Structure of composite type which I am returning from WCF.

        [DataContract]
        public class DC_Emps
        {
            [DataMember]
            public int EmpId { get; set; }
            [DataMember]
            public string EmpCode { get; set; }
            [DataMember]
            public string BarCode { get; set; }
            [DataMember]
            public string FirstName { get; set; }
            [DataMember]
            public string MiddleName { get; set; }
            [DataMember]
            public string LastName { get; set; }
            [DataMember]
            public DateTime? CreatedOn { get; set; }    // throwing "The given key was not present in the dictionary"
            [DataMember]
            public string CreatedBy { get; set; }
            [DataMember]
            public DateTime? ModifiedOn { get; set; }   //working fine in every case.
            [DataMember]
            public string ModifiedBy { get; set; }
        }

    This error is coming only if returning it throught WCF, its working fine if calling the method locally (without WCF) in a test project. I am using LINQ to SQL to access the database, here is the function which is returning the composite type:

     

    public DALDataContract.DC_Emps Transform(t_Emp value)

    {

    return new DALDataContract.DC_Emps()

    {

    EmpId = value.EmpId,

    EmpCode = value.EmpCode,

    BarCode = value.BarCode,

    CreatedBy = value.CreatedBy,

    //CreatedOn = (value.CreatedOn != null ? (DateTime)value.CreatedOn : DateTime.MinValue), ALTERNATE SOLUTION WITHOUT DateTime?

    CreatedOn = value.CreatedOn, //throwing error "given key was not present in the dictionary", on sql2k 2000 datetime having default value (getdate())

    FirstName = value.FirstName,

    LastName = value.LastName,

    MiddleName = value.MiddleName,

    ModifiedBy = value.ModifiedBy,

    ModifiedOn = value.ModifiedOn

    };

    }

     

    Please advise what to do?

    Regards,

    Govind Bhavan

    Wednesday, October 17, 2007 6:19 AM

Answers

All replies