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

Export grid to pdf

Hello,

I am trying to make an export of a grid in vb.net and when i click i get this error :
Une exception de type 'System.NullReferenceException' s'est produite dans Syncfusion.EJ.Export.dll mais n'a pas été gérée dans le code utilisateur

I don't understand why it make this can you help me ?


7 Replies

KK Karthick Kuppusamy Syncfusion Team November 21, 2016 10:12 AM UTC

Hi Guyot, 
 
Thanks for Contacting Syncfusion Support. 
 
We checked your issue and mentioned exception will occurred when the columns bound to the Grid are not defined in the Grid dataSource, and are passed to the exporting method in Code behind.  
  
For more information, refer the below knowledgebase documentation. 
  
 
Please let us know if you have any concern. 
 
Regards, 
K.Karthick. 



GU GUYOT November 22, 2016 07:52 AM UTC

Hi,

thank you for the answer. I agree with toyur diagnostic but i am not able to convert your C# code into VB.net and i cannaot use it. Can you help me ?

Nico


KK Karthick Kuppusamy Syncfusion Team November 23, 2016 11:07 AM UTC

Hi Guyot, 
 
We have analyzed your requirement and we have created a grid sample with exporting in vb.net. 
 
Please find the code example and sample. 
 
Cshtml 
 
 
<ej:Grid ID="Grid1"     AllowPaging="True" 
            EnableRowHover="true"     AllowCellMerging="false"  AllowReordering="false" Locale="en-US" AllowMultiSorting="false"  
            AllowSelection="True" Selectiontype="Single" 
               runat="server"> 
            <Columns> 
                . 
                . 
                . 
            </Columns> 
             <ClientSideEvents ToolbarClick="onToolBarClick" /> 
<ToolbarSettings ShowToolbar="True" ToolbarItems=" excelExport,wordExport,pdfExport,  add,edit,delete,update,cancel"> 
</ToolbarSettings> 
        </ej:Grid> 


Vb.Net 
 
Public Partial Class GridFeatures 
              Inherits System.Web.UI.Page 
              Private order As New List(Of Orders)() 
              Protected Sub Page_Load(sender As Object, e As EventArgs) 
                             BindDataSource() 
              End Sub 
              Private Sub BindDataSource() 
                             Dim orderId As Integer = 10000 
                             Dim empId As Integer = 0 
                             Dim i As Integer = 1 
                             While i < 9 
                                           order.Add(New Orders(orderId + 1, "VINET", empId + 1, 32.38, New DateTime(2014, 12, 25), "Reims")) 
                                           order.Add(New Orders(orderId + 2, "TOMSP", empId + 2, 11.61, New DateTime(2014, 12, 21), "Munster")) 
                                           order.Add(New Orders(orderId + 3, "ANATER", empId + 3, 45.34, New DateTime(2014, 10, 18), "Berlin")) 
                                           order.Add(New Orders(orderId + 4, "ALFKI", empId + 4, 37.28, New DateTime(2014, 11, 23), "Mexico")) 
                                           order.Add(New Orders(orderId + 5, "FRGYE", empId + 5, 67.0, New DateTime(2014, 5, 5), "Colchester")) 
                                           order.Add(New Orders(orderId + 6, "JGERT", empId + 6, 23.32, New DateTime(2014, 10, 18), "Newyork")) 
                                           orderId += 6 
                                           empId += 6 
                                           System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1) 
                             End While 
                             Me.Grid1.DataSource = order 
                             Me.Grid1.DataBind() 
              End Sub 
              <Serializable> _ 
              Public Class Orders 
                             Public Sub New() 
                             End Sub 
                             Public Sub New(orderId As Integer, customerId As String, empId As Integer, freight As Double, orderDate As DateTime, shipCity As String) 
                                           Me.OrderID = orderId 
                                           Me.CustomerID = customerId 
                                           Me.EmployeeID = empId 
                                           Me.Freight = freight 
                                           Me.OrderDate = orderDate 
                                           Me.ShipCity = shipCity 
                             End Sub 
                             Public Property OrderID() As Integer 
                             Public Property CustomerID() As String 
                             Public Property EmployeeID() As Integer 
                             Public Property Freight() As Double 
                             Public Property OrderDate() As DateTime 
                             Public Property ShipCity() As String 
              End Class 
              Protected Sub FlatGrid_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 
Protected Sub FlatGrid_ServerWordExporting(sender As Object, e As Syncfusion.JavaScript.Web.GridEventArgs) 
              Dim exp As New WordExport() 
              exp.Export(Grid1.Model, DirectCast(Grid1.DataSource, IEnumerable), "Export.docx", True, True, "flat-lime") 
End Sub 
Protected Sub FlatGrid_ServerPdfExporting(sender As Object, e As Syncfusion.JavaScript.Web.GridEventArgs) 
              Dim exp As New PdfExport() 
              exp.Export(Grid1.Model, DirectCast(Grid1.DataSource, IEnumerable), "Export.pdf", True, True, "flat-lime") 
End Sub 
End Class 



Regards, 
K.Karthick. 



GU GUYOT January 25, 2017 03:41 PM UTC

Ok, it works fine with this kind of datasource but i can't make it works with an SQLDataSource. 

It makes an "system.NullReferenceException" on bolded text in code below

    Protected Sub FlatGrid_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

Can you help me ?


SA Saravanan Arunachalam Syncfusion Team January 26, 2017 07:42 AM UTC

Hi Guyot, 
It could be possibly occur due to the dataSource passed to the exporting method is compared with the Grid dataSource. Thus, when a column is not defined in the Grid dataSource, it is obtained as null value and hence throws a null exception. So, ensure that the columns bound to the Grid are defined in the Grid dataSource before exporting the Grid. 
If still the issue is exist, please provide the that exception with call stack details. 
Regards, 
Saravanan A. 



GU GUYOT January 26, 2017 08:23 AM UTC

Hi,

with my SqlDataSource defined in aspx file :

                    <asp:SqlDataSource ID="SqlDataSourceOrdinateurs" runat="server" ConnectionString="<%$ ConnectionStrings:DefaultConnection %>" SelectCommand="PsListeOrdinateurDate" SelectCommandType="StoredProcedure">
                        <SelectParameters>
                            <asp:ControlParameter ControlID="DatePicker1" DbType="Date" Name="Date" PropertyName="Value" />
                        </SelectParameters>
                    </asp:SqlDataSource>

And in aspx.vb file :

 Private Sub BindDataSource()
        Grid1.DataSource = SqlDataSourceOrdinateurs
        Grid1.DataBind()
    End Sub
Grid display correctly but now when i click on excel export button i get this :

L'exception System.InvalidCastException n'a pas été gérée par le code utilisateur
  HResult=-2147467262
  Message=Impossible d'effectuer un cast d'un objet de type 'System.Web.UI.WebControls.SqlDataSource' en type 'System.Collections.IEnumerable'.
  Source=Parc
  StackTrace:
       à Parc.Ordinateurs.FlatGrid_ServerExcelExporting(Object sender, GridEventArgs e) dans D:\Dropbox (NGRI)\Visual Studio\Parc\Parc\Ordinateurs.aspx.vb:ligne 46
       à Syncfusion.JavaScript.Web.Grid.PostBackEventHandler(String EventName, Dictionary`2 args)
       à Syncfusion.JavaScript.Web.CommonDataBoundControl.RaisePostBackEvent(String EventArgument)
  InnerException: 
Thank you for help.



RU Ramdhas  Ueikattan Syncfusion Team January 27, 2017 12:02 PM UTC

Hi Guyot, 

Thanks for your update, 

We have analyzed  your query and the invalid cast exception could be possibly occur due to convert the SqlDataSource to IEnumerable directly. So we suggest you to fill the sqlData into the DataTable and then cast it to IEnumerable type. We were achieved your requirement by using DataTable and We create a sample based on your requirements please find the sample . 


Code Snippet  
Partial Public Class GridFeatures 
    Inherits System.Web.UI.Page 
    Private dt As New DataTable 
    Protected Sub Page_Load(sender As Object, e As EventArgs) 
        BindDataSource() 
    End Sub 
    Private Sub BindDataSource() 
        Dim dt As New DataTable("Order") 
        Dim myConnection As New SqlConnection(ConfigurationManager.ConnectionStrings("NORTHWNDConnectionString").ToString()) 
        dt = New DataTable("Order") 
        Dim cmd As New SqlCommand() 
        cmd.Connection = myConnection 
        cmd.CommandText = "select * from Orders" 
        cmd.CommandType = CommandType.Text 
        Dim da As New SqlDataAdapter() 
        da.SelectCommand = cmd 
        If myConnection.State = ConnectionState.Closed Then 
            myConnection.Open() 
        End If 
        da.Fill(dt) 
        Grid1.DataSource = dt 
    End Sub 
    <Serializable> _ 
    Public Class Orders 
        Public Sub New() 
        End Sub 
        Public Sub New(orderId As Integer, customerId As String, empId As Integer) 
            Me.OrderID = orderId 
            Me.CustomerID = customerId 
            Me.EmployeeID = empId 
        End Sub 
        Public Property OrderID() As Integer 
        Public Property CustomerID() As String 
        Public Property EmployeeID() As Integer 
    End Class 
    Private Function ConvertToIenumerable(dataTable As DataTable) As IEnumerable(Of Orders) 
        Dim orders = New List(Of Orders)() 
        For Each row As DataRow In dataTable.Rows 
            Dim order As New Orders() 
            order.OrderID = Convert.ToInt32(row("OrderID")) 
            order.CustomerID = row("CustomerID").ToString() 
            order.EmployeeID = Convert.ToInt32(row("EmployeeID")) 
 
            orders.Add(order) 
       Next 
        Return orders.AsEnumerable() 
    End Function 
 
    Protected Sub FlatGrid_ServerExcelExporting(sender As Object, e As Syncfusion.JavaScript.Web.GridEventArgs) 
 
        Dim exp As New ExcelExport() 
        exp.Export(Grid1.Model, ConvertToIenumerable(Grid1.DataSource), "Export.xlsx", ExcelVersion.Excel2010, True, True, _ 
            "flat-lime") 
    End Sub 
    Protected Sub FlatGrid_ServerWordExporting(sender As Object, e As Syncfusion.JavaScript.Web.GridEventArgs) 
 
        Dim exp As New WordExport() 
        exp.Export(Grid1.Model, ConvertToIenumerable(Grid1.DataSource), "Export.docx", True, True, "flat-lime") 
    End Sub 
    Protected Sub FlatGrid_ServerPdfExporting(sender As Object, e As Syncfusion.JavaScript.Web.GridEventArgs) 
 
        Dim exp As New PdfExport() 
        exp.Export(Grid1.Model, ConvertToIenumerable(Grid1.DataSource), "Export.pdf", True, True, "flat-lime") 
    End Sub 
End Class 
 
Regards,  
Ramdhas U. 


Loader.
Up arrow icon