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

RadGrid aggregate = sum error when no data

1 Answer 185 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Nestor
Top achievements
Rank 1
Nestor asked on 03 Dec 2008, 01:07 PM
I have setup a RadGrid column to use Aggregate="sum", everything works fine... until there is no data to show, in which case I get the error "Object reference not set to an instance of an object.".

The RadGrid connects to a ObjectDataSource inline, the ObjectDataSource connects to a BLL function that returns a DataTable, when DataTable is null (no records founded), then I get the error. I guess I can go around this problem by setting the Aggregate="sum" in code behind in a proper event only when there is data, but... should not the RadGrid cover this case? If some one knows how please help!. Thanks. Below the code (the column TotalAmount contains the aggregate).

 

            <telerik:RadGrid ID="grdOrder" runat="server" DataSourceID="ObjectDataSource1" Skin="Web20"   
                AllowPaging="True" AllowSorting="false" ShowFooter="true" 
                GridLines="None" AutoGenerateColumns="False">  
            <MasterTableView DataSourceID="ObjectDataSource1">  
                <NoRecordsTemplate> 
                    No orders to display  
                </NoRecordsTemplate>      
                <Columns> 
                    <telerik:GridBoundColumn DataField="CustomerID" HeaderText="Customer">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="CustomerName" HeaderText="">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="OrderID" HeaderText="Order No">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="TotalAmount" HeaderText="Total"   
                        DataFormatString="{0:N}" Aggregate="Sum">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="CurrencyID" HeaderText="CY">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="DateCreated" HeaderText="Order Date"   
                        DataFormatString="{0:d}">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="DatePaid" HeaderText="Date Paid"   
                        DataFormatString="{0:d}">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="DateShipped" HeaderText="Date Shipped"   
                        DataFormatString="{0:d}">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridHyperLinkColumn DataNavigateUrlFields="OrderID"   
                        DataNavigateUrlFormatString="Order.aspx?orderID={0}" Text="Details...">  
                    </telerik:GridHyperLinkColumn> 
                </Columns> 
            </MasterTableView> 
            <PagerStyle Mode="NextPrevAndNumeric" Position="TopAndBottom" /> 
            </telerik:RadGrid> 
 
    <asp:ObjectDataSource ID="ObjectDataSource1" runat="server"   
        SelectMethod="ShippedOrders" 
        TypeName="Cloud1.Reports.Orders">  
        <SelectParameters> 
            <asp:Parameter Name="customerID" Type="Int32" DefaultValue="0" /> 
            <asp:ControlParameter ControlID="dtmFromDateCreated" Name="fromDateCreated"   
                PropertyName="SelectedDate" Type="String" /> 
            <asp:ControlParameter ControlID="dtmToDateCreated" Name="toDateCreated"   
                PropertyName="SelectedDate" Type="String" /> 
            <asp:ControlParameter ControlID="dtmFromDateShipped" Name="fromDateShipped"   
                PropertyName="SelectedDate" Type="String" /> 
            <asp:ControlParameter ControlID="dtmToDateShipped" Name="toDateShipped"   
                PropertyName="SelectedDate" Type="String" /> 
        </SelectParameters> 
    </asp:ObjectDataSource> 

1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 04 Dec 2008, 12:19 PM
Hi,

Try setting the datasource  of the RadGrid to  an empty string when the  DataTable is null 

protected

void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)

 

{

 

  if(dtTable == null)

 

     RadGrid1.DataSource =

String.Empty;

 

}

Thanks,
Princy

Tags
Grid
Asked by
Nestor
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Share this question
or