11 Replies Latest reply on May 30, 2014 1:46 PM by Gregor

    Attribute.GetValue() returns "Specified cast is invalid". Why is this?

    AlistairFrith

      We have a project that collects the current values for a number of attributes of an element. These are all PI-Point attributes. On our development system, these values are all numeric and everything is lovely. But on the customer's site (on which we cannot run a debugger), the application displays "The specified cast is invalid" for all these PIPoint attributes. If I look at them in PI System explorer, they all have good values. Here is the (rather ugly) code:

       
      double testDouble;
      bool canParseDouble = double.TryParse(subRootItem.GetValue().ToString(), out testDouble);
      
      subAttr.SubAttributes.Add
      ( 
          (subRootItem.Name != null)  ? subRootItem.Name : "N/A", 
          new AbstractedAFAttribute()
              {
                  Name = (subRootItem.Name != null) ? subRootItem.Name : "N/A",
      
                  Value = ( (canParseDouble)
                              ? String.Format("{0:0.00}", Convert.ToDouble(subRootItem.GetValue().ToString())) 
                              : 
      "Not numeric: " + subRootItem.GetValue().ToString())
                          + ( (subRootItem.DefaultUOM != null && subRootItem.DefaultUOM.Abbreviation != null && canParseDouble)
                              ? subRootItem.DefaultUOM.Abbreviation.ToString() 
                              : "")
                          + "    " 
                          + "(" + (subRootItem.GetValue().Timestamp != null ? subRootItem.GetValue().Timestamp.ToString() : "No Timestamp available") + ")",
      
                  PITag = (subRootItem.DataReference != null) ? subRootItem.DataReference.ToString() : "N/A",
              }
      );
      

      The red-highlighted bit is what we get at the customer site and the resultant text is "Not numeric: Specified cast is not valid" . subRootItem is an AFAttribute object.

       

      Any ideas?

       

      --- Alistair.

        • Re: Attribute.GetValue() returns "Specified cast is invalid". Why is this?
          AlistairFrith

          Ah, so the 'preview' tab does not actually show you the message as it will appear on the forum. Great. I will highlight the line another way then...

           

           

           
          double testDouble;
          bool canParseDouble = double.TryParse(subRootItem.GetValue().ToString(), out testDouble);
           
          subAttr.SubAttributes.Add
          ( 
              (subRootItem.Name != null)  ? subRootItem.Name : "N/A", 
              new AbstractedAFAttribute()
                  {
                      Name = (subRootItem.Name != null) ? subRootItem.Name : "N/A",
           
                      Value = ( (canParseDouble)
                                  ? String.Format("{0:0.00}", Convert.ToDouble(subRootItem.GetValue().ToString())) 
                                  : "Not numeric: " + subRootItem.GetValue().ToString())  // <-- *** THIS IS WHAT HAPPENS AT THE CUSTOMER SITE. ***
                              + ( (subRootItem.DefaultUOM != null && subRootItem.DefaultUOM.Abbreviation != null && canParseDouble)
                                  ? subRootItem.DefaultUOM.Abbreviation.ToString() 
                                  : "")
                              + "    "
                              + "(" + (subRootItem.GetValue().Timestamp != null ? subRootItem.GetValue().Timestamp.ToString() : "No Timestamp available") + ")",
           
                      PITag = (subRootItem.DataReference != null) ? subRootItem.DataReference.ToString() : "N/A",
                  }
          );
          

          Is that clearer? 

           

          --- Alistair.