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?
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?
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.
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.
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.
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?
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.
Re: Why this function doesn't report exception to the given status bar?
Quote:
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?
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!
Re: Why this function doesn't report exception to the given status bar?
My guess? is that you're breaking on all errors.
-tg
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.
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
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.
Re: Why this function doesn't report exception to the given status bar?
Quote:
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
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.
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