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


  • 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.

        public class DC_Emps
            public int EmpId { get; set; }
            public string EmpCode { get; set; }
            public string BarCode { get; set; }
            public string FirstName { get; set; }
            public string MiddleName { get; set; }
            public string LastName { get; set; }
            public DateTime? CreatedOn { get; set; }    // throwing "The given key was not present in the dictionary"
            public string CreatedBy { get; set; }
            public DateTime? ModifiedOn { get; set; }   //working fine in every case.
            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?


    Govind Bhavan

    Wednesday, October 17, 2007 6:19 AM


All replies