0

I am using ASP.NET C# and have written a page using the XmlDataSource tool to read an xml file and display the data in a GridView. Everything is great when the file is there. However, the xml file is generated every hour and if you happen to connect to the page while the file is being updated (takes about 2 minutes) an error is displayed (because the file is not there). As I am using the built-in tools to connect and read the xml, is there code that I can use to check if the file exists and, if it does not, open the page without the file.

I can see that there is code to do an if exists, however I can not seem to figure out the part "what to do?" to ignore the XMLDataSource tool. Perhaps I can make a label appear that says to come back in a few minutes, but how do I get it to ignore the data reader?

if (!File.Exists(filename))
{
     // what to do?
}  
user1188241
  • 123
  • 1
  • 3
  • 9
  • 1
    I'm guessing a bit here, but you could a) set an empty xml(?) b) set null to datasource c) don't show the gridview (set visible false) – Mario S Apr 19 '12 at 19:33
  • I'm not sure how much custom logic you can add, if you can, you could generate the xml file to a file named "myxmlfile_generating.xml" and after it is finished delete the old xml file and rename the new one to the original file name. Then you would just have a very small time when it is not available. Of course you should although handle this case. – MatthiasG Apr 19 '12 at 19:44
  • I can set the gridview and a formview I have to false, however the xmldatasource still tries to read the file. I don't know the exact code to set the xmldatasource to not execute. – user1188241 Apr 19 '12 at 19:50
  • @MatthiasG - This is what I am doing, however during the time the file is being updated the page does not load. There is a slight window but this still can happen. – user1188241 Apr 19 '12 at 19:52

2 Answers2

2

If you try to use File.Exists in this way, you're going to be disappointed.

Let's say you have code that works in the simplest way:

if (!File.Exists(filename))
{
    // Tell user that file isn't there.
}
else
{
    // The file exists, so now go party on it.
    DoSomething(filename);
}

So your program determines that the file exists. But before DoSomething is called, the process that creates the file opens it for exclusive access. Your DoSomething method is then going to fail. So your check for the file's existence was irrelevant.

Yes, it's a very small window. I can tell you from experience that things do indeed happen within those very small windows. I've been bitten by code similar to that above.

I strongly suggest that you write your code so that it handles the FileNotFoundException (or whatever exception) that is thrown when the file doesn't exist. For example:

try
{
    DoSomething(filename);
    // now format the page as normal
}
catch (FileNotFoundException)
{
    // notify user that the file wasn't found
}

Done this way, no other process can pull the rug out from under you. You know that the file is there because you have it open.

Jim Mischel
  • 131,090
  • 20
  • 188
  • 351
  • Thx,I tried but I still get the error: `protected void Page_Load(object sender, EventArgs e) { string xmlFile = Server.MapPath("~/App_Data/diskspace.xml"); try { XmlDS.DataFile = xmlFile; XmlDS.EnableCaching = true; XmlDS.CacheDuration = 6000; XmlDS.CacheExpirationPolicy = DataSourceCacheExpiry.Absolute; XmlDS.DataBind(); } catch (Exception ex) { lblE.Visible = true; GV.Visible = false; FV.Visible = false; }` } – user1188241 Apr 19 '12 at 20:32
  • @user1188241: What error are you getting? Does the code in the `catch` block actually get executed? – Jim Mischel Apr 19 '12 at 20:41
  • Update - I found this method and it works. Istuck it in the try. I just need to do some more testing. 'XmlDS.GetXmlDocument(); – user1188241 Apr 19 '12 at 20:43
0

Here is my solution. I change variable names to protect the innocent. Many thanks to Jim Mischel...

protected void Page_Load(object sender, EventArgs e)
{
    string xmlFile = Server.MapPath("~/App_Data/file.xml");

    try
    {
        XmlDataSource1.DataFile = xmlFile;
        XmlDataSource1.GetXmlDocument();
    }
    catch (Exception ex)
    {
        lblErrorMessage.Visible = true;
        gridView1.Visible = false;
        formView1.Visible = false;
    }
}
user1188241
  • 123
  • 1
  • 3
  • 9