locked
Accessing Thing Type elements RRS feed

  • Question

  • Hate to ask a C# question but I'm a newbie to C# and ASP.NET.


    My C# code for the submit button (code for TextBox follows) is:

        protected void c_buttonSave_Click(object sender, EventArgs e)
        {
            string phoneNumber = c_textboxPhoneNumber.Text;
            Contact contact = new Contact();
            contact.phone.number = phoneNumber;
            PersonInfo.SelectedRecord.NewItem(contact);
        }

    I get an error for "contact.phone.number". Visual Web Developer says the ItemType Contact does not contain a definition for "phone".

    But urn:com.microsoft.wc.thing.types:contact contains address, phone, and email. And, urn:com.microsoft.wc.thing.types:phone contains description, is-primary, and number.

    What am I missing?

    Or is there a good reference for syntax access to C# classes online somewhere?

    This is my web form TextBox:

    <tr>
        <td><asp:Label ID="label1" runat="server" Text="Phone Number:" Visible="True"/></td>
        <td><asp:TextBox ID="c_textboxPhoneNumber" runat="server"></asp:TextBox></td>
    </tr>

    Appreciate it,
    Gary
    Friday, July 24, 2009 5:14 PM

Answers

  • First of all some basics:

    Contact data type is a singleton on a record. So there can be atmost one thing of type Contact in record. So if you already have one you need to update that rather than trying to create a new one. 

    Here is how you add a phone number to the contact (assuming HV record has an instance)

    Contact  c =  <obtain the existing instance>;
    Phone ph = new Phone();
    //set propertes on phone using c_textboxPhoneNumber.Text
    c.ContactInformation.Phone.Add(ph);
    PersonInfo.SelectedRecord.UpdateItem(c);


    //To obtain the existing instance of a type you can use the GetSingleValue function defined in Hello World sdk sample like this:
    Contact c = GetSingleValue<Contact>(Contact.TypeId);

    if there is no instance, you can always create a new Contact Instance as

    Contact c = new Contact();

    and finally call PersonInfo.SelectedRecord.NewItem(c)

    HTH

    Raj


    Raj HealthVault Developer Tool http://xray.getrealconsulting.com
    Friday, July 24, 2009 5:23 PM
  • Here is the code:

    T GetSingleValue<T>(Guid typeID) where T:class
        {
            HealthRecordSearcher searcher = PersonInfo.SelectedRecord.CreateSearcher();
    
            HealthRecordFilter filter = new HealthRecordFilter(typeID);
            searcher.Filters.Add(filter);
    
            HealthRecordItemCollection items = searcher.GetMatchingItems()[0];
    
            if (items != null && items.Count > 0)
            {
                return items[0] as T;
            }
            else
            {
                return null;
            }
        }
    Friday, July 24, 2009 6:45 PM
  • Hi Karthick

    Please use this code. Hope this helps

     

    Contact c = GetSingleValue<Contact>(Contact.TypeId);
    Phone ph = new Phone("1222");
    c.ContactInformation.Phone.Add(ph);
    PersonInfo.SelectedRecord.UpdateItem(c);

    Thanks
    Mahesh

    Friday, July 24, 2009 6:46 PM
  • To update an item, you have to fetch it, modify the values and then call UpdateItem() on the instance. Please refer to the following thread:
    http://social.msdn.microsoft.com/Forums/en-US/healthvault/thread/e89a1545-d6da-4ec0-8da8-e14481478ffd/

    Friday, July 24, 2009 6:52 PM
  • Hi

    We currently do not support client applications, though we are hoping to in the future. This post contains more information about this topic.

    Thanks
    Mahesh
    Friday, July 24, 2009 11:42 PM
  • If by "client application" you mean an application that runs on a desktop machine instead of a web application, you won't find any examples of doing that, because we don't support it.

    We currently don't have a way to authenticate that a client application is who it says it is, and doing so is part of our model.

    I don't believe we have any official published samples of communicating with the raw XML web service outside of the web service documentation you refer to above.



    Mahesh

    Monday, July 27, 2009 5:21 PM

All replies

  • First of all some basics:

    Contact data type is a singleton on a record. So there can be atmost one thing of type Contact in record. So if you already have one you need to update that rather than trying to create a new one. 

    Here is how you add a phone number to the contact (assuming HV record has an instance)

    Contact  c =  <obtain the existing instance>;
    Phone ph = new Phone();
    //set propertes on phone using c_textboxPhoneNumber.Text
    c.ContactInformation.Phone.Add(ph);
    PersonInfo.SelectedRecord.UpdateItem(c);


    //To obtain the existing instance of a type you can use the GetSingleValue function defined in Hello World sdk sample like this:
    Contact c = GetSingleValue<Contact>(Contact.TypeId);

    if there is no instance, you can always create a new Contact Instance as

    Contact c = new Contact();

    and finally call PersonInfo.SelectedRecord.NewItem(c)

    HTH

    Raj


    Raj HealthVault Developer Tool http://xray.getrealconsulting.com
    Friday, July 24, 2009 5:23 PM
  • Hi

    Are you trying to insert the phone information.

    You can use ContactInfo class which is present in Microsoft.Health.ItemTypes.

    Please Note that the ContactInformation field of Person type is also the same type


    Thanks
    Mahesh
    Friday, July 24, 2009 5:24 PM
  • Hi Raj,

     Thanks for your useful reply. I followed your code and when i did this

                Contact contact = new Contact();
                Phone ph = new Phone();
                ph.Number = "4562317892";
                contact.ContactInformation.Phone.Add(ph);
                PersonInfo.SelectedRecord.NewItem(contact);



    I get the below error:
     
    "HealthServiceException was unhandled by user code.

    The item specified is of a singleton type for which an instance already exists. To update the existing instance, first get the instance, modify the data, and then call HealthVault to update."

    Please, can you help me with some sample code here to correct this bug?

    Thanks in advance,
    Karthick

    Friday, July 24, 2009 6:00 PM
  • Hi Karthick,

    Please refer to the following link to update an item and see if it helps:
    http://social.msdn.microsoft.com/Forums/en-US/healthvault/thread/7787f44f-4b9a-4363-a180-9917f1c2388d/
    Friday, July 24, 2009 6:08 PM
  • Hi Mansi, Many thanks for your reply. But if i use this code Contact contact = GetSingleValue(Contact.TypeId); contact.ContactInformation.Phone.Add(new Phone("01233456", "Mobile", false)); PersonInfo.SelectedRecord.UpdateItem(contact); I don't know what code to include for GetSingleValue and hence i replaced(if you have the code for GetSingleValue, please do share) Contact contact = GetSingleValue(Contact.TypeId); with Contact contact = new Contact(); and i ran and i am struck with this error: "itemsToUpdate Parameter name: An item cannot be updated without specifying it's unique identifier." Please help me on this. Thanks again, Karthick
    Friday, July 24, 2009 6:40 PM
  • Here is the code:

    T GetSingleValue<T>(Guid typeID) where T:class
        {
            HealthRecordSearcher searcher = PersonInfo.SelectedRecord.CreateSearcher();
    
            HealthRecordFilter filter = new HealthRecordFilter(typeID);
            searcher.Filters.Add(filter);
    
            HealthRecordItemCollection items = searcher.GetMatchingItems()[0];
    
            if (items != null && items.Count > 0)
            {
                return items[0] as T;
            }
            else
            {
                return null;
            }
        }
    Friday, July 24, 2009 6:45 PM
  • Hi Karthick

    Please use this code. Hope this helps

     

    Contact c = GetSingleValue<Contact>(Contact.TypeId);
    Phone ph = new Phone("1222");
    c.ContactInformation.Phone.Add(ph);
    PersonInfo.SelectedRecord.UpdateItem(c);

    Thanks
    Mahesh

    Friday, July 24, 2009 6:46 PM
  • To update an item, you have to fetch it, modify the values and then call UpdateItem() on the instance. Please refer to the following thread:
    http://social.msdn.microsoft.com/Forums/en-US/healthvault/thread/e89a1545-d6da-4ec0-8da8-e14481478ffd/

    Friday, July 24, 2009 6:52 PM
  • Thanks a million for your help Mansi and Mahesh.

    Thanks,
    Karthick
    Friday, July 24, 2009 6:55 PM
  • Thanks a lot. I appreciate the help.

    Gary
    Friday, July 24, 2009 7:04 PM
  • Hi Mansi/Mahesh/Raj/Everyone,

     I am able to transfer the phone number from web page to healthvault a/c.

     But  i am supposed to transfer the phone number from a winform to healthvault a/c.

     Please assume that we have a textbox and submit button, as soon as when you type the number and click on submit, it should get saved on healthvault a/c DB.

    I referenced the healthvault.dlls, added namespaces and added the same code but i get a null reference error.

    "Object reference not set to an instance of an object."

    "NullReferenceException was handled"

      T GetSingleValue<T>(Guid typeID) where T : class
            {
                HealthRecordSearcher searcher = HealthServicePage.CurrentPage.PersonInfo.SelectedRecord.CreateSearcher();

                HealthRecordFilter filter = new HealthRecordFilter(typeID);
                searcher.Filters.Add(filter);

                HealthRecordItemCollection items = searcher.GetMatchingItems()[0];

                if (items != null && items.Count > 0)
                {
                    return items[0] as T;
                }
                else
                {
                    return null;
                }
            }

    Please help with your suggestions.

    Thanks,
    Karthick

    Friday, July 24, 2009 9:52 PM
  • This error means the object was not instantiated. Before it is accessed it has to be instantiated. Please refer to a similar thread and see if that works for you:
    http://social.msdn.microsoft.com/forums/en-US/healthvault/thread/55d9589b-2b7e-40b9-b127-6b69d72371e6/
    Friday, July 24, 2009 11:06 PM
  • Thanks Mansi, but what i am trying to replicate is the same code what i have in asp.net page , am trying in c# winform(assume that i have internet connection in my system).

    1. I added references for healthvault.dlls
    2. Imported the namespaces

    using Microsoft.Health;
    using Microsoft.Health.Web;
    using Microsoft.Health.ItemTypes;
    using System.Web.UI;

    3. Added an app.config file and added my application id.

     <appSettings>
        <add key="ApplicationId" value="99fac29a-8a03-492c-ad42-1edae676d1a5" />
        <add key="ShellUrl" value="https://account.healthvault-ppe.com/" />
        <add key="HealthServiceUrl" value="https://platform.healthvault-ppe.com/platform/" />
      </appSettings>

    4. In .cs page

       T GetSingleValue<T>(Guid typeID) where T : class
            {
                try
                {
                    //This line(marked with ** gives me Null Reference and object not to set instance exception)

                    ** HealthRecordSearcher searcher = PersonInfo.SelectedRecord.CreateSearcher();

                    HealthRecordFilter filter = new HealthRecordFilter(typeID);
                    searcher.Filters.Add(filter);

                    HealthRecordItemCollection items = searcher.GetMatchingItems()[0];

                    if (items != null && items.Count > 0)
                    {
                        return items[0] as T;
                    }
                    else
                    {
                        return null;
                    }
                }
                catch (Exception e1)
                {
                    MessageBox.Show(e1.ToString());
                }
                return null;
            }

      private void button1_Click(object sender, EventArgs e)
            {
                Contact contact = GetSingleValue<Contact>(Contact.TypeId);

                contact.ContactInformation.Phone.Add(new Phone("01233456", "Mobile", false));
     
                HealthServicePage.CurrentPage.PersonInfo.SelectedRecord.UpdateItem(contact);
            }

    Please suggest some points to overcome that error.

    Thanks,
    Karthick

    Friday, July 24, 2009 11:13 PM
  • I tried doing the same and did not get an exception. Did you go through the Hello World sample in the SDK? See if that helps. I will update you once I find out the resolution.
    Friday, July 24, 2009 11:42 PM
  • Hi

    We currently do not support client applications, though we are hoping to in the future. This post contains more information about this topic.

    Thanks
    Mahesh
    Friday, July 24, 2009 11:42 PM
  • Sorry Karthick, I missed out winform in your query. As Mahesh said we currently do not support client applications.

    Thanks,
    Mansi
    Friday, July 24, 2009 11:47 PM
  • Thanks both Mansi and Mahesh.

    Is there a tricky way to handle this?

    Do healthvault have webservices in-built to transfer data to HV a/c?

    Like If i pass parameters to the webservice link then it should get saved.

    Hope you got my idea here.

    Please reply.


    Thanks in advance,
    Karthick
    Saturday, July 25, 2009 12:04 AM
  • If by "client application" you mean an application that runs on a desktop machine instead of a web application, you won't find any examples of doing that, because we don't support it.

    We currently don't have a way to authenticate that a client application is who it says it is, and doing so is part of our model.

    I don't believe we have any official published samples of communicating with the raw XML web service outside of the web service documentation you refer to above.



    Mahesh

    Monday, July 27, 2009 5:21 PM