File write timing issue

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
 
K

kpg

Assuming my understanding of the problem is ccorrect:


Well...it was not. It's much worse.


I by-pass the file altogether, as the xml I am using is returned from a
web service, so I set the xmldatasource.data property to the xml string.

So now I have: (pseudocode)


sub Page_Load
if not postback then
dim xmlstring as string = getfromwebservice(someargs)
session("mydata") = xmlstring
xmldatasource1.transformfile = "mytransform.xsl"
xmldatasource1.data = session("mydata")
end if
end sub


sub some_handler
'here a different sort tranformation is applied
xmldatasource1.transformfile = "mytransform1.xsl"
xmldatasource1.data = session("mydata")
end sub


On initial site session startup when I visit the page
I get the expected results.

then I go to a different page, change a setting that will
cause the webservice to return a different xml string, go
back to this page...and it still displays the old data!


I find that impossible.


Then, if a postback is fired form a radiobutton to change the
sort (by changing the transformfile) the data is still wrong!


however (this is a big however) the session("mydata") is the
correct xml!

How can this be?


The xmldatasoure or the frivview is presisting data from a
previous page load...but where?

I am at a total loss.

Next strp is to try to reporduce this behaviour in a simple
two page web app with contrived data (like a timestamp) instead
of the webservice.

any ideas?

thanks,
kpg
 
K

kpg

I built a simple tow page site and was able to reporduce this.


This has got to be a browser caching issue.
 
K

kpg

Here is the page load on page 2


Protected Sub Page_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Load

If Not Page.IsPostBack Then

Dim theTime As String = Now.ToString

XmlDataSource1.Data = "<?xml version=""1.0"" ?><records><record id='1'>
<time>" & theTime & "</time></record></records>"
Label1.Text = theTime

End If

end sub


On page 1 I have a inkbutton to page 2
On page 2 I have a link button to page 1

On first run the label time and grid time match.

Then navigating to page 1 then back to page 2
cuases the label time to update but NOT the grid time.


Could someone please explain this to me?

many thanks,
kpg
 
K

kpg

Here is the page load on page 2


Protected Sub Page_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Load

If Not Page.IsPostBack Then

Dim theTime As String = Now.ToString

XmlDataSource1.Data = "<?xml version=""1.0"" ?><records><record
id='1'>
<time>" & theTime & "</time></record></records>"
Label1.Text = theTime

End If

end sub


On page 1 I have a inkbutton to page 2
On page 2 I have a link button to page 1

On first run the label time and grid time match.

Then navigating to page 1 then back to page 2
cuases the label time to update but NOT the grid time.


Could someone please explain this to me?

many thanks,
kpg

here is the page 2 html.


<asp:GridView ID="GridView1" runat="server"
DataSourceID="XmlDataSource1">

<Columns>

<asp:TemplateField HeaderText="time">
<ItemTemplate>
<%#XPath("time")%>
</ItemTemplate>
</asp:TemplateField>

</Columns>


</asp:GridView>

<asp:XmlDataSource ID="XmlDataSource1" runat="server">
</asp:XmlDataSource>
 
K

kpg

heh, learn something new everyday...


XmlDataSource1.EnableCaching = False


thanks for listening

kpg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top