4 Replies Latest reply on Oct 28, 2016 1:05 AM by Eugene Lee

    Adding AF Elements using AF SDK

    natdavidson

      Hello,

       

      I'm pretty new to c#, though i am really eager to learn, i do have experience with VBA.

      I'm trying to create an element to a test database, and learn how to adapt this to a production envrionment.

      I'm able to connect to the PI server and AF server and find the default AF databases, I then use this code:

                  PISystem piSystem = piSystems.DefaultPISystem;

                  var db = piSystem.Databases.DefaultDatabase;

                  Console.WriteLine("Default Database is {0}", db);

                  var Credential = new NetworkCredential("user", "pass", "domain");

       

                  piSystem.Connect(Credential);

       

                  AFElement element = db.Elements.Add("NateTest");

                  element.Description = "Nate's test element";

                  element.CheckIn();

       

                  piSystem.Disconnect();

       

      I get an error saying "An unhandled exception of type 'System.InvalidOperationException' occurred in OSIsoft.AFSDK.dll"

       

      Additional information: Database 'Osisoft-test' has been disconnected. This is caused by using an object from a database after calling PISystem.Disconnect.

       

      Any help would be appreciated.

        • Re: Adding AF Elements using AF SDK
          Eugene Lee

          Hi Nate,

           

          There are 2 ways to resolve this.

          1. (Recommended) Let AF SDK handle the connection by removing

          var Credential = new NetworkCredential("user", "pass", "domain");
          piSystem.Connect(Credential);
          

          from the code.

           

          OR

           

          2. Shift that same block of code

          var Credential = new NetworkCredential("user", "pass", "domain");
          piSystem.Connect(Credential);
          

          above this line instead of below

          var db = piSystem.Databases.DefaultDatabase;
          1 of 1 people found this helpful
            • Re: Adding AF Elements using AF SDK
              David Hearn

              Eugene is correct. The reason you are getting the error is because you obtained the AFDatabase 'db' object using a default connection to the server (the SDK automatically made a connection to the server to read the databases) and then used it after forcing a connection using a credential which must do an automatic disconnect to establish the connection using the specified credential.

            • Re: Adding AF Elements using AF SDK
              natdavidson

              Thanks Eugene Lee, also David (tried to mention you as well but it didn't come up with any suggestions) for the explanation on why this would be the case. I moved the Credential line up to the top, as i would prefer to make a connection explicitly, and it made the test element. But this makes me wonder, if i let AF SDK make the connection, what credentials get used?