Hi,
I am trying to do something that I thought would be really simple, but I spent a day trying to find this and this is the best I could find and it does not work.
I have retived data from SQL server and populated a data grid, not all I want to do is send it out to a CSV file. I found this code below on the net, but when I run it it comes back with an error:
"System.NullReferenceException: 'Object reference not set to an instance of an object.'" at this line:
I am not sure what it means, except for at the end there are no Nulls in the data.
Can someone please tell me how to get this to work, or if you have a better solution please le me know.
Thank you
I am trying to do something that I thought would be really simple, but I spent a day trying to find this and this is the best I could find and it does not work.
I have retived data from SQL server and populated a data grid, not all I want to do is send it out to a CSV file. I found this code below on the net, but when I run it it comes back with an error:
"System.NullReferenceException: 'Object reference not set to an instance of an object.'" at this line:
VB.NET:
csv += cell.Value.ToString().Replace(",", ";") & ","c
Can someone please tell me how to get this to work, or if you have a better solution please le me know.
Thank you
VB.NET:
'Build the CSV file data as a Comma separated string.
Dim csv As String = String.Empty
'Add the Header row for CSV file.
For Each column As DataGridViewColumn In Me.dtgDataGridViewer.Columns
csv += column.HeaderText & ","c
Next
'Add new line.
csv += vbCr & vbLf
'Adding the Rows
For Each row As DataGridViewRow In dtgDataGridViewer.Rows
For Each cell As DataGridViewCell In row.Cells
'Add the Data rows.
csv += cell.Value.ToString().Replace(",", ";") & ","c
Next
'Add new line.
csv += vbCr & vbLf
Next
'Exporting to Excel
Dim folderPath As String = "C:\Temp\"
File.WriteAllText(folderPath & "DataGridViewExport.csv", csv)
End Sub