Results 1 to 16 of 16

Thread: Why this function doesn't report exception to the given status bar?

  1. #1

    Thread Starter
    Addicted Member kutlesh's Avatar
    Join Date
    Jun 2018
    Location
    Skopje, Macedonia
    Posts
    211

    Why this function doesn't report exception to the given status bar?

    I am making my own utils project with static functions to use. I have this utility function:

    Code:
    Public Sub OpenRecordSetForwardReadOnly(ByRef recordSet As ADODB.Recordset, ByVal sql As String, ByRef dbConn As ADODB.Connection, Optional ByVal statusBar As ToolStripStatusLabel = Nothing)
            Try
                recordSet.Open(sql, dbConn, ADODB.CursorTypeEnum.adOpenForwardOnly, ADODB.LockTypeEnum.adLockReadOnly, ADODB.CommandTypeEnum.adCmdText)
            Catch ex As Exception
                Dim funcName As String = System.Reflection.MethodInfo.GetCurrentMethod().Name
                Dim msg As String = funcName & ": " & ex.Message & ", Source: " & ex.Source & ", StackTrace: " & ex.StackTrace
    
                Console.WriteLine(msg)
    
                If Not IsNothing(statusBar) Then
                    statusBar.Text = msg
                End If
            End Try
        End Sub
    which of course seems fine, but it doesn't report the error message to the given ToolStripStatusLabel component when I call it like so:

    Code:
    Private Sub checkExistingDataBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles izlezBtn.Click
         Dim rs as new ADODB.RecordSet
         myUtils.OpenRecordSetForwardReadOnly(rs, someSelectSQLString, db2Conn, Me.statusbarTB)
    End Sub
    Instead it breaks the application with exception that the operation is not allowed when the object is closed(accessing table that doesn't exist with the given name.)

    Well I can add Try/Catch block

    Code:
    Private Sub checkExistingDataBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles izlezBtn.Click
    try
         Dim rs as new ADODB.RecordSet
         myUtils.OpenRecordSetForwardReadOnly(rs, someSelectSQLString, db2Conn, Me.statusbarTB)
    Catch ex As Exception
         Dim funcName As String = System.Reflection.MethodInfo.GetCurrentMethod().Name
         Dim msg As String = funcName & ": " & ex.Message & ", Source: " & ex.Source & ", StackTrace: " & ex.StackTrace
    
         Console.WriteLine(msg)
         statusBar.Text = msg
    end Try
    End Sub
    but that is not the point of the utils function since it has its own Try/Catch block.

    How can I report exception message to status bar without adding additional Try/Catch block?

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,598

    Re: Why this function doesn't report exception to the given status bar?

    Might be a dumb question, but should it be passed byRef rather than byVal?

  3. #3

    Thread Starter
    Addicted Member kutlesh's Avatar
    Join Date
    Jun 2018
    Location
    Skopje, Macedonia
    Posts
    211

    Re: Why this function doesn't report exception to the given status bar?

    Hmm, tried with ByRef. It doesn't make a difference.

    Well it seems that the ToolStripStatusLabel.Text gets set with the error message. The thing is I don't see the text afterwards.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Why this function doesn't report exception to the given status bar?

    On the subject of ByRef and ByVal, there's no justification for any of your parameters being ByRef. If you think there is, you don;t know what ByRef is actually for so you should do some reading on the subject.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Why this function doesn't report exception to the given status bar?

    I have to say, you really shouldn't be writing utility functions using ADODB anyway. That code looks to have a heavy VB6 influence, with the Recordset and the ByRefs used as they would be in VB6. You really ought to learn VB.NET as VB.NET, which means learning ADO.NET, or even Entity Framework.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Why this function doesn't report exception to the given status bar?

    As for your issue, if you step through the code in that OpenRecordSetForwardReadOnly, where does it actually break? Are you saying that it's on the recordSet.Open call or somewhere else? If on that call, can you still continue on from that point?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Addicted Member kutlesh's Avatar
    Join Date
    Jun 2018
    Location
    Skopje, Macedonia
    Posts
    211

    Re: Why this function doesn't report exception to the given status bar?

    ADO.NET is for some of the future versions of the application. Even switching to C#. But ADO.NET is used with C# as well.

    The code breaks after i call

    Code:
    if recordSetObject.EOF then
    ' do something
    end if
    But I solved this with checking state of the RecordSet

    Code:
    if recordSetObject.State = 1 then
         if recordSetObject.EOF then
              ' do something
         end if
    end if
    So it jumps over the exception. But still cannot figure out why the ToolStripStatusLabel component doesn't display the text although when I debug, the Text field shows the exception contents.
    Maybe because I send it as a parameter of a function? Needs some sort of GUI refresh?

    Note: I am writing utility functions since there are couple of conditions that has to be met. One of those is to check if certain code exists in a database table.
    But new tables are created each year so the application might fail if the necessary table doesn't exist. It is a requirement to have new table each year for the same data types.
    Last edited by kutlesh; May 8th, 2019 at 06:00 AM.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Why this function doesn't report exception to the given status bar?

    Quote Originally Posted by kutlesh View Post
    The code breaks after i call

    Code:
    if recordSetObject.EOF then
    ' do something
    end if
    So, nothing to do with the code you actually posted?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Addicted Member kutlesh's Avatar
    Join Date
    Jun 2018
    Location
    Skopje, Macedonia
    Posts
    211

    Re: Why this function doesn't report exception to the given status bar?

    Why do I don't see the text in the status bar? Damn!

  10. #10
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Why this function doesn't report exception to the given status bar?

    My guess? is that you're breaking on all errors.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  11. #11

    Thread Starter
    Addicted Member kutlesh's Avatar
    Join Date
    Jun 2018
    Location
    Skopje, Macedonia
    Posts
    211

    Re: Why this function doesn't report exception to the given status bar?

    No, i removed the breakpoints, but still no text in the status bar label

    I am missing something.

  12. #12
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Why this function doesn't report exception to the given status bar?

    I didn't say anything about breakpoints. I said you have break on all errors turned on. I think it's Ctrl+Alt+E ... brings up a dialog window where you can select the conditions under which the code execution will stop.. and one of . those is when there's an exception in your code... even if it's handled...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  13. #13
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Why this function doesn't report exception to the given status bar?

    But if we ARE talking about breakpoints, what happens when you put a breakpoint on this line:

    statusBar.Text = msg

    If the breakpoint is hit, that would be interesting, but I'm betting that it is not, in which case, the question you asked has been answered...and another question presents itself.
    My usual boring signature: Nothing

  14. #14
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,129

    Re: Why this function doesn't report exception to the given status bar?

    Quote Originally Posted by kutlesh View Post
    I am making my own utils project with static functions to use. I have this utility function:

    Code:
    Public Sub OpenRecordSetForwardReadOnly(ByRef recordSet As ADODB.Recordset, ByVal sql As String, ByRef dbConn As ADODB.Connection, Optional ByVal statusBar As ToolStripStatusLabel = Nothing)
            Try
                recordSet.Open(sql, dbConn, ADODB.CursorTypeEnum.adOpenForwardOnly, ADODB.LockTypeEnum.adLockReadOnly, ADODB.CommandTypeEnum.adCmdText)
            Catch ex As Exception
                Dim funcName As String = System.Reflection.MethodInfo.GetCurrentMethod().Name
                Dim msg As String = funcName & ": " & ex.Message & ", Source: " & ex.Source & ", StackTrace: " & ex.StackTrace
    
                Console.WriteLine(msg)
    
                If Not IsNothing(statusBar) Then
                    statusBar.Text = msg
                End If
            End Try
        End Sub
    which of course seems fine, but it doesn't report the error message to the given ToolStripStatusLabel component when I call it like so:
    don't mean to be rude ..but
    you should really try and use ADO.Net

    loads of people will help here

    EDIT:
    most of the Error Messages won't fit in the ToolStripStatusLabel anyway
    you should create a ErrorForm with a multiline textbox or something like that
    Last edited by ChrisE; May 8th, 2019 at 10:37 AM.
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  15. #15

    Thread Starter
    Addicted Member kutlesh's Avatar
    Join Date
    Jun 2018
    Location
    Skopje, Macedonia
    Posts
    211

    Re: Why this function doesn't report exception to the given status bar?

    ADO.NET and maybe C# in future versions of the application.
    As for the status bar, I truly didn't think of the message being sent to ToolStripStatusLabel component can be too long so it doesn't fit and doesn't get displayed.
    Will check that.

  16. #16
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,129

    Re: Why this function doesn't report exception to the given status bar?

    Hi

    as for you Function the Cursor Location is missing

    should be like this

    Code:
     With DBRecordset
           .ActiveConnection = DBConnection
           .CursorLocation = ADODB.CursorLocationEnum.adUseClient
           .LockType = ADODB.LockTypeEnum.adLockOptimistic
            .Open("SELECT A,B From yourTable")
        End With
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width