This is a migrated thread and some comments may be shown as answers.

RadFilter - FieldEditors collection is empty

1 Answer 116 Views
Filter
This is a migrated thread and some comments may be shown as answers.
Todd
Top achievements
Rank 1
Todd asked on 20 Aug 2013, 01:46 AM
Server Error in '/' Application.
  
FieldEditors collection is empty.
  
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
  
Exception Details: System.NullReferenceException: FieldEditors collection is empty.
  
Source Error:
  
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
  
Stack Trace:
  
  
[NullReferenceException: FieldEditors collection is empty.]
   Telerik.Web.UI.RadFilterDataFieldEditorCollection.System.Collections.Generic.IList<Telerik.Web.UI.RadFilterDataFieldEditor>.get_Item(Int32 index) +156
   Telerik.Web.UI.RadFilter.AddChildExpression(RadFilterGroupExpressionItem groupItem, Boolean isGroup) +118
   Telerik.Web.UI.RadFilterCommandEventArgs.ExecuteCommand(Object source) +173
   Telerik.Web.UI.RadFilter.OnBubbleEvent(Object source, EventArgs args) +185
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +70
   Telerik.Web.UI.RadFilterExpressionItem.OnBubbleEvent(Object source, EventArgs args) +131
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +70
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +29
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2981

I received the above error trying to dynamically create Rad Filters.  The error occurs on Postback when selecting to add a new filter expression.

I am using Telerik.Web.UI, Version=2013.1.403.35.

protected void Page_Load(object sender, EventArgs e)
{
    if (string.IsNullOrEmpty(this.FilterType))
        throw new NullReferenceException("FilterType must not be undefined.");
 
    var instance = Activator.CreateInstance("MyDLL, Version=1.0.0.0, Culture=neutral, PublicKeyToken=123456789", "MyDLL.Data.Biz.MetaData." + this.FilterType);
 
    PropertyInfo[] properties = instance.GetType().GetProperties(
        BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.Public);
 
    foreach (PropertyInfo p in properties)
    {
 
        RadFilterDataFieldEditor f = null;
 
        if (p.PropertyType == typeof(int))
        {
            f = new RadFilterNumericFieldEditor();
        }
 
        if (p.PropertyType == typeof(bool))
        {
            f = new RadFilterBooleanFieldEditor();
        }
 
        if (p.PropertyType == typeof(DateTime))
        {
            f = new RadFilterDateFieldEditor();
        }
 
        if (p.PropertyType == typeof(string))
        {
            f = new RadFilterTextFieldEditor();
        }
 
 
        if (f != null)
        {
            filter.FieldEditors.Add(f);
        }
 
        if (!Page.IsPostBack)
        {
            if (p.PropertyType == typeof(int))
            {
                f.DisplayName = p.Name.HumanizeString();
                f.DataType = typeof(int);
                f.FieldName = p.Name;
                f.ToolTip = p.Name;
            }
 
            if (p.PropertyType == typeof(bool))
            {
                f.DisplayName = p.Name.HumanizeString();
                f.DataType = typeof(bool);
                f.FieldName = p.Name;
                f.ToolTip = p.Name;
            }
 
            if (p.PropertyType == typeof(DateTime))
            {
                f.DisplayName = p.Name.HumanizeString();
                f.DataType = typeof(DateTime);
                f.FieldName = p.Name;
                f.ToolTip = p.Name;
            }
 
            if (p.PropertyType == typeof(string))
            {
                f.DisplayName = p.Name.HumanizeString();
                f.DataType = typeof(string);
                f.FieldName = p.Name;
                f.ToolTip = p.Name;
            }
        }
    }
}

Thanks for any insight.

1 Answer, 1 is accepted

Sort by
0
Antonio Stoilkov
Telerik team
answered on 22 Aug 2013, 07:07 AM
Hi Todd,

The exception could be thrown by incorrect field editors creation or if the FieldEditors collection is empty and does not have and field editors added to it. You could take a look at the help topic below which describes how to correctly add field editors dynamically in code behind. The idea is add the editors only in the initial page load. Additionally, could you confirm that you are adding at least one FieldEditor in the FieldEditors collection?

Regards,
Antonio Stoilkov
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
Tags
Filter
Asked by
Todd
Top achievements
Rank 1
Answers by
Antonio Stoilkov
Telerik team
Share this question
or