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

Sorting on complex object result in NullReferenceException

4 Answers 158 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Danny
Top achievements
Rank 1
Danny asked on 19 Mar 2012, 03:07 PM
Hi,

I have a problem when trying to sorting on a column with a complex object without any value.

My code looks basically as follow:
public class Product{
 
  public int Id {get;set;}
  public string Name {get;set;}
 
  public Statistics Stats {get;set;}
}
 
public class Statistics {
  public int Downloads {get;set;}
}

The grid:

...
 <telerik:GridBoundColumn UniqueName="Id" DataType="System.Int32" DataField="Id" HeaderText="Id">
 </telerik:GridBoundColumn>
 <telerik:GridBoundColumn UniqueName="Name" DataType="System.String" DataField="Name" HeaderText="Name">
 </telerik:GridBoundColumn>
 <telerik:GridBoundColumn UniqueName="Downloads" DataType="System.Int32" DataField="Stats.Downloads" HeaderText="Downloads" DataFormatString="{0:n0}">
 </telerik:GridBoundColumn>
...

When I bind multiple records, some of them have the Stats property null, it's bind correctly. The records without stats are kept blanc for the Downloads column.

However, when I try to sort on the Downloads column, I get a NullReferenceException.

Any idea how to solve this?

Thanks
Danny

4 Answers, 1 is accepted

Sort by
0
Accepted
Shinu
Top achievements
Rank 2
answered on 20 Mar 2012, 01:19 PM
Hello Danny,

I have tried the similar scenario and could reproduce the error when sorting the column ‘Downloads’ (if it contain null value). I could solve this by setting EnableLinqExpressions property of grid as false. Please try this and check whether this eliminates this exception.

aspx:
<telerik:RadGrid ID="RadGrid1" runat="server" OnNeedDataSource="RadGrid1_NeedDataSource" EnableLinqExpressions="false"
            AllowSorting="true"></telerik:RadGrid>

Regards,
-Shinu.
0
Danny
Top achievements
Rank 1
answered on 20 Mar 2012, 03:20 PM
Unfortenately, I still receive the NullReferenceException

Find below my complete grid markup:

<telerik:RadGrid ID="grdData" runat="server" AllowSorting="true" Skin="Default" OnNeedDataSource="grdData_NeedDataSource" OnItemDataBound="grdData_ItemDataBound" AllowPaging="true"
  PageSize="20" AllowFilteringByColumn="true" ShowFooter="false" OnPreRender="grdData_PreRender" EnableLinqExpressions="false">
  <GroupingSettings CaseSensitive="false" />
  <ClientSettings Resizing-ClipCellContentOnResize="false" AllowColumnsReorder="true" ReorderColumnsOnClient="false" EnableRowHoverStyle="true" ColumnsReorderMethod="Reorder">
    <Animation AllowColumnReorderAnimation="true" AllowColumnRevertAnimation="true" />
  </ClientSettings>
  <MasterTableView TableLayout="Auto" AutoGenerateColumns="false" ShowFooter="false">
    <NoRecordsTemplate>
      <div class="grid-empty">
        No data found
      </div>
    </NoRecordsTemplate>
    <ItemStyle HorizontalAlign="Right" />
    <AlternatingItemStyle HorizontalAlign="Right" />
    <HeaderStyle HorizontalAlign="Right" />
    <Columns>
      <telerik:GridBoundColumn UniqueName="Id" DataField="Id" HeaderText="Id" ShowFilterIcon="false" AndCurrentFilterFunction="Contains" AutoPostBackOnFilter="true" FilterControlWidth="3em">
        <HeaderStyle Width="1%" HorizontalAlign="Left" />
        <ItemStyle HorizontalAlign="Left" />
      </telerik:GridBoundColumn>
      <telerik:GridHyperLinkColumn UniqueName="Name" DataNavigateUrlFields="Id" HeaderText="Name" DataNavigateUrlFormatString="/overview/product.aspx?id={0}" DataTextField="Name" SortExpression="Name"
        ItemStyle-Font-Underline="true" ShowFilterIcon="false" DataType="System.String" CurrentFilterFunction="Contains" FilterControlWidth="15em" AutoPostBackOnFilter="true">
        <HeaderStyle HorizontalAlign="Left" />
        <ItemStyle HorizontalAlign="Left" />
      </telerik:GridHyperLinkColumn>
      <telerik:GridBoundColumn UniqueName="Downloads" DataType="System.Int32" DataField="Statistic.Downloads" HeaderText="Downloads" AllowFiltering="false" DataFormatString="{0:n0}">
      </telerik:GridBoundColumn>
      <telerik:GridTemplateColumn UniqueName="Ecpm" HeaderText="eCPM" DataType="System.Decimal" AllowFiltering="false">
      </telerik:GridTemplateColumn>
    </Columns>
  </MasterTableView>
</telerik:RadGrid>

Thanks
0
Shinu
Top achievements
Rank 2
answered on 21 Mar 2012, 08:32 AM

Hello Danny,

tried with your aspx page and it is still working for me. Here is my complete code for more reference. Please paste your code behind if it doesn’t help. Anyway my Telerik version is (2012, 1, 215, 35).

 

<telerik:RadGrid ID="RadGrid1" runat="server" OnNeedDataSource="RadGrid1_NeedDataSource"
           EnableLinqExpressions="false" AllowSorting="true">
           <MasterTableView>
               <Columns>
                   <telerik:GridBoundColumn UniqueName="Id" DataType="System.Int32" DataField="Id" HeaderText="Id">
                   </telerik:GridBoundColumn>
                   <telerik:GridBoundColumn UniqueName="Name" DataType="System.String" DataField="Name"
                       HeaderText="Name">
                   </telerik:GridBoundColumn>
                   <telerik:GridBoundColumn UniqueName="Downloads" DataType="System.Int32" DataField="Stats.Downloads"
                       HeaderText="Downloads" DataFormatString="{0:n0}">
                   </telerik:GridBoundColumn>
               </Columns>
           </MasterTableView>
       </telerik:RadGrid>


C#:
public ArrayList DataSource
   {
       get
       {
           object obj = Session["DataSource"];
           if (obj == null)
           {
               return new ArrayList();
           }
           return (ArrayList)obj;
       }
       set
       {
           Session["DataSource"] = value;
       }
   }
 
   protected void Page_Load(object sender, EventArgs e)
   {
       if (!IsPostBack)
       {
           ArrayList dataSource = new ArrayList();
           Statistics st = new Statistics();
           st.Downloads = 2;
           dataSource.Add(new Product() { Id = 1, Name = "aaa", Stats = st });
           dataSource.Add(new Product() { Id = 2, Name = "bbb" ,});
           this.DataSource = dataSource;
       }
   }
  
   protected void RadGrid1_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
   {
       this.RadGrid1.DataSource = this.DataSource;
   }
 
   public class Product
   {
       public int Id { get; set; }
       public string Name { get; set; }
       public Statistics Stats { get; set; }
   }
 
   public class Statistics
   {
       public int Downloads { get; set; }
   }

Thanks,
Shinu.
0
Danny
Top achievements
Rank 1
answered on 21 Mar 2012, 09:59 AM
Hi Shinu,

When I used this example, it does work. Retrying my orignal page, it does work as well... No idea why it didn't work yesterday.

Anyway, thanks!
Tags
Grid
Asked by
Danny
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Danny
Top achievements
Rank 1
Share this question
or