+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 15

Thread: Global Error Class

  1. #1
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    Global Error Class

    In order to catch errors etc. I thought I'd create a global error class that I could drop any errors (mainly db errors) into.

    So in a globalBLL I have:-
    Code:
    Namespace MyApp.App.BLL.Globals
        Public Class MyAppError
    #Region "Private Variables"
            Private _err As String = String.Empty
    #End Region
    #Region "Properties"
            Public Property err() As String
                Get
                    Return _err
                End Get
                Set(ByVal value As String)
                    _err = value
                End Set
            End Property
    #End Region
    #Region "Constructors"
            Public Sub New()
            End Sub
            Public Sub New(ByVal _Err As String)
                Me.err = _Err
            End Sub
    #End Region
        End Class
     
    End Namespace
    
    Then I have an AddUser function in my DAL
    Code:
            Public Shared Function AddUser(ByVal user As MyAppUser, ByVal ref As Integer) As Boolean
     
                Dim myErr as New MyAppError
                Dim conn As New MySqlConnection(ConnDAL.connString)
                Dim mycomm As New MySqlCommand("AddUser", conn)
                mycomm.CommandType = Data.CommandType.StoredProcedure
                Using conn
                    Using mycomm
                        Try
                            Dim salt As String = CreateSalt(8)
                            mycomm.Parameters.Add(New MySqlParameter("username", user.username))
                            mycomm.Parameters.Add(New MySqlParameter("pwdHash", CreatePasswordHash(user.pwdHash, salt)))
                            mycomm.Parameters.Add(New MySqlParameter("pwdSalt", salt))
                            mycomm.Parameters.Add(New MySqlParameter("access", user.accessI))
                            mycomm.Parameters.Add(New MySqlParameter("forename", user.forename))
                            mycomm.Parameters.Add(New MySqlParameter("surname", user.surname))
                            Dim pAdd1 As MySqlParameter = mycomm.Parameters.Add("add1", MySqlDbType.VarChar)
                            If String.IsNullOrEmpty(user.add1) Then
                                pAdd1.Value = DBNull.Value
                            Else
                                pAdd1.Value = user.add1
                            End If
                            Dim pAdd2 As MySqlParameter = mycomm.Parameters.Add("add2", MySqlDbType.VarChar)
                            If String.IsNullOrEmpty(user.add2) Then
                                pAdd2.Value = DBNull.Value
                            Else
                                pAdd2.Value = user.add2
                            End If
                            Dim pAdd3 As MySqlParameter = mycomm.Parameters.Add("add3", MySqlDbType.VarChar)
                            If String.IsNullOrEmpty(user.add3) Then
                                pAdd3.Value = DBNull.Value
                            Else
                                pAdd3.Value = user.add3
                            End If
                            Dim pCity As MySqlParameter = mycomm.Parameters.Add("city", MySqlDbType.VarChar)
                            If String.IsNullOrEmpty(user.city) Then
                                pCity.Value = DBNull.Value
                            Else
                                pCity.Value = user.city
                            End If
                            Dim pCounty As MySqlParameter = mycomm.Parameters.Add("county", MySqlDbType.VarChar)
                            If String.IsNullOrEmpty(user.county) Then
                                pCounty.Value = DBNull.Value
                            Else
                                pCounty.Value = user.county
                            End If
                            Dim pPostcode As MySqlParameter = mycomm.Parameters.Add("postcode", MySqlDbType.VarChar)
                            If String.IsNullOrEmpty(user.postcode) Then
                                pPostcode.Value = DBNull.Value
                            Else
                                pPostcode.Value = user.postcode
                            End If
                            Dim pTelno1 As MySqlParameter = mycomm.Parameters.Add("telno1", MySqlDbType.VarChar)
                            If String.IsNullOrEmpty(user.telno1) Then
                                pTelno1.Value = DBNull.Value
                            Else
                                pTelno1.Value = user.telno1
                            End If
                            Dim pTelno2 As MySqlParameter = mycomm.Parameters.Add("telno2", MySqlDbType.VarChar)
                            If String.IsNullOrEmpty(user.telno2) Then
                                pTelno2.Value = DBNull.Value
                            Else
                                pTelno2.Value = user.telno2
                            End If
                            Dim pFaxno1 As MySqlParameter = mycomm.Parameters.Add("faxno1", MySqlDbType.VarChar)
                            If String.IsNullOrEmpty(user.faxno1) Then
                                pFaxno1.Value = DBNull.Value
                            Else
                                pFaxno1.Value = user.faxno1
                            End If
                            Dim pEmail As MySqlParameter = mycomm.Parameters.Add("email", MySqlDbType.VarChar)
                            If String.IsNullOrEmpty(user.email) Then
                                pEmail.Value = DBNull.Value
                            Else
                                pEmail.Value = user.email
                            End If
                            Dim pAddedby As MySqlParameter = mycomm.Parameters.Add("addedby", MySqlDbType.Int16)
                            pAddedby.Value = ref
                            Dim pUserref As MySqlParameter = mycomm.Parameters.Add("ref", MySqlDbType.Int16)
                            pUserref.Direction = Data.ParameterDirection.Output
                            conn.Open()
                            mycomm.ExecuteNonQuery()
                            user.ref = pUserref.Value
                        Catch ex As Exception
                            myErr.err = ex.ToString
                        Finally
                            mycomm.Dispose()
                            conn.Close()
                            conn.Dispose()
                        End Try
                    End Using
                End Using
     
                If String.IsNullOrEmpty(myErr.err) Then
                    Return True
                Else
                    Return False
                End If
     
            End Function
    
    But I can't then obviously get the error instance in the page that called the function. Previously I had an err property in each class I created. Just wondered if there was a global way of handling this?

  2. #2
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Reston, VA
    Posts
    4,547
    Blog Entries
    9
    Real Name
    Jason
    Rep Power
    22

    Ideally, you would have a base class that all of your other BLL classes would inherit. The base class would have all the methods and properties that should be available to all of these classes via inheritence.
    jmurrayhead
    If you agree, give me rep.
    If you like it here...throw us a few bones to help support us.


  3. #3
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    So something like?
    Code:
    Public Class MyAppUser
    Inherits MyAppError
    

  4. #4
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Reston, VA
    Posts
    4,547
    Blog Entries
    9
    Real Name
    Jason
    Rep Power
    22

    Right, but I'd make 'MyAppError' more generic so it would look like it contains methods other than error handling. For example, I name my base class "BizObject".
    jmurrayhead
    If you agree, give me rep.
    If you like it here...throw us a few bones to help support us.


  5. #5
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    Quote Originally Posted by jmurrayhead View Post
    Right, but I'd make 'MyAppError' more generic so it would look like it contains methods other than error handling. For example, I name my base class "BizObject".
    I see...Gotcha...

  6. #6
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Reston, VA
    Posts
    4,547
    Blog Entries
    9
    Real Name
    Jason
    Rep Power
    22

    Take a look here, for more information: http://www.developerbarn.com/blogs/j...ase-class.html
    jmurrayhead
    If you agree, give me rep.
    If you like it here...throw us a few bones to help support us.


  7. #7
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    How do you deal with List(Of ) methods?

    For example, if I have in a page:-
    Code:
    Dim UserList as List(Of MyAppUser) = MyAppUser.GetUserList()
    
    As your GetUserList is not a class, it doesn't inherit the global err class, so you can't set this value in the GetUserList DAL function.

    How do you deal with errors in a List(Of ) method?

  8. #8
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Reston, VA
    Posts
    4,547
    Blog Entries
    9
    Real Name
    Jason
    Rep Power
    22

    Code:
    Try
        Dim UserList as List(Of MyAppUser) = MyAppUser.GetUserList()
    Catch ex As Exception
        DisplayError(ex.Message.ToString)
    End Try
    
    jmurrayhead
    If you agree, give me rep.
    If you like it here...throw us a few bones to help support us.


  9. #9
    Barn Newbie Memnoch will become famous soon enough Memnoch's Avatar
    Join Date
    Nov 2008
    Location
    Missouri
    Posts
    38
    Blog Entries
    1
    Rep Power
    4

    Not sure why you would go through the effort of creating a completely new error class, when you could use either of the following:

    1) The pages Page_Error event as seen below:
    Code:
    Private Sub Page_Error(ByVal sender As Object, ByVal e As System.EventArgs) 
    Handles MyBase.Error
      Handle Your Errors Here    
    End Sub
    
    2) The Applications Application_Error event in the Global.asax file, as seen below:
    Code:
    Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
        Handle Your Errors Here 
    End Sub
    
    Being educated does not make you intelligent.

  10. #10
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    Hmm..I've not read anything about using Page_Error before. Sounds like it might work. I could also use it to email me any errors, which is what my Classic ASP site did...

    Would that also pick up errors from application layers? ie a Data Access Layer and a Business Logic Layer...

    If I had this, for example:-
    In a Data Access Layer
    Code:
    Public Shared Function GetSomething(ByVal ref as integer) as Something
      try
          conn.open
          rs = mycomm.executereader
     
      Catch ex As Exception
         'Would it pick up this error
      End Try
    
    Also, could you just have it in a Master Page and it pick up errors from all .aspx pages, or would one in the master page only pick up errors in the master page?

    If there is an error does it process the rest of the page or stop processing?

    The thing I like about the way I've done it above is being able to pass an error message back to the page like, "An error occurred whilst building the Client List", for example. Can you do something like that with this method?

    Sorry for all the questions. I've not used this before so before I start changing from something I'm used to, I'd just like more of an insight with how it works and what you can and can't do with it.

+ Reply to Thread
Page 1 of 2 1 2 LastLast

Similar Threads

  1. Button Class
    By richyrich in forum HTML & CSS Help
    Replies: 18
    Last Post: February 27th, 2009, 12:32 PM
  2. class name is ambiguous
    By richyrich in forum .NET Development
    Replies: 11
    Last Post: August 12th, 2008, 12:11 PM
  3. Generic Paging Class
    By Shem in forum .NET Development
    Replies: 4
    Last Post: July 18th, 2008, 12:29 PM
  4. FTP Class Library?
    By Wolffy in forum .NET Development
    Replies: 6
    Last Post: May 30th, 2008, 11:26 AM
  5. Class library
    By Shem in forum .NET Development
    Replies: 11
    Last Post: May 22nd, 2008, 08:01 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

SEO by vBSEO