locked
InvalidCastException was handled by user Code RRS feed

  • Question

  • I was trying to retrieve Medications and and Conditions from HealthVault with ApplicationID that has access to this information. I could able to get Conditions data but when i tried to get Medications data using the same Collection object, i am getting an  error on the subject. Here is additional error.
    Unable to cast object of type 'Microsoft.Health.HealthRecordItem' to type 'Microsoft.Health.ItemTypes.Medication. I eally need help.
    Thanks in advance.
    Thursday, November 19, 2009 12:53 PM

Answers

All replies

  • This error is usually because you have requested both versions of medication data type for the application id in the configuration site.

    The following thread explains this error, the reasons and the fix in detail

    http://social.msdn.microsoft.com/Forums/en-US/healthvault/thread/f4f30e88-d328-46bf-b2d3-d9a104a393b1

    Raj
    Raj HealthVault Developer Tool http://xray.getrealconsulting.com
    Thursday, November 19, 2009 3:11 PM
  • Thanks Rajesh, I really appreciate your help. I did everything as it is said on thread but i still having some problem. I can able to access Notes part of Medications but not core data. I have given an applicationId read write update and delte access to Condtions and Medications. Can you tell me why this is happening?
    Best Regards,
    Addis
    Thursday, November 19, 2009 8:28 PM
  • Hello,
    Can you please specify your application ID?

    Also if possible please paste the code snippet which is throwing this error.

    Thanks and Regards,
    Aneesh D.
    Thursday, November 19, 2009 9:53 PM
  • Here is:

    ApplicationId 

     

    1427bc2a-e17f-46a7-a8ce-8bbb86d9e33a
    The code snippet is:

    Collection

     

    <T> GetHealthRecordItemCollection<T>(Guid typeID,

     

    HealthRecordAccessor offlineAccessor) where T : HealthRecordItem

    {

     

    HealthRecordSearcher offlineSearcher = new HealthRecordSearcher(offlineAccessor);

    offlineSearcher.Filters.Add(

    new HealthRecordFilter(typeID));

     

    HealthRecordItemCollection item1 = offlineAccessor.GetItemsByType(Medication.TypeId, HealthRecordItemSections.EffectivePermissions);

     

     

    HealthRecordItemCollection offlineItems = offlineSearcher.GetMatchingItems()[0];

     

    Collection<T> recordCollection = new Collection<T>();

     

    foreach (HealthRecordItem item in offlineItems)

    {

     

    recordCollection.Add((T)item);
    Exception thrown here

    QuestionInvalidCastException was handled by user Code

    Unable to cast object of type 'Microsoft.Health.HealthRecordItem' to type 'Microsoft.Health.ItemTypes.Medication'.

    }

     

    return recordCollection;

    }

    I really appreciate your help,
    Best Regards,
    Addisu

    Friday, November 20, 2009 12:52 AM
  • In the configuration center both Medication and Medication V1 are selected for this app id in the offline rule. Can you please uncheck the Medication v1 type and keep only Medication and then try again? Here please note that after you make change in the configuration center you will have to wait for half an hour to let the changes get reflected.

    Hope this helps.

    Thanks and Regards,
    Aneesh D.
    Friday, November 20, 2009 9:04 PM
  • Thanks Aneesh for your kindly response.
    I have Unchecked Medication V1 and tried after a day but the same exception is thrown again. Can you please send me a  code snippet that will help me to retireve Medications from HealthVault. I am very new to HealthVault. I will appreciate if you can tell me which namespaces or methods i have to use to update Conditions and Medications.
    Best Regards,
    Addisu
    Sunday, November 22, 2009 2:59 PM
  • How are you calling GetHealthRecordItemCollection function?

    You are going to have problems if you are passing an incompatible typeid ...

    For ex:

    GetHealthRecordItemCollection<Medication>(Microsoft.HealthVault.ItemTypes.Condition.TypeId, HealthRecordItemSections.Default>)


    Raj HealthVault Developer Tool http://xray.getrealconsulting.com
    Monday, November 23, 2009 7:38 AM
  • Rajesh,
    It was not Incopatible typeId. Here is the calling method:

    Collection

     

    <Medication> medicationCollection = GetHealthRecordItemCollection<Medication>(Medication.TypeId, offlineAccessor);
    Here is method:

    Collection

     

    <T> GetHealthRecordItemCollection<T>(Guid typeID,

     

    HealthRecordAccessor offlineAccessor) where T : HealthRecordItem

    {

     

    HealthRecordSearcher offlineSearcher = new HealthRecordSearcher(offlineAccessor);

    offlineSearcher.Filters.Add(

    new HealthRecordFilter(typeID));

     

    HealthRecordItemCollection offlineItems = offlineSearcher.GetMatchingItems()[0];

     

    Collection<T> recordCollection = new Collection<T>();

     

    foreach (HealthRecordItem item in offlineItems)

    {

    recordCollection.Add((T)item); //InvalidCastException was handled by user Code

    }

     

    return recordCollection;

    }
    Can you  see any incompatiblity in this code?
    Thanks a lot,
    Addisu

    Monday, November 23, 2009 7:37 PM
  • Mahesh,
    It is more than an hour since you unchecked Medications V1 that we might thought caused InvalidCastException.
    I checked and the same error happend. I will give both methods so that you guys can take a look at it and help me what has gone wrong. I pased both calling methods one for Conditions and one for Medications. Conditions work fine. No error and i could able to retrieve all  Conditions but Medications has InvalidCastException error. Please i need your help.

    HealthRecordAccessor

     

    offlineAccessor = new HealthRecordAccessor(HealthVaultConnectionManager.CreateConnection(_applicationId, personId), recordId);

     

    Collection<Medication> medicationCollection = GetHealthRecordItemCollection<Medication>(Medication.TypeId, offlineAccessor);

     

    Collection<Condition> conditionCollection = GetHealthRecordItemCollection<Condition>(Condition.TypeId, offlineAccessor);

     

    GetHealthRecordItemCollection:

     

    Collection<T> GetHealthRecordItemList<T>(Guid typeID,

     

    HealthRecordAccessor offlineAccessor) where T : HealthRecordItem

    {

     

    HealthRecordSearcher offlineSearcher = new HealthRecordSearcher(offlineAccessor);

    offlineSearcher.Filters.Add(

    new HealthRecordFilter(typeID));

     

    HealthRecordItemCollection offlineItems = offlineSearcher.GetMatchingItems()[0];

     

    Collection<T> recordCollection = new Collection<T>();

     

    foreach (HealthRecordItem item in offlineItems)

    {

    recordCollection.Add((T)item);

    }

     

    return recordCollection;

    }

    Thanks,
    Addisu

    Tuesday, November 24, 2009 7:12 PM

  • This issue "InvalidCastException was handled by user Code" is resloved in the following thread http://social.msdn.microsoft.com/Forums/en-US/healthvault/thread/3b31fab0-4fa0-4d79-8f92-cf3a05e1f694


    -Mahesh
    Monday, December 7, 2009 8:32 PM