application_error event doesn't fire in global.asax

Giganews Newsgroups
Subject: application_error event doesn't fire in global.asax
Posted by:  c676228 (betty@newsgroup.nospam)
Date: Mon, 14 Sep 2009

hi all,
I have the following in the Globle.asax, but the event doesn't fire when I
created
an exception(the error page comes up, like "Object reference not set to an
instance of an object.").
what I am trying to do is to get all form data so it will help the debug.
The viewstate fields are kipped since request.form.toStrng will include
viewstates
which make the form data unreadable. See below.

Another question is: if my form has quite a number of dynamica controls,
will that be included in the form data collection?
If the program loop through all controls in the form, will the program get
more controls than just form data fields including label, image, dynamic
content etc.?

void Application_Error(object sender, EventArgs e)
    {
        // Code that runs when an unhandled error occurs
        string[] strKeys;
        string[] strValues;
        string formData = "";
        int i;
        int j;

        Exception ex = Server.GetLastError();

        if (typeof(HttpUnhandledException).IsInstanceOfType(ex))
            ex = ex.InnerException;

        /* Build up querystring to match form collection.
    Skip the view state field. */
        strKeys = Request.Form.AllKeys;
        for (i = 0; i < Request.Form.Count; i++)
        {
            strValues = Request.Form.GetValues(i);
            for (j = 0; j < strValues.GetUpperBound(0); j++)
            {
                if (strKeys[i] != "__VIEWSTATE")
                {
                    formData += strKeys[i] + "=" + strValues[j] + "&";
                }
            }
        }
        if (formData.Length > 0)
        {
            //Response.Write(formData);
            //Response.End();
            System.IO.StreamWriter StreamWriter1 =
                new System.IO.StreamWriter(Server.MapPath("test.txt"));
            StreamWriter1.WriteLine(formData);
            StreamWriter1.Close();
        }

    }

--
Betty

Replies