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.
Unfortunately, activation email could not send to your email. Please try again.

Grid Excel Export Datasource Is Nothing

Thread ID:

Created:

Updated:

Platform:

Replies:

128391 Jan 18,2017 12:13 PM Jan 19,2017 10:47 PM ASP.NET Web Forms 3
loading
Tags: Grid
Levi Leonards
Asked On January 18, 2017 12:13 PM

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

Prasanna Kumar Viswanathan [Syncfusion]
Replied On January 19, 2017 08:14 AM

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 
 


Levi Leonards
Replied On January 19, 2017 02:42 PM

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

Prasanna Kumar Viswanathan [Syncfusion]
Replied On January 19, 2017 10:47 PM

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 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.

;