K
kpg
Hi all,
I am creating an XML file then using that file with an XMLDataScoure.
A Gridview is bound to the XMLDataSource.
Of importance is the fact that the file already exists, and I am
overwriting it with new data on Page_Load.
Here is the sequence:
-Write to existing file, overwriting it with new data.
-Set XMLDatasource.DataFile to the file just written to
Now about 75% of the time the gridview shows data that
was previously in the file, not the current file data.
If I add a delay in the form of writing the file to a log
directory before I write it to the normal location then
it fails only about 10% of the time.
Even if before I delete the file before I write to it I
still get the above behavior!
The only explanation I have for this is that the file delete/write
operation is returning before the file is actual physically committed
the the drive, and the xmldatasource is reading the old data.
By the time I inspect the file after seeing the bad data in the grid
the file is always correct, and a refreah (postback) fixes the gridview
as expected.
Here is the code for the file write, with my addition to delete the file
first (which as I mentioned had no effect).
Public Shared Sub WriteToFile(ByVal fileName As String, _
ByVal data As String, Optional ByVal Append As Boolean = True)
'this added in a futile attempt to fix the problem
Try
Dim theFile As FileInfo = New FileInfo(fileName)
If theFile.Exists Then
If theFile.IsReadOnly Then
theFile.IsReadOnly = False
End If
If Not Append Then
File.Delete(fileName)
End If
End If
Catch ex As Exception
End Try
'this is the original code
Dim sw As StreamWriter
Try
sw = New StreamWriter(fileName, Append)
sw.WriteLine(data)
sw.Flush()
Catch ex As Exception
Finally
If Not sw Is Nothing Then sw.Close()
End Try
End Sub
Assuming my understanding of the problem is ccorrect:
How can I ensure that the data has been fully committed to the hard
drive before I exit this routine?
Otherwise, any ideas?
Thanks,
kpg
I am creating an XML file then using that file with an XMLDataScoure.
A Gridview is bound to the XMLDataSource.
Of importance is the fact that the file already exists, and I am
overwriting it with new data on Page_Load.
Here is the sequence:
-Write to existing file, overwriting it with new data.
-Set XMLDatasource.DataFile to the file just written to
Now about 75% of the time the gridview shows data that
was previously in the file, not the current file data.
If I add a delay in the form of writing the file to a log
directory before I write it to the normal location then
it fails only about 10% of the time.
Even if before I delete the file before I write to it I
still get the above behavior!
The only explanation I have for this is that the file delete/write
operation is returning before the file is actual physically committed
the the drive, and the xmldatasource is reading the old data.
By the time I inspect the file after seeing the bad data in the grid
the file is always correct, and a refreah (postback) fixes the gridview
as expected.
Here is the code for the file write, with my addition to delete the file
first (which as I mentioned had no effect).
Public Shared Sub WriteToFile(ByVal fileName As String, _
ByVal data As String, Optional ByVal Append As Boolean = True)
'this added in a futile attempt to fix the problem
Try
Dim theFile As FileInfo = New FileInfo(fileName)
If theFile.Exists Then
If theFile.IsReadOnly Then
theFile.IsReadOnly = False
End If
If Not Append Then
File.Delete(fileName)
End If
End If
Catch ex As Exception
End Try
'this is the original code
Dim sw As StreamWriter
Try
sw = New StreamWriter(fileName, Append)
sw.WriteLine(data)
sw.Flush()
Catch ex As Exception
Finally
If Not sw Is Nothing Then sw.Close()
End Try
End Sub
Assuming my understanding of the problem is ccorrect:
How can I ensure that the data has been fully committed to the hard
drive before I exit this routine?
Otherwise, any ideas?
Thanks,
kpg