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
Loading
Sam HobbsPosted Jul 27, 2011, 11:18 PM
So I will take a wild guess and it correct then it is not so wild. Perhaps you use (perhaps write to) the file in the same program then you don't Dispose of the file object that was used to use the file. However I must also say that you should not use Dispose unless you know there is a need for it. Usually C# and .Net makes it unnecessary to call Dispose and usually it is more likely that you could cause a problem using Dispose. In this situation it might be that we know there is a need for it.
Suthish NairPosted Jul 27, 2011, 6:31 AM
Zoran HorvatPosted Jul 27, 2011, 6:10 AM
Zoran
AndrewPosted Jul 27, 2011, 6:05 AM
The C:\ drive is only being used for initial testing, this will eventually be replaced by a network folder path. You did however give me an idea.....i replaced the hard-coded filename with one dynamically created incorporating the current date/time stamp (making it always unique). This has not yet failed initial testing :)
Many thanks.
Zoran HorvatPosted Jul 27, 2011, 5:29 AM
As the first measure, try not to write to C:\ because Windows XP and later prohibit or at least don't agree with writing to root of C:. Try writing to C:\tmp\ or something like that.
Zoran