We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Grid Excel Export Datasource Is Nothing

I am trying to get a simple Grid to export to Excel but I am getting an error saying that the Grid1.Datasource is Nothing when I click on the Excel Export Icon.
The grid is set to use a SQLDatasource as the DatasourceID.
I have tried a few things, but I cannot figure out what to use in the place of Grid1.DataSource.


ASPX Page:
<%@ Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/Site.Master" CodeBehind="dataquery.aspx.vb" Inherits="EcWeb3.dataquery" %>
<asp:Content ID="Content3" ContentPlaceHolderID="MainContent" runat="server">   
    <div class ="body">
        <asp:Panel runat='server' ID="Grid1Panel">
            <ej:Grid ID="Grid1" runat='server' AllowFiltering="True" AllowResizing="True" DataSourceID="SqlDataSource1"
                AllowPaging="True" AllowSorting="True" Width="1200px" OnServerExcelExporting="Grid_ServerExcelExporting" >
                <FilterSettings FilterType="Excel" />
                <ToolbarSettings ShowToolbar="True" ToolbarItems="excelExport" />
                <Columns>
                    <ej:Column DataType="string" Field="FACILITY" IsPrimaryKey="True" TextAlign="Left" Width="40"  />
                    <ej:Column DataType="string" Field="ID" Headertext="Circuit ID" IsPrimaryKey="True" TextAlign="Left" Width="50" />
                    <ej:Column DataType="string" Field="DESCRIPTION" TextAlign="Left" Width="90" />
                    <ej:Column DataType="string" Field="DESCRIPTION_2" TextAlign="Left" Width="90" />
                    <ej:Column DataType="string" Field="TYPE" TextAlign="Left" Width="40" />
                </Columns>
            </ej:Grid>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:EC_WEB %>" ProviderName="<%$ ConnectionStrings:EC_WEB.ProviderName %>" SelectCommand="SELECT [FACILITY], [ID], [DESCRIPTION], [DESCRIPTION_2], [TYPE], [CLIENT] FROM [CIRCUITS] WHERE (([CLIENT] = ?) AND ([FACILITY] = ?))">
                <SelectParameters>
                    <asp:Parameter DefaultValue="BP" Name="CLIENT" Type="String" />
                    <asp:Parameter DefaultValue="ATLANTIS" Name="FACILITY" Type="String" />
                </SelectParameters>
            </asp:SqlDataSource>
        </asp:Panel>
    </div>
</asp:Content>


VB Code Behind:
Imports Syncfusion.EJ.Export
Imports Syncfusion.XlsIO
Public Class dataquery
    Inherits EcPage
    Protected Sub Grid_ServerExcelExporting(sender As Object, e As Syncfusion.JavaScript.Web.GridEventArgs)
        Dim exp As New ExcelExport()
        exp.Export(Grid1.Model, DirectCast(Grid1.DataSource, IEnumerable), "Export.xlsx", ExcelVersion.Excel2010, True, True, "flat-lime")
    End Sub
End Class

3 Replies

PK Prasanna Kumar Viswanathan Syncfusion Team January 19, 2017 01:14 PM UTC

Hi Levi, 

Thanks for contacting Syncfusion support. 

To export to excel in grid while binding SQLDataSource, use select method of SqlDataSource. It is used to retrieves data from the underlying database by using the SelectCommand SQL string. 
 
Find the code example and sample:  
 
 
<ej:Grid ID="FlatGrid" runat="server" AllowSorting="True" AllowGrouping="true" AllowResizing="true"  OnServerExcelExporting="FlatGrid_ServerExcelExporting" AllowPaging="True" AllowFiltering="True" >        
        <FilterSettings FilterType="Excel" /> 
        <ToolbarSettings ShowToolbar="True" ToolbarItems="excelExport" /> 
        <ClientSideEvents DataBound="databound" /> 
        </ej:Grid> 
    <asp:SqlDataSource ID="SqlData" runat="server" ConnectionString="<%$ ConnectionStrings:NORTHWNDConnectionString %>" 
            SelectCommand="SELECT * FROM [Territories]"></asp:SqlDataSource> 
--------------------------------------------- 
protected void FlatGrid_ServerExcelExporting(object sender, Syncfusion.JavaScript.Web.GridEventArgs e) 
        { 
            ExcelExport exp = new ExcelExport(); 
            DataView data = (DataView)SqlData.Select(new DataSourceSelectArguments()); 
            DataTable dt = data.Table; 
            exp.Export(FlatGrid.Model, dt, "Export.xlsx", ExcelVersion.Excel2010, true, true, "flat-lime"); 
        } 


Regards, 
Prasanna Kumar N.S.V 
 



LL Levi Leonards January 19, 2017 07:42 PM UTC

Thank you very much!
Your code example worked perfectly and the Excel Export is now working.


PK Prasanna Kumar Viswanathan Syncfusion Team January 20, 2017 03:47 AM UTC

Hi Levi, 

We are happy to hear that your issue has been resolved. 

Please let us know if you need any further assistance. 

Regards, 
Prasanna Kumar N.S.V 


Loader.
Live Chat Icon For mobile
Up arrow icon