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

Getting "Object reference not set to an instance of an object" when clicking on Export To Excel button

3 Answers 1037 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Rajesh
Top achievements
Rank 1
Rajesh asked on 01 Aug 2011, 10:44 AM
Hi,

I am getting the error "Object reference not set to an instance of an object" when I click on Export to Excel button:

Here are the details:

Object reference not set to an instance of an object.

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: Object reference not set to an instance of an object.

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: Object reference not set to an instance of an object.]
   Telerik.Web.UI.GridTableView.PrepareExport() +72
   Telerik.Web.UI.Grid.Export.TableViewExporter.PrepareForExportInternal(GridTableView tableView, Boolean ignorePaging, Boolean dataOnly) +373
   Telerik.Web.UI.GridTableView.ClearTableViewControls(Control control, TableViewExporter exporter, GridTableView ownerTable) +597
   Telerik.Web.UI.Grid.Export.TableViewExporter.PrepareForExportInternal(GridTableView tableView, Boolean ignorePaging, Boolean dataOnly) +670
   Telerik.Web.UI.Grid.Export.TableViewExporter.PrepareForExport() +59
   Telerik.Web.UI.Grid.Export.TableViewExporter.page_PreRender(Object sender, EventArgs e) +34
   System.EventHandler.Invoke(Object sender, EventArgs e) +0
   System.Web.UI.Control.OnPreRender(EventArgs e) +8686422
   System.Web.UI.Control.PreRenderRecursiveInternal() +80
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +842


Version Information: Microsoft .NET Framework Version:2.0.50727.3623; ASP.NET Version:2.0.50727.3618



What might be the possible reason for this?

-Rajesh

3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 01 Aug 2011, 10:57 AM
Hello Rajesh,

I suppose this is a databinding issue. In order to implement advanced feature like Exporting, paging, etc a better approach is to use Advanced data binding using NeedDataSourceEvent. Check the following help documentation which explains more about this.
Advanced Data-binding (using NeedDataSource event).

Thanks,
Princy
0
Rajesh
Top achievements
Rank 1
answered on 01 Aug 2011, 09:51 PM
I am using Need Data Source Event to bind data to the grid. I am still getting the same error. One thing that I noticed is that only when I enable IgnorePaging = true, i get the error. Also looking at the method signature (PrepareForExportInternal) where its failing, I have validated the values for all the parameters in the method signature and nothing is null.

Here is code behind file

/// <summary>
        /// This event is called to populate data on the MainGrid of the page.
        /// </summary>
        /// <param name="source"></param>
        /// <param name="e"></param>
        protected void MainGrid_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
  
            if (ContractSearchResponse == null) return;
  
            contracts = ContractSearchResponse.Contracts;
          
            var columnsDataQuery = (from contract in contracts
                                    orderby String.Concat(contract.InsuredFirstName, string.Empty, String.Empty, contract.InsuredLastName) ascending
                                    select new
                                    {   Insured = String.Format("{0} {1}", contract.InsuredFirstName, contract.InsuredLastName),
                                        ContractNumber = contract.ContractNumber,
                                        Address = string.Concat(contract.AddressLine1, ", ", contract.AddressCityName, ", ", contract.AddressCityCode, ", ", contract.AddressStateCode),
                                        MarketSegment = contract.MarketSegment,
                                        LineOfBusiness = contract.LineType,
                                        EffExpDates = contract.ContractExpDate,
                                        Status = contract.TransactionStatus.ToString(),
                                        RiskState = contract.RiskState,
                                        Premium = String.Format("${0}",contract.RenewalTermPremium.ToString())
                                    }).AsQueryable();
       
            ((RadGrid)source).DataSource = columnsDataQuery;
        }

Here is html code code:

<telerik:RadAjaxManager ID="radAjaxManager" runat="server">
    <ClientEvents OnRequestStart="onRequestStart" />
    <AjaxSettings>
        <telerik:AjaxSetting AjaxControlID="gridResults">
            <UpdatedControls>
                <telerik:AjaxUpdatedControl ControlID="gridResults" LoadingPanelID="RadAjaxLoadingPanel1" />
            </UpdatedControls>
        </telerik:AjaxSetting>
    </AjaxSettings>
</telerik:RadAjaxManager>
<telerik:RadAjaxLoadingPanel ID="radAjaxLoadingPanel" runat="server" Transparency="25"
    CssClass="AjaxLoadingPanel" />
<telerik:RadGrid ID="gridResults" runat="server" CellSpacing="0" GridLines="Both"
    AllowPaging="True" PageSize="20" ShowGroupPanel="True" OnItemCommand="GridResult_ItemCommand" OnItemCreated="GridResult_ItemCreated"
    OnNeedDataSource="MainGrid_NeedDataSource" BorderStyle="Solid" GroupingEnabled="true"
    AutoGenerateColumns="false" AllowSorting="true" Width="100%" ExportSettings-ExportOnlyData="true" ExportSettings-IgnorePaging="true"
    ExportSettings-OpenInNewWindow="true" MasterTableView-EnableViewState="true" >
    <ClientSettings AllowColumnsReorder="false" ReorderColumnsOnClient="true" AllowAutoScrollOnDragDrop="True">
        <Resizing AllowRowResize="True" EnableRealTimeResize="True" ResizeGridOnColumnResize="True"
            AllowColumnResize="True"></Resizing>
    </ClientSettings>
    <PagerStyle CssClass="RadGridPager" Mode="NextPrevNumericAndAdvanced"></PagerStyle>
    <SortingSettings></SortingSettings>
    <ExportSettings HideStructureColumns="true" />
    <MasterTableView AllowMultiColumnSorting="True" EditMode="PopUp" CommandItemDisplay="Top">
        <CommandItemSettings ShowRefreshButton="true" ShowExportToExcelButton="true" ShowAddNewRecordButton="false" />
        <NestedViewTemplate>
            <asp:panel runat="server" id="InnerContainer" cssclass="viewWrap" visible="false">
                <telerik:RadTabStrip runat="server" ID="TabStrip" MultiPageID="Multipage" SelectedIndex="0">
                    <tabs>
                  <telerik:RadTab runat="server" Text="Additional Info" PageViewID="additionalInfoView">
                  </telerik:RadTab>
                  <telerik:RadTab runat="server" Text="Coverage Details" PageViewID="coverageDetailsView">
                  </telerik:RadTab>
                </tabs>
                </telerik:RadTabStrip>
                <telerik:RadMultiPage runat="server" ID="multipage" SelectedIndex="0" RenderSelectedPageOnly="false">
                    <telerik:RadPageView runat="server" ID="additionalInfoView">
                        <telerik:RadGrid ID="additionalInfoGrid" runat="server" CellSpacing="0" GridLines="Both"
                            AllowPaging="True" PageSize="5" ShowGroupPanel="true" OnNeedDataSource="AdditionalInfoGrid_NeedDataSource"
                            Width="60%">
                            <clientsettings allowcolumnsreorder="false" reordercolumnsonclient="true">
                          <Resizing AllowRowResize="True" EnableRealTimeResize="True" ResizeGridOnColumnResize="True" AllowColumnResize="True"></Resizing>
                      </clientsettings>
                            <mastertableview showheader="true" autogeneratecolumns="False" allowpaging="true"
                                pagesize="7" hierarchyloadmode="ServerOnDemand">
                        <Columns>
                            <telerik:GridBoundColumn
                                HeaderText="Customer Number" UniqueName="CustomerNumber" 
                                DataField="CustomerNumber" ItemStyle-CssClass="RadGridItemStyle" HeaderStyle-CssClass="RadGridItemStyle" Visible ="false">
                            </telerik:GridBoundColumn>
                            <telerik:GridBoundColumn
                                HeaderText="Date of Birth" UniqueName="DateOfBirth" 
                                DataField="DateOfBirth" ItemStyle-CssClass="RadGridItemStyle" HeaderStyle-CssClass="RadGridItemStyle" Visible ="false">
                            </telerik:GridBoundColumn>
                            <telerik:GridBoundColumn 
                                HeaderText="Agency" UniqueName="Agency"
                                DataField="Agency" ItemStyle-CssClass="RadGridItemStyle" HeaderStyle-CssClass="RadGridItemStyle" Visible ="false">
                            </telerik:GridBoundColumn>
                            <telerik:GridBoundColumn
                                HeaderText="Producer Code" UniqueName="ProducerCode" 
                                DataField="ProducerCode" ItemStyle-CssClass="RadGridItemStyle" HeaderStyle-CssClass="RadGridItemStyle" Visible ="false">
                            </telerik:GridBoundColumn>
                            <telerik:GridBoundColumn 
                                HeaderText="Last Modified" UniqueName="LastModified" 
                                DataField="LastModified" ItemStyle-CssClass="RadGridItemStyle" HeaderStyle-CssClass="RadGridItemStyle" Visible ="false">
                            </telerik:GridBoundColumn>
                            <telerik:GridBoundColumn 
                                HeaderText="Comment" UniqueName="Comment" DataField="Comment" ItemStyle-CssClass="RadGridItemStyle" HeaderStyle-CssClass="RadGridItemStyle" Visible ="false">
                            </telerik:GridBoundColumn>
                        </Columns>
                     </mastertableview>
                        </telerik:RadGrid>
                    </telerik:RadPageView>
                    <telerik:RadPageView runat="server" ID="coverageDetailsView">
                        <telerik:RadGrid ID="coverageDetailsGrid" runat="server" CellSpacing="0" GridLines="Both"
                            AllowPaging="True" PageSize="5" ShowGroupPanel="true" OnNeedDataSource="CoverageDetailsGrid_NeedDataSource"
                            Width="60%">
                            <clientsettings allowcolumnsreorder="false" reordercolumnsonclient="true">
                         <Resizing AllowRowResize="True" EnableRealTimeResize="True" ResizeGridOnColumnResize="True" AllowColumnResize="True"></Resizing>
                     </clientsettings>
                            <mastertableview showheader="true" autogeneratecolumns="False" allowpaging="true"
                                pagesize="7" hierarchyloadmode="ServerOnDemand">
                        <Columns>
                            <telerik:GridBoundColumn DataField="NumberOfDrivers" 
                                HeaderText="Drivers" 
                                UniqueName="NumberOfDrivers" ItemStyle-CssClass="RadGridItemStyle" HeaderStyle-CssClass="RadGridItemStyle" Visible ="false">
                            </telerik:GridBoundColumn>
                            <telerik:GridBoundColumn DataField="NumberOfVehicles" 
                                HeaderText="Vehicles" 
                                UniqueName="NumberOfVehicles" ItemStyle-CssClass="RadGridItemStyle" HeaderStyle-CssClass="RadGridItemStyle" Visible ="false">
                            </telerik:GridBoundColumn>
                            <telerik:GridBoundColumn DataField="CompCollisionDeductible" 
                                HeaderText="Comp/Collsion Deductibles" UniqueName="CompCollisionDeductible" ItemStyle-CssClass="RadGridItemStyle" HeaderStyle-CssClass="RadGridItemStyle" Visible ="false">
                            </telerik:GridBoundColumn>
                                <telerik:GridBoundColumn DataField="LiabilityLimit" 
                                HeaderText="Policy Limits" UniqueName="LiabilityLimit" ItemStyle-CssClass="RadGridItemStyle" HeaderStyle-CssClass="RadGridItemStyle" Visible ="false">
                            </telerik:GridBoundColumn>
                            <telerik:GridBoundColumn DataField="LinesOfBusinessPremium" 
                                HeaderText="Lines of Business/Premium" UniqueName="LinesOfBusinessPremium" ItemStyle-CssClass="RadGridItemStyle" HeaderStyle-CssClass="RadGridItemStyle" Visible ="false">
                            </telerik:GridBoundColumn>
                            <telerik:GridBoundColumn DataField="RenewalPremiumPerLOB" 
                                HeaderText="Renewal Premium per LOB" UniqueName="RenewalPremiumPerLOB" ItemStyle-CssClass="RadGridItemStyle" HeaderStyle-CssClass="RadGridItemStyle" Visible ="false">
                            </telerik:GridBoundColumn>
                        </Columns>
                    </mastertableview>
                        </telerik:RadGrid>
                    </telerik:RadPageView>
                </telerik:RadMultiPage>
            </asp:panel>
        </NestedViewTemplate>
        <RowIndicatorColumn Visible="True">
        </RowIndicatorColumn>
        <ExpandCollapseColumn Visible="True">
        </ExpandCollapseColumn>
        <Columns>
            <telerik:GridBoundColumn HeaderText="Quote Number" UniqueName="ContractNumber" DataField="ContractNumber"
                ItemStyle-CssClass="RadGridItemStyle" HeaderStyle-CssClass="RadGridItemStyle"
                SortExpression="ContractNumber">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn HeaderText="Insured" UniqueName="Insured" DataField="Insured"
                ItemStyle-CssClass="RadGridItemStyle" HeaderStyle-CssClass="RadGridItemStyle"
                SortExpression="Insured">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn HeaderText="Street Address" UniqueName="Address" DataField="Address"
                ItemStyle-CssClass="RadGridItemStyle" HeaderStyle-CssClass="RadGridItemStyle"
                SortExpression="Address">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn HeaderText="Risk State" UniqueName="RiskState" DataField="RiskState"
                ItemStyle-CssClass="RadGridItemStyle" HeaderStyle-CssClass="RadGridItemStyle"
                SortExpression="RiskState">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn HeaderText="Line Of Business" UniqueName="LineOfBusiness"
                DataField="LineOfBusiness" ItemStyle-CssClass="RadGridItemStyle" HeaderStyle-CssClass="RadGridItemStyle"
                SortExpression="LineOfBusiness">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn HeaderText="Product" UniqueName="MarketSegment" DataField="MarketSegment"
                ItemStyle-CssClass="RadGridItemStyle" HeaderStyle-CssClass="RadGridItemStyle"
                SortExpression="MarketSegment">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn HeaderText="Eff - Exp Dates" UniqueName="EffExpDates" DataField="EffExpDate"
                ItemStyle-CssClass="RadGridItemStyle" HeaderStyle-CssClass="RadGridItemStyle"
                SortExpression="EffExpDates">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn HeaderText="Status" UniqueName="Status" DataField="Status"
                ItemStyle-CssClass="RadGridItemStyle" HeaderStyle-CssClass="RadGridItemStyle"
                SortExpression="Status">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn HeaderText="Agency" UniqueName="Agency" DataField="Agency"
                ItemStyle-CssClass="RadGridItemStyle" HeaderStyle-CssClass="RadGridItemStyle"
                SortExpression="Agency">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn HeaderText="Premium" UniqueName="Premium" DataField="Premium"
                ItemStyle-CssClass="RadGridItemStyle" HeaderStyle-CssClass="RadGridItemStyle"
                SortExpression="Premium">
            </telerik:GridBoundColumn>
            <telerik:GridEditCommandColumn UniqueName="actionsCommandColumn" EditText="Go" ButtonType="PushButton">
            </telerik:GridEditCommandColumn>
        </Columns>
        <EditFormSettings PopUpSettings-Modal="true" InsertCaption="Actions" EditFormType="Template">
            <FormTemplate>
                <Action:QuoteAndpolicyActions ID="quoteAndPolicyActions" runat="server" />
            </FormTemplate>
        </EditFormSettings>
    </MasterTableView>
0
Princy
Top achievements
Rank 2
answered on 02 Aug 2011, 05:13 AM
Hello Rajesh,

I tried the same scenario and it is working as expected in my end. I suppose this issue arises when you are exporting ajaxified RadGrid. Check the following help documentation which explainshow to export a RadGrid when it is ajaxified.
Export from Ajaxified Grid.

Thanks,
Princy.
Tags
Grid
Asked by
Rajesh
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Rajesh
Top achievements
Rank 1
Share this question
or