Export Dataset to CSV File using VB.Net

The following VB.Net Code Snippet is used to export Dataset to csv File.

All Data tables of a data set will be exported to CSV File.

Private Sub ExportDatasetToCsv(ByVal MyDataSet As DataSet)

'Declaration of Variables
Dim dt As DataTable
Dim dr As DataRow

Dim myString As String
Dim bFirstRecord As Boolean = True

Dim myWriter As New System.IO.StreamWriter("C:\MyTestCSV.csv")

myString = ""


For Each dt In MyDataSet.Tables

For Each dr As DataRow In dt.Rows

bFirstRecord = True

For Each field As Object In dr.ItemArray

If Not bFirstRecord Then


End If


bFirstRecord = False


'New Line to differentiate next row



Catch ex As Exception

MsgBox (ex.Message)

End Try

'Write the String to the Csv File


'Clean up

End Sub


ExportDatasetToCsv (ds)

Where ds is the dataset.

It will export all data tables of data set to csv File C:\MyTestCSV.csv

Reference: http://vijirajkumar.blogspot.com/2009/06/export-dataset-to-csv-file-using-vbnet.html


Author: pratik12 Apr 2010 Member Level: Gold   Points : 0

hey IT Show Error That Dt is not a Collectino type

Author: Viji RAJKUMAR19 Apr 2010 Member Level: Gold   Points : 1

Hi Pratik,

The issue is solved.

The line should be

For Each dr As DataRow In dt.Rows, the .Rows was missing .

Thanks for the information.

Check now.

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: