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

Error exporting PDF from grid control.

Hi, I am trying to get a PDF grid export working in VB.Net using the following code

   Protected Sub FlatGrid_ServerPdfExporting(ByVal sender As Object, ByVal e As Syncfusion.JavaScript.Web.GridEventArgs)

        Dim exp As PdfExport = New PdfExport

        exp.Export(Grid6.Model, CType(Grid6.DataSource, IEnumerable), "JExport.pdf")

    End Sub


I get the following error when clicking the export button...

Unable to cast object of type 'System.Data.DataTable' to type 'System.Collections.IEnumerable'.


If I comment out the exp.Export line and try again I get the following error ...

Illegal characters in path.


Could you please advise how to resolve this in VB.Net

Also,  Is it possible to add some descriptive text next to the PDF Export button ??

Many thanks , John


4 Replies 1 reply marked as answer

PS Pon Selva Jeganathan Syncfusion Team November 18, 2022 11:40 AM

Hi John,


Query: I get the following error when clicking the export button... Unable to cast object of type 'System.Data.DataTable' to type 'System.Collections.IEnumerable'.


To avoid this issue, we suggest you follow the below code example,


 

Your code:

 

   Dim exp As PdfExport = New PdfExport()

        exp.Export(OrdersGrid.Model, CType(OrdersGrid.DataSource, IEnumerable), "Export.xlsx", ExcelVersion.Excel2010, True, True, "flat-lime")

     

 

Modified code:

 

Dim exp As PdfExport = New PdfExport()

        exp.Export(OrdersGrid.Model, OrdersGrid.DataSource, "Export.pdf", True, True, "flat-lime")

 


Please refer to the below sample,

https://www.syncfusion.com/downloads/support/directtrac/general/ze/vbexport1674555985


If you still facing issue, please provide the following information


  1. Package version details.
  2. Replication steps.
  3. Video demo / screenshot of the issue
  4. If possible, try to reproduce the reported issue in the provided sample or share the reproducible sample.


The requested information will be helpful to proceed further.


Query: Is it possible to add some descriptive text next to the PDF Export button ??


We achieved your requirement by using the custom toolbar item feature of grid. Custom toolbar is used to create your own toolbar items in toolbar


Please refer to the below code snippet,


<ej:Grid ID="OrdersGrid" runat="server"  AllowPaging="True" ……  >

        

            <ToolbarSettings ShowToolbar="True" ToolbarItems="excelExport,wordExport,pdfExport">

                 <CustomToolbarItem>

                    <ej:CustomToolbarItem TemplateID="#Refresh" />

                    </CustomToolbarItem>

            </ToolbarSettings>

        </ej:Grid>

 

….

 

<script id="Refresh" type="text/x-jsrender">

 

            <a class="e-toolbaricons refresh" />

        CSV export

        </script>

     <style type="text/css" class="cssStyles">

          

            .refresh {

                background-image: url("../Content/images/Add csv-02.png");

            }

        </style>


Please refer to the below help documentation,

https://help.syncfusion.com/aspnet/grid/toolbar#custom-toolbar-items


Regards, 

Pon selva 









JF John Fleet November 19, 2022 01:23 PM

Hi, thank you for your reply.

I tried this and I get a null reference exception error (object not set) which appears to come from ej.Export.dll


I have enclosed project references, picture or runtime error and the project files for scrutiny.


Please note that the grid is databound and shows data, all fields are bound to datasource.


Many thanks for your help


Rgds, John


Attachment: PDF_Export_issue_339280dd.zip


PS Pon Selva Jeganathan Syncfusion Team November 21, 2022 11:06 AM

Query: I tried this and I get a null reference exception error (object not set) which appears to come from ej.Export.dll


We can see from your code that you are exporting the SQL data source. When exporting the SqlDataSource, you need to retrieve the data from the database using the SqlDataSource's select method and pass the dataSource to the export() server method.


We have already discussed about how to export the Grid when we bind the SQL DataSource in ASP.NET in our Syncfusion Knowledge base.


Please refer to the below KB documentation,

https://www.syncfusion.com/kb/8342/how-to-export-the-grid-when-we-bind-the-sql-datasource-in-asp-net

For null exception: https://www.syncfusion.com/kb/3018/null-exception-while-exporting


If you still facing issue after trying the above method, please provide the following information


  1. Grid’s model class details
  2. Video demo of the issue and share the stacktrace details (if face any)
  3. Package version details
  4. Try to replicate the issue in our shared sample or share the issue reproducible sample.


The requested information will be helpful to proceed further


If this post is helpful, please consider Accepting it as the solution so that other members can locate it more quickly.




JF John Fleet November 25, 2022 11:18 AM

I have just discovered the 'PrintGrid' option that offers print or PDF Save which is ideal.

Thank you for your help.

John


Marked as answer
Loader.
Live Chat Icon For mobile
Up arrow icon