locked
Data type extension problems + multiple extension deleting RRS feed

  • Question

  • I seem to be unable to retrieve data saved into a data type extension. Or - I'm not even sure if it is actually going in there!

    Here is my extension class -

        public class UserExtensionTest : HealthRecordItemExtension
        {
            static readonly string ExtensionSource = "MyApp.ExtensionTest";
    
            string m_extensiontest;
    
            public UserExtensionTest()
            {
            }
    
            public UserExtensionTest(string extensionTestText) : base(ExtensionSource)
            {
                m_extensiontest = extensionTestText;
                Source = ExtensionSource;
            }
    
            public string ExtensionText
            {
                get { return m_extensiontest; }
                set { m_extensiontest = value; }
            }
    
            protected override void ParseXml(System.Xml.XPath.IXPathNavigable extensionData)
            {
                XPathNavigator navigator = extensionData.CreateNavigator();
                XPathNavigator ExtensionTestNode = navigator.SelectSingleNode("extension/ExtensionTest");
    
                if (ExtensionTestNode != null)
                {
                    m_extensiontest = ExtensionTestNode.Value;
                }
            }
    
            protected override void WriteXml(System.Xml.XmlWriter writer)
            {
                writer.WriteStartElement("ExtensionTest");
                writer.WriteString(m_extensiontest);
                writer.WriteEndElement();
            }
    
            public static void RegisterExtensionHandler()
            {
                ItemTypeManager.RegisterExtensionHandler(ExtensionSource, typeof(UserExtensionTest), true);
            }
        }
    

    I've split the addition of a new extension, saving data and retrieving data up.

            void AddExtension1()
            {
                Height height = MyApp.DB.GetSingleValue<Height>(PersonInfo, Height.TypeId);
    
                UserExtensionTest extension = new UserExtensionTest("Extension test 1");
                height.CommonData.Extensions.Add(extension);
    
                PersonInfo.SelectedRecord.NewItem(height);
            }
    
            void SaveData()
            {
                Height height = MyApp.DB.GetSingleValue<Height>(PersonInfo, Height.TypeId);
    
                height.CommonData.Extensions.Add(new UserExtensionTest(XText.Text));
                PersonInfo.SelectedRecord.NewItem(height);
            }
    
           void RetrieveData()
            {
                UserExtensionTest.RegisterExtensionHandler();
                string testoutput = String.Empty;
                Height height = MyApp.DB.GetSingleValue<Height>(PersonInfo, Height.TypeId);
    
                foreach (HealthRecordItemExtension extension in height.CommonData.Extensions)
                {
                    UserExtensionTest hExtension = extension as UserExtensionTest;
                    if (hExtension != null)
                    {
                        testoutput = hExtension.ExtensionText;
                        break;
                    }
                }
                XText.Text = testoutput;
    <span style="white-space:pre">	</span>}
    

    XText is the textbox used for input/output of data.

    There are two issues - I'm not getting the value back out (if it's even going in), and every time I save the data an extra extension is created, currently for height.CommonData.Extensions the Count = 16. I'm only wanting the one extension and the value in it to be overwritten each time the save is called.

    Following on from that, from both the custom data types I was previously working on and this extension of a data type, I've got a lot of unwanted custom items and extensions littering up the records. How do I go about deleting them?

     

    Thursday, February 2, 2012 10:55 AM

All replies

  • It's amazing how deciding to post a question on the forum suddenly means you go away and make progress after being stuck for ages!

    I now know why nothing is being output - only the first of the multiple extensions is being output (because of the 'break' statement) and that one happens to be blank. Without the break, the last extension processed in the foreach loop contains the expected data - the last saved value.

    However, this does still leave the question of why a new extension is being saved every time instead of the old value being overwritten - I have a feeling I'm missing something very simple and obvious...

    How to delete all the unwanted extensions is still unknown as well.

    Thursday, February 2, 2012 11:19 AM