Export to CSV

12 posts, 1 answers
  1. ronald
    ronald avatar
    9 posts
    Member since:
    Mar 2011

    Posted 10 Mar 2011 Link to this post

    Need some help here...

    I'm exporting a RadDataGrid to CSV but get the 'object reference to instance of an object' error. Every time i have a empty *.csv doc and the csvExporter.RunExport crashes...

    Changing it to an Excel exporter makes it work fine.

    code:

    Dim exportGridView As New Telerik.WinControls.UI.RadGridView
     exportGridView.DataSource = rep.GetObjectCorrespondentieGegevens(projectId)
    Public Function GetObjectCorrespondentieGegevens(ByVal id As Integer) As Object Implements     ICorrespondentieRepository.GetObjectCorrespondentieGegevens
               Dim model = From p In _db.tblAannemers.AsEnumerable() _
                           Select p.fldAannemerNaam
               Return model
           End Function
    Private Shared Sub RunExportToCSV(ByVal fileName As String, ByRef gridView As RadGridView)
               Dim csvExporter As ExportToCSV = New ExportToCSV(gridView)
               'csvExporter.FileExtension = "txt"
               'csvExporter.SummariesExportOption = SummariesOption.ExportAll
               Try
                   'csvExporter.
                   csvExporter.SummariesExportOption = SummariesOption.DoNotExport
                   csvExporter.RunExport(fileName) ' this is where the exception happens :-(
               Catch ex As Exception
               End Try
           End Sub
  2. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 10 Mar 2011 Link to this post

    Hello,

    I haven't been able to replicate this issue. I have prepared you a sample that exports to CSV that can be pasted into a new project. Please can you let me know if this works for you

    Designer File
    <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
    Partial Class Form1
        Inherits System.Windows.Forms.Form
      
        'Form overrides dispose to clean up the component list.
        <System.Diagnostics.DebuggerNonUserCode()> _
        Protected Overrides Sub Dispose(ByVal disposing As Boolean)
            Try
                If disposing AndAlso components IsNot Nothing Then
                    components.Dispose()
                End If
            Finally
                MyBase.Dispose(disposing)
            End Try
        End Sub
      
        'Required by the Windows Form Designer
        Private components As System.ComponentModel.IContainer
      
        'NOTE: The following procedure is required by the Windows Form Designer
        'It can be modified using the Windows Form Designer.  
        'Do not modify it using the code editor.
        <System.Diagnostics.DebuggerStepThrough()> _
        Private Sub InitializeComponent()
            Me.RadGridView1 = New Telerik.WinControls.UI.RadGridView()
            Me.RadButtonExport = New Telerik.WinControls.UI.RadButton()
            CType(Me.RadGridView1, System.ComponentModel.ISupportInitialize).BeginInit()
            CType(Me.RadButtonExport, System.ComponentModel.ISupportInitialize).BeginInit()
            Me.SuspendLayout()
            '
            'RadGridView1
            '
            Me.RadGridView1.Location = New System.Drawing.Point(12, 13)
            Me.RadGridView1.Name = "RadGridView1"
            Me.RadGridView1.Size = New System.Drawing.Size(320, 286)
            Me.RadGridView1.TabIndex = 0
            Me.RadGridView1.Text = "RadGridView1"
            '
            'RadButtonExport
            '
            Me.RadButtonExport.Location = New System.Drawing.Point(245, 305)
            Me.RadButtonExport.Name = "RadButtonExport"
            Me.RadButtonExport.Size = New System.Drawing.Size(87, 24)
            Me.RadButtonExport.TabIndex = 2
            Me.RadButtonExport.Text = "Export CSV"
            '
            'Form1
            '
            Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
            Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
            Me.ClientSize = New System.Drawing.Size(344, 340)
            Me.Controls.Add(Me.RadButtonExport)
            Me.Controls.Add(Me.RadGridView1)
            Me.Name = "Form1"
            Me.Text = "Form1"
            CType(Me.RadGridView1, System.ComponentModel.ISupportInitialize).EndInit()
            CType(Me.RadButtonExport, System.ComponentModel.ISupportInitialize).EndInit()
            Me.ResumeLayout(False)
      
        End Sub
        Friend WithEvents RadGridView1 As Telerik.WinControls.UI.RadGridView
        Friend WithEvents RadButtonExport As Telerik.WinControls.UI.RadButton
      
    End Class

    Form1.vb
    Imports Telerik.WinControls.UI
    Imports Telerik.WinControls
    Imports Telerik.Data
    Imports System.ComponentModel
    Imports Telerik.WinControls.UI.Export
      
      
    Public Class Form1
      
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Me.RadGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill
            Dim list As New BindingList(Of Person)()
            For i As Integer = 0 To 50
                list.Add(New Person("Name " & i.ToString(), "Description " & i.ToString(), i, True))
            Next
            Me.RadGridView1.DataSource = list
      
        End Sub
      
        Private Sub RadButtonExcel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadButtonExport.Click
            Dim export As New ExportToCSV(Me.RadGridView1)
            export.RunExport("Your file path...\MyFile.csv")
        End Sub
      
    End Class
      
    Public Class Person
        Public Sub New(ByVal name As String, ByVal description As String, ByVal id As Integer, ByVal hasBeard As Boolean)
            Me.Name = name
            Me.Description = description
            Me.Id = id
            Me.HasBeard = hasBeard
        End Sub
      
        Public Property Name As String
        Public Property Description As String
        Public Property Id As Integer
        Public Property HasBeard As Boolean
    End Class

    Hope this helps
    Richard
  3. ronald
    ronald avatar
    9 posts
    Member since:
    Mar 2011

    Posted 10 Mar 2011 Link to this post

    Sorry, didn't work.

    Still get the object reference not set when the Runexport methode is executed.

    what Telerik dll's do i need for this in the bin folder of my application?

  4. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 10 Mar 2011 Link to this post

    Hello,

    As per this help topic you will need to include the TelerikData.dll

    The collowing namespaces are used:
    Telerik.WinControls.Data
    Telerik.WinControls.UI.Export

    Please could you confirm which version you are using and post a stack trace if this still doesn't work for you
    thanks
    Richard
  5. ronald
    ronald avatar
    9 posts
    Member since:
    Mar 2011

    Posted 10 Mar 2011 Link to this post

    I only make an inmemory instance of the GridView... When I put it on the Form it works...
  6. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 10 Mar 2011 Link to this post

    Hello,

    You mentioned that the sample that I sent to you caused the same issue. Is this still the case, or do you now have this resolved?
    Thanks
    Richard
  7. ronald
    ronald avatar
    9 posts
    Member since:
    Mar 2011

    Posted 10 Mar 2011 Link to this post

    when i put the gridview designtime on the form it works.

    when i make an in memory instance of the gridview, in my class, it works NOT!
  8. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 10 Mar 2011 Link to this post

    Hello,

    Please post a sample application here to demonstrate the problem using the Format Code block which will help me to assist you further.
    Thanks
    Richard
  9. ronald
    ronald avatar
    9 posts
    Member since:
    Mar 2011

    Posted 10 Mar 2011 Link to this post

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Me.RadGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill
            Dim rep As DAL.Repository.CorrespondentieRepository = New DAL.Repository.CorrespondentieRepository
            'Dim list As New BindingList(Of Person)()
            'For i As Integer = 0 To 50
            '    list.Add(New Person("Name " & i.ToString(), "Description " & i.ToString(), i, True))
            'Next
            'Me.RadGridView1.DataSource = rep.GetObjectCorrespondentieList(32)
            Dim gv2 As RadGridView = New RadGridView()
            gv2.DataSource = rep.GetObjectCorrespondentieList(32)
            Dim export2 As New ExportToCSV(gv2)
            'Dim export As New ExportToCSV(Me.RadGridView1)
            export2.RunExport("c:\MyFile.csv")
      
        End Sub
    this results in an exception like: null reference...
    Changing it to an ExportToExcelML(gv2)  WORKS???
  10. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 10 Mar 2011 Link to this post

    Hello,

    Please consider the following code. I hope this helps, but if you need further help, please let me know

    Imports Telerik.WinControls.UI
    Imports Telerik.WinControls.UI.Export
    Imports Telerik.WinControls.Data
    Imports System.ComponentModel
      
      
    Public Class Form1
      
        Private source As New BindingList(Of Person)()
      
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            CreateMyDataSource()
      
            Dim gv2 As RadGridView = New RadGridView()
            Me.Controls.Add(gv2)
            gv2.Visible = False
            gv2.DataSource = source
            Dim export As New ExportToCSV(gv2)
            export.RunExport("C:\Users\Richard\MyFile.csv")
      
            MessageBox.Show("Done")
        End Sub
      
      
        Private Sub CreateMyDataSource()
            For i As Integer = 0 To 10
                Me.source.Add(New Person(i, "Name " & i.ToString()))
            Next
        End Sub
    End Class
      
    Public Class Person
        Public Sub New(ByVal id As Integer, ByVal name As String)
            Me.Name = name
            Me.Id = id
        End Sub
      
        Public Property Name As String
        Public Property Id As Integer
    End Class

    Regards,
    Richard
  11. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 11 Mar 2011 Link to this post

    Hello,

    did this help? If so, please mark as answer. If you need further assistance please let me know
    thanks
    Richard
  12. Answer
    Martin Vasilev
    Admin
    Martin Vasilev avatar
    1061 posts

    Posted 15 Mar 2011 Link to this post

    Hello ronald,

    I just want to add that we do not support exporting an instance of RadGridView, which is not added to any form. The work-around, which Richard has already provided, to put a grid on a form with Visible = false is the right way to overcome this limitation in your case.

    Best wishes,
    Martin Vasilev
    the Telerik team
    Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
Back to Top