Andrew

Andrew

  • NA
  • 11
  • 0

Very strange export to csv behaviour!

Jul 27 2011 5:21 AM
Hi All, i have two button on two different tab pages of a tab control. Each one calls the same sub to generate a csv file by providing a filename, the datagridview, a boolean to display columnheaders, and the delimiter value.

As you can see there is no difference between the two calls other than the filename and datagridview reference. So why is it btnExportEnquiry works fine but btnExportVolumes give the error: "Access to the path 'C:\exporttest2.csv' is denied." ?????????????

This happens regardless of what order i click them and whether or not the file already exists. btnExportEnquiry always works, btnExportVolumes always fails.

ExportDGVToCSV(


Private Sub btnExportEnquiry_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExportEnquiry.Click"C:\exporttest.csv", grdEnquiry, True, ",")End Sub

ExportDGVToCSV(


Private Sub btnExportVolumes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExportVolumes.Click"C:\exporttest2.csv", grdDocType, True, ",")End Sub

Private














Sub ExportDGVToCSV(ByVal strExportFileName As String, _ByVal DataGridView As DataGridView, _Optional ByVal blnWriteColumnHeaderNames As Boolean = True, _Optional ByVal strDelimiterType As String = ",")Dim sr As IO.StreamWriter = File.CreateText(strExportFileName)Dim strDelimiter As String = strDelimiterTypeDim intColumnCount As Integer = DataGridView.Columns.Count - 1Dim strRowData As String = ""

If blnWriteColumnHeaderNames Then

strRowData += Replace(DataGridView.Columns(intX).Name, strDelimiter,
IIf(intX < intColumnCount, strDelimiter,


sr.WriteLine(strRowData)


For intX As Integer = 0 To intColumnCount"") & _"")Next intXEnd If

strRowData =
For intX As Integer = 0 To DataGridView.Rows.Count - 1""

strRowData += Replace(DataGridView.Rows(intX).Cells(intRowData).Value, strDelimiter,
IIf(intRowData < intColumnCount, strDelimiter,


sr.WriteLine(strRowData)


sr.Close()
sr.Dispose()
MsgBox(


For intRowData As Integer = 0 To intColumnCount"") & _"")Next intRowDataNext intX"Done!")End Sub

Answers (5)