locked
Class Library - How to show the error occur when using the method from the Class Library RRS feed

  • Question

  • Good Day Everyone

    I have created a Class Library, that has functionality that calls SQL procedures, then I import it on my ASP.NET MVC project, I'm okay when using my DLL but the problem is when the is an error happened inside the method, I have no idea what is the source or the cause of the error.

        Public Sub queryExecute(sqlString As String, isStoredProcedure As Boolean, GetParameters() As String, GetParameterValues() As String)
            Try
    
                Dim connStr As String = ConfigurationManager.ConnectionStrings(connectionName).ToString
    
                Using sqlConn As New SqlConnection(connStr)
    
                    Using sqlCmd As New SqlCommand(sqlString, sqlConn)
    
                        If isStoredProcedure = True Then
                            sqlCmd.CommandType = CommandType.StoredProcedure
                        Else
                            sqlCmd.CommandType = CommandType.Text
                        End If
    
                        Dim i As Integer, getI As Integer
    
                        getI = GetParameters.Length
    
                        For i = 0 To getI - 1
                            sqlCmd.Parameters.AddWithValue(GetParameters(i), GetParameterValues(i))
                        Next
    
                        sqlConn.Open()
                        sqlCmd.ExecuteNonQuery()
                        sqlCmd.Dispose()
                        sqlConn.Dispose()
                        sqlConn.Close()
    
                    End Using
    
                End Using
    
    
            Catch ex As Exception
                MsgBox(ex.Message, vbOK, "Err")
            Finally
    
            End Try
        End Sub

    I tried to fix this by putting a Message box on the Exception, but the problem is the messagebox also shows on the website, when I'm debbugging my project, Is there a best way to handle this error and also know the message of the error? 

    thanks and regards.

    • Moved by Alex-KSGZ Tuesday, April 16, 2019 7:30 AM
    Thursday, April 4, 2019 8:08 AM

All replies

  • Hi,

     Your issue is more related about asp.net,ask in the following forum:

    https://forums.asp.net/

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, April 4, 2019 8:46 AM
  • I tried to fix this by putting a Message box on the Exception, but the problem is the messagebox also shows on the website, when I'm debbugging my project, Is there a best way to handle this error and also know the message of the error? 

    An ASP.NET Web solution that is hosting the DLL doesn't know what a Windows form MsgBox() is about. So that is never going to work.

    What you need is Global Exception Handling that will catch any error the was thrown in the ASP.NET MVC or the DLL code that the ASP.NET MVC is referencing, logs the error and redirects to the Error.vbhtml that is in the Shared Views in the ASP.NET MVC part of the solution, which is implemented using a Basecontroller that all controllers inherit from the Basecontroller. 

    Secondly, what is the purpose of running the code you have that is basically doing nothing as far as returning data from the method? It should be a Function that returns data,  and it should not be a Sub.

    It's a little push that can be addressed in the MVC forum in ASP.NET forums.

    https://forums.asp.net/

    Thursday, April 4, 2019 11:00 AM
  • In classes it is never clever to use a messagebox, even if it is a Windows Forms or WPF project. 

    Make from the Sub a Function which returns the Exception.

    Set the Exception in your class as a public member and move the exception to it in your code

    Public Class Class1
        Public Function QueryExecute(SqlString As String, IsStoredProcedure As Boolean, GetParameters() As String, GetParameterValues() As String) As Exception
            Try
                Dim connStr As String = ConfigurationManager.ConnectionStrings(ConnectionName).ToString
                Using sqlConn As New SqlConnection(connStr)
                    Using sqlCmd As New SqlCommand(SqlString, sqlConn)
                        If IsStoredProcedure = True Then
                            sqlCmd.CommandType = CommandType.StoredProcedure
                        Else
                            sqlCmd.CommandType = CommandType.Text
                        End If
                        Dim i As Integer, getI As Integer
                        getI = GetParameters.Length
                        For i = 0 To getI - 1
                            sqlCmd.Parameters.AddWithValue(GetParameters(i), GetParameterValues(i))
                        Next
                        sqlConn.Open()
                        sqlCmd.ExecuteNonQuery()
                        sqlCmd.Dispose()
                        sqlConn.Dispose()
                        sqlConn.Close()
                    End Using
                End Using
            Catch ex As Exception
                Return ex
            End Try
        End Function
    End Class


    Success
    Cor

    Thursday, April 4, 2019 5:12 PM
  • I have not seen you make a post into the AP.NET MVC forum where I would help you, but I can help you in this forum if you like. What version of MVC are you using? Is it ASP.NET MVC5?
    Friday, April 5, 2019 12:41 AM