0

Let's say I have a webform and there are some labels, textboxs on it. The webform also has a gridview to read xml file. Let's say I type a wring file name:

protected void Page_Load(object sender, EventArgs e)
{
   DataSet DS = new DataSet();
   DS.ReadXml(Server.MapPath("~/wrongfilename.xml")); // raise an error
   GridView1.DataSource = DS;
   GridView1.DataBind();
}

and this is the page error handler:

protected void Page_Error(object sender, EventArgs e)
{
   Exception ex = Server.GetLastError();
   Server.ClearError();
}

so I actually handled this error and clear this error. So gridview has no data to display,fair enough. But the page is still a blank page when I execute the program, I think I should at least get textbox, labels displayed with a empty gridview, but it just a blank page. Why the page is not showing other components and how to display other components?

2 Answers2

0

If you're trying to ignore the error, the code you've written in the Page_Error is the wrong way to go about it. Once you've reached Page_Error it's too late to salvage the rendering of the page on which the error occurred. Page_Error is supposed to be used to gracefully handle unexpected exceptions e.g. log the error and/or redirect them to an error page.

If you want your page to continue loading after an error parsing your xml I would suggest you need....

protected void Page_Load(object sender, EventArgs e)
{
   DataSet DS = new DataSet();
   try
   {
       DS.ReadXml(Server.MapPath("~/wrongfilename.xml")); // raise an error
   }
   catch(Exception ex)
   { 
      // error handling 
   }
   GridView1.DataSource = DS;
   GridView1.DataBind();
}

NOTE: Normally I would catch the specific Exceptions you might expect in the normal operation of the program, catching exceptions of type Exception is not advisable. I've used the Exception type as you've not specified which Exceptions you're receiving that you're trying to handle. Read more here...

Why not catch general Exceptions

Community
  • 1
  • 1
Mick
  • 6,527
  • 4
  • 52
  • 67
0

Just Pass/ hide the error

protected void Page_Load(object sender, System.EventArgs e)
{
    try
    {
        throw new Exception();
    }
    catch (Exception ex)
    {
    }
}

public void Page_Error(object sender, EventArgs e)
{
    Response.Write("This page is not valid.");
}
krlzlx
  • 5,752
  • 14
  • 47
  • 55
Ahsan
  • 3
  • 7