aspx_beginners thread: handling error with dataSet accessing xml file
Message #1 by Cindy.Somerville@h... on Tue, 26 Nov 2002 18:03:05
|
|
Hi
In my code I have
DataSet myDataSet = new DataSet();
myDataSet.ReadXml(Server.MapPath(xmlLocation));
This code runs without errors.
I would like to write code that will handle an error if
the file does not exist, is spelt wrong or is moved.
I tried a try/catch block but it didn't work.
The error is a compile error.... right?
How do I handle the possibility of an error here?
Cindy
Message #2 by "ketty" <jxu787@1...> on Fri, 29 Nov 2002 15:27:58
|
|
I have once met the same question with you . I think you can create a xml
file first,then try it again.
Message #3 by Cindy.Somerville@h... on Fri, 29 Nov 2002 19:59:00
|
|
My file already exists with data in it.
I will need to append the file.
But, I want to ensure that it is still there before trying to write to it.
Message #4 by "ketty" <jxu787@1...> on Sat, 30 Nov 2002 06:06:01
|
|
I suggest you to download the code of ch14 ,there is a good example in the
EditItemTemplate.aspx
Message #5 by "ketty" <jxu787@1...> on Sat, 30 Nov 2002 06:16:01
|
|
The following code is a small example which I wrote yesterday. There is
nothing wrong.
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.IO" %>
<script runat="server">
void Page_Load(object sender, EventArgs e)
{
string sourceXml = Server.MapPath("MyCalendar.xml");
if (!(File.Exists(sourceXml)))
{
label.Text="wrong";
}
DataSet dataSet = new DataSet();
dataSet.ReadXml(sourceXml);
display.DataSource=dataSet;
display.DataBind();
}
</script>
<html>
<head>
</head>
<body>
<form runat="server">
<asp:DataGrid id=display runat=server/>
<asp:Label id=label runat=server/>
</form>
</body>
</html>
|