Quick Search for:  in language:    
earier,example,global,error,handler,written,n
   Code/Articles » |  Newest/Best » |  Community » |  Jobs » |  Other » |  Goto » | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
.Net Stats

 Code: 164,675. lines
 Jobs: 538. postings

 How to support the site

 
Sponsored by:

 
You are in:
 
Login





Latest Code Ticker for .Net.
Arakan Membership Control
By Naing Win on 4/8


Click here to see a screenshot of this code!Quadratic Equation Solver Plus
By d m on 4/6

(Screen Shot)

Click here to see a screenshot of this code!KServiceManager
By Kajal Sinha on 4/6

(Screen Shot)

Click here to see a screenshot of this code!KProxyServer
By Kajal Sinha on 4/6

(Screen Shot)

Tree ADT
By Andrew Powney on 4/5


Click here to see a screenshot of this code!Tetris Attack Demo
By John Shedletsky on 4/5

(Screen Shot)

Click here to see a screenshot of this code!NotePad
By Nabeel Asif on 4/5

(Screen Shot)

Click here to see a screenshot of this code!Remote Explorer with no Winsock
By Nicholas Afxentiou on 4/5

(Screen Shot)

Click here to see a screenshot of this code!H.D.I-hardiskin fo
By fbe1978@hotmail .com on 4/5

(Screen Shot)

Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!

Free Magazine
FREE full subscription to Network World Magazine!


With news, in-depth features, tests/product reviews, and surveys, Network World is designed to help you master ALL aspects of your job AND conquer your networking challenges. Year after year, your colleagues have voted Network World the "most valuable source of networking news".
Click here for more information on a free full-year subscription!

See other free magazines available


Affiliate Sites

 
 
   

Global Error Handler in VB.Net

Print
Email
 

Submitted on: 4/4/2003 10:11:36 AM
By: Troy Blake 
Level: Advanced
User Rating: By 9 Users
Compatibility:VB.NET, ASP.NET

Users have accessed this code 9066 times.
 
 
     I saw the earier example of a global error handler written in C#, but needed it written in VB for my company. I translated the earlier work into my version in VB. It was suggested by a couple of people that I provide my VB version, so here it is. I just hope you find it useful. You can visit the C# version at: http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=948&lngWId;=10 It was submitted by Joel Thoms on 2/5/2003. Thanks to all that asked me to post the VB version. Special thanks to Charles Richardson for helping me track down a bug. When you paste the code into the IDE, most of the formatting should return.

 
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!

    //**************************************
    //     
    // for :Global Error Handler in VB.Net
    //**************************************
    //     
    Original submission was by Joel 
    Thoms, this is just the VB.Net 
    conversion. I didn't write this
    code, I just "translated" it to 
    VB.
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
1) You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.   
2) You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
3) You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
4) You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.

    //**************************************
    //     
    // Name: Global Error Handler in VB.Net
    // Description:I saw the earier example 
    //     of a global 
    error handler written in C#, but 
    needed it written in VB for my 
    company. I translated the earlier 
    work into my version in VB. It was 
    suggested by a couple of people 
    that I provide my VB version, so 
    here it is. I just hope you find 
    it useful.
    You can visit the C# version at: 
    http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=948&lngWId;=10
    It was submitted by Joel 
    Thoms on 2/5/2003. Thanks to all
    that asked me to post the VB version.
    Special thanks to Charles Richardson
    for helping me track down a bug.
    When you paste the code into the IDE,
    most of the formatting should return.
    // By: Troy Blake
    //
    // Inputs:'Sample Use
    Try
    'Regular Code Here
    Catch MyErr As Exception
    ' Catch Errors
    ' Send Email Only
    SendHtmlError(MyErr, "yourname@yourBusiness.com")
    ' Show Error Only
    Response.Write(GetHTMLError(MyErr))
    End Try
    //
    // Assumes:'Code Statement
    SendHtmlError(MyErr, "yourname@yourBusiness.com")
    'To Show error without email
    Response.Write(GetHTMLError(MyErr))
    //
    // Side Effects:I hard-coded some of the
    //     more basic 
    aspects of the email. For example, 
    I hard-coded the from address, email 
    subject, etc. because I knew in 
    advance what I wanted them to be. 
    You could pass these values as part 
    of the call to the sub.
    I just needed an easy way to email 
    the formatted error message to me 
    in the event of an error. This 
    works great in ASP.Net and VB.Net 
    applications.
    //
    //This code is copyrighted and has    // limited warranties.Please see http://
    //     www.Planet-Source-Code.com/vb/scripts/Sh
    //     owCode.asp?txtCodeId=1111&lngWId;=10    //for details.    //**************************************
    //     
    
    'Code Module
    Imports System
    Imports System.Data
    Imports System.Web
    Imports System.Web.Mail
    Imports System.Collections.Specialized
    Module ModError
    Public Sub SendHtmlError(ByVal Ex As Exception, ByVal EmailAddress As String)
    Dim Mail As New MailMessage()
    Mail.From = "ERROR_HANDLER"
    Mail.To = EmailAddress
    Mail.Subject = "Custom Intranet Error"
    Mail.Body = GetHTMLError(Ex)
    Mail.BodyFormat = MailFormat.Html
    SmtpMail.SmtpServer = "100.1.1.1"
    SmtpMail.Send(Mail)
    End Sub
    Public Function GetHTMLError(ByVal Ex As Exception) As String
    'Returns HTML an formatted error message.
    Dim Heading As String
    Dim MyHTML As String
    Dim Error_Info As New NameValueCollection()
    Heading = "<TABLE BORDER=""0"" WIDTH=""100%"" CELLPADDING=""1"" CELLSPACING=""0""><TR><TD bgcolor=""black"" COLSPAN=""2""><FONT face=""Arial"" color=""white""><B> <!--HEADER--></B></FONT></TD></TR></TABLE>"
    MyHTML = "<FONT face=""Arial"" size=""4"" color=""red"">Error - " & Ex.Message & "</FONT><BR><BR>"
    Error_Info.Add("Message", CleanHTML(Ex.Message))
    Error_Info.Add("Source", CleanHTML(Ex.Source))
    Error_Info.Add("TargetSite", CleanHTML(Ex.TargetSite.ToString()))
    Error_Info.Add("StackTrace", CleanHTML(Ex.StackTrace))
    MyHTML += Heading.Replace("<!--HEADER-->", "Error Information")
    MyHTML += CollectionToHtmlTable(Error_Info)
    '// QueryString Collection
    MyHTML += "<BR><BR>" + Heading.Replace("<!--HEADER-->", "QueryString Collection")
    MyHTML += CollectionToHtmlTable(HttpContext.Current.Request.QueryString)
    '// Form Collection
    MyHTML += "<BR><BR>" + Heading.Replace("<!--HEADER-->", "Form Collection")
    MyHTML += CollectionToHtmlTable(HttpContext.Current.Request.Form)
    '// Cookies Collection
    MyHTML += "<BR><BR>" + Heading.Replace("<!--HEADER-->", "Cookies Collection")
    MyHTML += CollectionToHtmlTable(HttpContext.Current.Request.Cookies)
    '// Session Variables
    MyHTML += "<BR><BR>" + Heading.Replace("<!--HEADER-->", "Session Variables")
    MyHTML += CollectionToHtmlTable(HttpContext.Current.Session)
    '// Server Variables
    MyHTML += "<BR><BR>" + Heading.Replace("<!--HEADER-->", "Server Variables")
    MyHTML += CollectionToHtmlTable(HttpContext.Current.Request.ServerVariables)
    Return MyHTML
    End Function
    Public Function CollectionToHtmlTable(ByVal Collection As NameValueCollection) As String
    Dim TD As String
    Dim MyHTML As String
    Dim i As Integer
    TD = "<TD><FONT face=""Arial"" size=""2""><!--VALUE--></FONT></TD>"
    MyHTML = "<TABLE width=""100%"">" & _
    " <TR bgcolor=""#C0C0C0"">" & _
    TD.Replace("<!--VALUE-->", " <B>Name</B>") & _
    " " & TD.Replace("<!--VALUE-->", " <B>Value</B>") & "</TR>"
    'No Body? -> N/A
    If (Collection.Count <= 0) Then
    Collection = New NameValueCollection()
    Collection.Add("N/A", "")
    Else
    'Table Body
    For i = 0 To Collection.Count - 1
    MyHTML += "<TR valign=""top"" bgcolor=""#EEEEEE"">" & _
    TD.Replace("<!--VALUE-->", Collection.Keys(i)) & " " & _
    TD.Replace("<!--VALUE-->", Collection(i)) & "</TR> "
    Next i
    End If
    'Table Footer
    Return MyHTML & "</TABLE>"
    End Function
    Private Function CollectionToHtmlTable(ByVal Collection As HttpCookieCollection) As String
    'Converts HttpCookieCollection to NameValueCollection
    Dim NVC = New NameValueCollection()
    Dim i As Integer
    Dim Value As String
    Try
    If Collection.Count > 0 Then
    For i = 0 To Collection.Count - 1
    NVC.Add(i, Collection(i).Value)
    Next i
    End If
    Value = CollectionToHtmlTable(NVC)
    Return Value
    Catch MyError As Exception
    MyError.ToString()
    End Try
    End Function
    Private Function CollectionToHtmlTable(ByVal Collection As System.Web.SessionState.HttpSessionState) As String
    'Converts HttpSessionState to NameValueCollection
    Dim NVC = New NameValueCollection()
    Dim i As Integer
    Dim Value As String
    If Collection.Count > 0 Then
    For i = 0 To Collection.Count - 1
    NVC.Add(i, Collection(i).ToString())
    Next i
    End If
    Value = CollectionToHtmlTable(NVC)
    Return Value
    End Function
    Private Function CleanHTML(ByVal HTML As String) As String
    If HTML.Length <> 0 Then
    HTML.Replace("<", "<").Replace("\r\n", "<BR>").Replace("&", "&").Replace(" ", " ")
    Else
    HTML = ""
    End If
    Return HTML
    End Function
    End Module


Other 1 submission(s) by this author

 

 
Report Bad Submission
Use this form to notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
Reason:
 
Your Vote!

What do you think of this code(in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor See Voting Log
 
Other User Comments
4/9/2003 11:01:32 AM:Sean Wilkins
Nice, I like it better than the code 
that appeared in the asp.netPRO May 
2003 editon! A lot faster, cleaner and 
more professional than the stuff that 
they tried to pass along!  They had the 
potential, but it looks like you did it!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
4/9/2003 11:33:43 AM:Troy Blake
Just wanted to make sure I say "Thank 
You" to everyone who has voted or sent 
me an email about this code. I'm just 
glad I could help.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
6/5/2003 8:41:48 AM:
great code.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
2/27/2004 9:40:28 AM:l
im unable to get this code running, ok 
i got the code into the modul and it 
works fine... but i dont understand how 
to call the function to enable this 
errorhandle....
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
2/27/2004 10:41:30 AM:l
Me again, i got it with a error 
handling and 
GetHtmlError(Err.GetExeption)
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.

NOTICE: The author of this code has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular code, please click here.
 
Name:
Comment:

 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | .Net Home | Site Home | Other Sites | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyright© 1997-2004 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.  Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.