kamagra how much to take

Pretty Up WebPart Exception Display

When you have to dump an error out of a WebPart, you have a lot of ways that you can display the error to the user which will probably vary in terms of what the error actually is composed of. A lot of times, you shouldn’t just dump out the stack trace or other cryptic messages that are going to freak users out when they see it, since it will cause a mass chaos that will result in the burning down of cubes and mass riots fueled by heated confusion. Or I am exaggerating.
Anyways, I had a small WebPart at work that I display all my errors that occur in a small list under the WebPart that occur. Its pretty. Its clean. And it doesn’t cause hysteria if something bad happens.

I keep this in a WebPart base class that I inherit all of my WebParts out of, but you could just as easily paste it in a class or whatever for output:

First, setup an ArrayList as a field you can consume in your class (whether its a custom WebPart base class or you are just using the standard WebPart base derivation):

  1. protected ArrayList _ErrorsArrayList;

Following, initialize this field from your constructor so your arraylist object is cocked, locked, and ready to rock (stolen from Top Gun):

  1. _ErrorsArrayList = new ArrayList();

Then you need two methods, one to add an error to the arraylist, and another to loop through the arraylist and display the exceptions that are housed in it:

Firstly, a small method to add an error to the arraylist, which will be looped through for display later:

  1. protected void AddAnError(Exception error)
  2.  
  3. {
  4. _ErrorsArrayList.Add(error);
  5. }

Secondly, output the errors as a list which can be displayed in the WebPart output:

  1. public void WebPartExceptionList(HtmlTextWriter output)
  2.  
  3. {
  4. if (_ErrorsArrayList.Count > 0)
  5. {
  6. output.RenderBeginTag(HtmlTextWriterTag.Ul);
  7. if (_ErrorsArrayList != null)
  8. foreach (Exception exception in _ErrorsArrayList)
  9. {
  10. output.RenderBeginTag(HtmlTextWriterTag.Li);
  11. output.Write(SPEncode.HtmlEncode(exception.Message));
  12. output.RenderEndTag();
  13. }
  14. output.RenderEndTag();
  15. }
  16. }

Then you are ready to go, as error occurs in your WebPart, they will be displayed as a simple list that users can report much more easily to you. You could use AddStyleAttribute if you wanted to format the list with your own styles if you wanted to, but who the hell cares, we all write perfect code so noone will ever see it anyways :)

Share

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>