Get field length from entity field

2 posts, 0 answers
  1. RICHARD FRIEND
    RICHARD FRIEND avatar
    16 posts
    Member since:
    Nov 2008

    Posted 06 May 2009 Link to this post

    Im trying to get the field length from an entity field with a user control that will be bound inside a FormView.

    Thus far i have the following, however im having trouble reflecting 

    OpenAccessEnhancedStateManager

    Any ideas or better approach..( maybe grab from config??)

    private static volatile IDictionary<Type, IDictionary<string, int>oaMaxLenCache = new Dictionary<Type, IDictionary<string, int>>();  
            public static int GetOpenAccessMaxLength(Control cf,string fieldName)  
            {  
                int maxLen = -1;  
                IDataItemContainer nc = cf.NamingContainer as IDataItemContainer;  
                if (nc != null)  
                {  
                    object di = nc.DataItem;  
                    if (di != null)  
                    {  
                        Type t = di.GetType();  
                        if (!oaMaxLenCache.ContainsKey(t)) { oaMaxLenCache.Add(t, new Dictionary<string, int>()); }//Add dict for type  
                        if (oaMaxLenCache[t].ContainsKey(fieldName))  
                        {  
                            //Load from cache  
                            maxLen = oaMaxLenCache[t][fieldName];  
                        }  
                        else  
                        {  
                            PropertyInfo fi = t.GetProperty("OpenAccessEnhancedStateManager",BindingFlags.Public); //Doesnt get the property...?? not sure why  
                            if (fi != null)  
                            {  
                                OpenAccessRuntime.DataObjects.PCStateMan sm = (OpenAccessRuntime.DataObjects.PCStateMan)fi.GetValue(di,null);  
                                if (sm != null)  
                                {  
                                    OpenAccessRuntime.metadata.FieldMetaData stateField = sm.owner.stateFields.Where(m => m.name == fieldName).SingleOrDefault();  
                                    if (stateField != null)  
                                    {  
                                        OpenAccessRuntime.Relational.metadata.RelationalField fld = stateField.storeField as OpenAccessRuntime.Relational.metadata.RelationalField;  
                                        if (fld != null)  
                                        {  
                                            maxLen = fld.mainTableCols[0].length;  
                                            oaMaxLenCache[t].Add(fieldName, maxLen);//Store in cache  
                                        }  
                                    }  
     
                                }  
                            }  
                        }  
                    }  
                }  
                return maxLen;  
            } 
  2. Jan Blessenohl
    Admin
    Jan Blessenohl avatar
    707 posts

    Posted 06 May 2009 Link to this post

    Hi RICHARD FRIEND,
    Maybe you want to use this approach:

    ClassMetaData

     

    [] cmds = Telerik.OpenAccess.Debug.ModelHelper.GetModel(scope1);
    foreach

     

    (ClassMetaData cmd in cmds)
    {
        Console.WriteLine("Class: " + cmd.Namespace + "." + cmd.Name);
        foreach (FieldMetaData fmd in cmd.GetFields())
            Console.WriteLine(" Field: " + fmd.Name + "(" + fmd.ColumnName + "(" + fmd.Length + "))");
    }

     

    You have to link against Telerik.OpenAccess.Runtime

    All the best,
    Jan Blessenohl
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  3. DevCraft banner
Back to Top