|
-
May 8th, 2019, 02:43 AM
#1
Thread Starter
Addicted Member
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?
-
May 8th, 2019, 03:15 AM
#2
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?
-
May 8th, 2019, 03:38 AM
#3
Thread Starter
Addicted Member
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.
-
May 8th, 2019, 05:39 AM
#4
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.
-
May 8th, 2019, 05:41 AM
#5
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.
-
May 8th, 2019, 05:45 AM
#6
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?
-
May 8th, 2019, 05:55 AM
#7
Thread Starter
Addicted Member
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.
-
May 8th, 2019, 06:38 AM
#8
Re: Why this function doesn't report exception to the given status bar?
 Originally Posted by kutlesh
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?
-
May 8th, 2019, 07:29 AM
#9
Thread Starter
Addicted Member
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!
-
May 8th, 2019, 08:10 AM
#10
Re: Why this function doesn't report exception to the given status bar?
My guess? is that you're breaking on all errors.
-tg
-
May 8th, 2019, 09:15 AM
#11
Thread Starter
Addicted Member
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.
-
May 8th, 2019, 09:31 AM
#12
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
-
May 8th, 2019, 09:52 AM
#13
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
 
-
May 8th, 2019, 10:28 AM
#14
Re: Why this function doesn't report exception to the given status bar?
 Originally Posted by kutlesh
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.
-
May 10th, 2019, 03:04 AM
#15
Thread Starter
Addicted Member
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.
-
May 10th, 2019, 03:36 AM
#16
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|