31 Replies Latest reply on Aug 11, 2015 8:21 AM by Rhys Kirk

    Query in pi System Explorer

    shifana

      Hi All,

       

      I need to update the attribute value and to create a version in pi af database.I can create a version but I couldnt update the attribute value through c# code. Will anyone please resolve this issue.Please refer the code below..

       

      code1.PNG

        • Re: Query in pi System Explorer
          Dan Fishman

          I see the same post here.

           

          You should try and actually obtain the new AFElement that is returned from the Create method.

          AFElement myVersionedElement = myAFElement.Version.Create(tdEffectiveDate, "Intitial Connection Mode Version 2");
          
            • Re: Query in pi System Explorer
              shifana

              Dan,

               

              the attribute value is not not reflecting in the af database but it is updating in the previous version after compiling the code.How to update this in current version and to be reflected in af database also.Please resolve this issue.

                • Re: Query in pi System Explorer
                  Dan Fishman

                  It appears your code is written to update the orginal AFElement and not the new version.  Again, I would recommend using another AFElement object to hold the version.  Here is some sample code that I have on my computer.  I also don't think you want to check to see if an AFValue is null.  What data reference are you using?

                   

                              AFElement versionedE = myElement.Version.Create(DateTime.Now, "version 1 complete") as AFElement;
                              versionedE.Attributes["attribute1"].SetValue(new AFValue("two"));
                  
              • Re: Query in pi System Explorer
                Marcos Vainer Loeff

                Hi Shifana,

                Here are my suggestions for you:

                 

                • If you debug your code and step in, are you sure the SetValue method is called?
                • What is it returned by the GetValue() method?
                • What is the value type of the attribute Encoder and what is the value type of the variable status? Are you sure they are compatible?

                If you still don’t know the issue, I suggest to separate our concerns. For testing purposes, please modify your code so that its only task would be to update the attribute. You can comment the lines related to the versioning. Creating a simple console application for this test is valid as well.

                 

                Finally, if you run the code snippet below, would it work for you?

                 

                 PISystems piSystems = new PISystems();
                            PISystem piSystem = piSystems["MARC-PI2014"];
                            AFDatabase db = piSystem.Databases["AFSDK Test"];
                            AFElement testElement = db.Elements["VersionTest"];
                
                
                            if (testElement == null)
                            {
                                testElement = db.Elements.Add("VersionTest");
                            }
                            AFAttribute testAttribute = testElement.Attributes["UpdateTest"];
                            if (testAttribute == null)
                            {
                                testAttribute = testElement.Attributes.Add("UpdateTest");
                            }
                
                
                            testAttribute.SetValue(new AFValue(2));
                            testAttribute.Database.CheckIn(AFCheckedOutMode.ObjectsCheckedOutThisThread);
                

                 

                Hope it helps!

                  • Re: Query in pi System Explorer
                    shifana

                    Yes Marcos while compiling it., It is working properly but it is not updating in the af database.The second screenshot which i have highlighted doesnt gets updated but in show history("first screenshot") the prtevious version has been updated but not the current version:(:( pls resolve this issue

                     

                    Please resolve this issueamr.PNG

                     

                    code2.PNG

                      • Re: Query in pi System Explorer
                        Marcos Vainer Loeff

                        Hello Shifana,

                         

                        Dan is correct. This is what is happening to your code:

                         

                                    //You get the latest version of an element 
                                    AFElement elementVersion1 = db.Elements["VersionTest"];
                        
                        
                                    //When you create a new version, elementVersion2 becomes the new latest version of the element
                                    AFElement elementVersion2 = elementVersion1.Version.Create(DateTime.Now, "newestVersion 4") as AFElement;
                                    
                                    //You then update elementVersion1, which is not the latest version of the elemenet
                                    elementVersion1.Attributes["UpdateTest"].SetValue(new AFValue(3));
                        
                        

                         

                        You should update elementVersion2 and not elementVersion1.

                         

                        Hope you have understood what is going on!

                          • Re: Query in pi System Explorer
                            shifana

                            Okay Marcos..I understood...sure I will try this method:)

                              • Re: Query in pi System Explorer
                                shifana

                                hi,

                                 

                                I have tried but it doesnt work..Will u pls say me how to update the attribute value for the existing element.??I have tried a lot but couldnt work:(:(

                                pls say some solution

                                  • Re: Query in pi System Explorer
                                    Marcos Vainer Loeff

                                    Hi Shifana,

                                     

                                    If you create an element with an attribute, then create different versions of this element, would you be able to make it work? If not, could you send me your code snippet for this simple example please?

                                      • Re: Query in pi System Explorer
                                        shifana

                                        Marcus I need to update only the attribute value of the existing element. I dont want to create the new element.

                                          • Re: Query in pi System Explorer
                                            Dan Fishman

                                            Shifana Begam A, I think you need to explain exactly what you would like to do.

                                             

                                            If you already have created the version of the element, you can find it in a number of ways.  For example, I can use the ApplyQueryDate method to find the AF object at a specific time context.

                                            AFElement MyNewElement = MyElement.ApplyQueryDate("y") as AFElement;
                                            

                                             

                                             

                                            The below line will create a new element version because you are calling a create method. If you do not want to create a new version, you should not be calling create.

                                            1.    AFElement elementVersion2 = elementVersion1.Version.Create(DateTime.Now, "newestVersion 4") as AFElement;  

                                             

                                             

                                                        

                                              • Re: Query in pi System Explorer
                                                shifana

                                                Marcos my doubt If I want to update the attribute value for (eg: Encoder = AMR ) and I need to change the value to (EG:Encoder = AMI) for the existing element (Eg:123456789) through c# code.No need to create the version. Only I have to update the value this element.What is the procedure for this?? Getting stuck with this.:(:( the attribute value is not updating in my AF database.Please resolve it.

                                                  • Re: Query in pi System Explorer
                                                    Rhys Kirk

                                                    Isn't the issue here that because the Attribute has no data reference then the value will always carry a time stamp of 01/01/1970 thus will always be before any latest version of the element that you create.

                                                      • Re: Query in pi System Explorer
                                                        Dan Fishman

                                                        Rhys Kirk, I'm not sure what the issue is and I don't think enough information has been provided to point us in a clear direction!  Attributes with no data reference, the timestamp is  01/01/1970 if no versions have been created or if it is the first version.  For additional versions, the timestamp is the effective date of the version.

                                                         

                                                        I see Shifana has actually changed the value as we see the "(was Value=..." statement so I'm not sure what is happening.  I don't know what the status object is in the code sample.  It sure does seem like Shifana is accidentally changing what is now the previous element version value.  Without more details I assume it is a mistake.

                                                         

                                                         

                                                        Also, I'm wondering what the use case of this is and perhaps PI Point storage will scale better, be easier to maintain, and work better with other OSIsoft/partner tools.

                                                          • Re: Query in pi System Explorer
                                                            shifana

                                                            Hi Dan and Marcos ...

                                                             

                                                            I have tried your method to update the attribute value in the current version but still not updating ..please say me another method to get updated the attribute value for the existing element in the current version. here is my my code- trying to create the new version and updating the attribute value.pls say sme solution and I need to know what modification I have to do:(:( Waiting for ur reply

                                                             

                                                              AFElement parentAFElement = obj_AFDatabase.Elements["FNI"];

                                                                                                    AFElement childAFElement = parentAFElement.Elements["Meters"];

                                                                                                    AFElement myAFElement = childAFElement.Elements[metId];

                                                                                                    DateTime tdEffectiveDate = DateTime.Now;

                                                                                                    myAFElement.Version.Create(tdEffectiveDate, "Initial Connection Mode Version 2");

                                                                                                    Console.WriteLine(myAFElement.Attributes["Encoder"].GetValue().Value.ToString());

                                                                                                    if (myAFElement.Attributes["Encoder"].GetValue()!= null)

                                                                                                        myAFElement.Attributes["Encoder"].SetValue(new AFValue(status));

                                                                                                    myAFElement.ApplyChanges();

                                                                                                    myAFElement.CheckIn();

                                                                                                    obj_AFDatabase.ApplyChanges();

                                                                                                    Console.WriteLine(myAFElement.Attributes["Encoder"].GetValue().Value.ToString());

                                                              • Re: Query in pi System Explorer
                                                                Rhys Kirk

                                                                Dan Fishman has already given you the answer. Here is your code modified to take that into account:

                                                                 

                                                                            AFElement parentAFElement = obj_AFDatabase.Elements["FNI"];
                                                                            AFElement childAFElement = parentAFElement.Elements["Meters"];
                                                                            AFElement myAFElement = childAFElement.Elements[metId];
                                                                            DateTime tdEffectiveDate = DateTime.Now;
                                                                            myAFElement = myAFElement.Version.Create(tdEffectiveDate, "Initial Connection Mode Version 2") as AFElement;
                                                                            Console.WriteLine(myAFElement.Attributes["Encoder"].GetValue().Value.ToString());
                                                                            if (myAFElement.Attributes["Encoder"].GetValue() != null)
                                                                                myAFElement.Attributes["Encoder"].SetValue(new AFValue(status));
                                                                                
                                                                            myAFElement.ApplyChanges();
                                                                            myAFElement.CheckIn();
                                                                            obj_AFDatabase.ApplyChanges();
                                                                            Console.WriteLine(myAFElement.Attributes["Encoder"].GetValue().Value.ToString());
                                                                

                                                                in particular this line:

                                                                            myAFElement = myAFElement.Version.Create(tdEffectiveDate, "Initial Connection Mode Version 2") as AFElement;
                                                                

                                                                 

                                                                The rest of your code is the same.

                                                                 

                                                                There are some other general programming techniques that you may benefit from...something you should consider as you develop further.