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

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


7 Replies 1 reply marked as answer

PS Pon Selva Jeganathan Syncfusion Team November 18, 2022 04:40 PM UTC

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 06:23 PM UTC

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 04:06 PM UTC

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 04:18 PM UTC

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

Thank you for your help.

John



SG Suganya Gopinath Syncfusion Team November 28, 2022 10:20 AM UTC

John, 

We are glad to know that the issues have been resolved at your end. Please get back to us for further assistance. 

We are marking this ticket as solved. 

Regards,

Suganya Gopinath. 



JF John Fleet December 5, 2022 07:03 PM UTC

Hi, can you please tell me if it is possible to put text next to the "Print" icon when

printing a grid. I am using the following and it works perfectly without any text...

<ToolbarSettings ShowToolbar="True" ToolbarItems="printGrid"></ToolbarSettings>


If I try your previous suggestions (as below) it does not even show the bound grid.

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

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

</ToolbarSettings>

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

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

   Print or PDF

</script>

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

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

</style>

I don't understand how having ToolbarItems="printGrid" and <CustomToolbarItem> work together.

Thank you for your help.

John



PS Pon Selva Jeganathan Syncfusion Team December 6, 2022 02:50 PM UTC

Hi John


Query: If I try your previous suggestions (as below) it does not even show the bound grid.


Based on your query, we suspect that you are not referring to the jsrender script file in your application. If you are using a template in your sample, you should refer to the jsrender script file in the script section.


Please refer to the below help documentation,

https://help.syncfusion.com/js/grid/getting-started#preparing-html-document


Query: I don't understand how having ToolbarItems="printGrid" and <CustomToolbarItem> work together.


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. Actions for this customized toolbar is defined in the toolbarClick event.


Please refer to the below code snippet,


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

        

<ClientSideEvents ToolbarClick="onToolBarClick" />

 

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

                 <CustomToolbarItem>

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

                    </CustomToolbarItem>

            </ToolbarSettings>

        </ej:Grid>

 

….

<script type="text/javascript">

            function onToolBarClick(args) {

                if (args.itemName == "Print")

                  //here print using print method

                    $("# OrdersGrid ").ejGrid("print");

            }

        </script>

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

 

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

       Print or PDF

        </script>

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

          

            .refresh {

                background-image: url("../Content/images/Add csv-02.png"); //Here you need to add the print icon

            }

        </style>



We have already discussed same in our help documentation. Please refer to the below help documentation,

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


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


  1. Share the site.master file
  2. Confirm whether you are referring the jsrender file in your application
  3. Complete grid code example
  4. Video demo of the issue and share the stacktrace details (if face any)
  5. Package version details
  6. Try to replicate the issue in our shared sample or share the issue reproducible sample.


The requested information will be helpful to proceed further








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