-
Apr 19th, 2006, 09:28 AM
#1
Thread Starter
Hyperactive Member
Try Catch and Disposing Objects
I am curious of why I cannot dispose the dataview (dv) as shown in this code block.
VB Code:
Private Sub FillMarketingPieceCombo()
Dim strSQL As String = _
"Select MarketingPiece From queMarketingPiece"
Dim con As New OleDbConnection(strCon)
Dim da As OleDbDataAdapter = _
New OleDbDataAdapter(strSQL, con)
Dim ds As DataSet = New DataSet
Try
da.Fill(ds, "MP")
Dim dv As DataView = ds.Tables("MP").DefaultView
cbMarketing.DataSource = dv
cbMarketing.DisplayMember = "MarketingPiece"
Catch ex As Exception
MessageBox.Show( _
ex.Message & vbNewLine & _
ex.Source, _
"Marketing Piece Error", _
MessageBoxButtons.OK, _
MessageBoxIcon.Error)
Finally
da.Dispose()
ds.Dispose()
dv.Dispose()
con.Dispose()
End Try
End Sub
I get the blue squiggly line under the dv.Dispose() with a message 'dv' is not declared. It is declared in the Try Catch Block and since I am using the Finally section of that TC Block why don't it recognize the declaration. Also does that mean my other Object disposals are being ignored since they are in the block and the declarations are outside of the block?
-
Apr 19th, 2006, 09:34 AM
#2
Fanatic Member
Re: Try Catch and Disposing Objects
dv is declared in the "try"-part of the code and is not know out this part of the code. Sou you can not access it in the "Finally". Change your code to the code below.
VB Code:
Private Sub FillMarketingPieceCombo()
Dim strSQL As String = _
"Select MarketingPiece From queMarketingPiece"
Dim con As New OleDbConnection(strCon)
Dim da As OleDbDataAdapter = _
New OleDbDataAdapter(strSQL, con)
Dim ds As DataSet = New DataSet
[COLOR=Red]Dim dv As DataView [/COLOR]
Try
da.Fill(ds, "MP")
[COLOR=Red]dv = ds.Tables("MP").DefaultView[/COLOR]
cbMarketing.DataSource = dv
cbMarketing.DisplayMember = "MarketingPiece"
Catch ex As Exception
MessageBox.Show( _
ex.Message & vbNewLine & _
ex.Source, _
"Marketing Piece Error", _
MessageBoxButtons.OK, _
MessageBoxIcon.Error)
Finally
da.Dispose()
ds.Dispose()
dv.Dispose()
con.Dispose()
End Try
End Sub
-
Apr 19th, 2006, 10:14 AM
#3
Thread Starter
Hyperactive Member
Re: Try Catch and Disposing Objects
Thank you. I did try that once before I posted but I got this error:
Variable 'dv' is used before it has been assigned a value. A null reference exception could result at runtime.
And my combobox comes up empty. I also tried putting the dv.Dispose at the end of the Try section but that cleared my list too.
Any other ideas. Or should I even worry about disposing this Object at all since this is a pretty small application and GC will eventually do it for me.
-
Apr 19th, 2006, 12:24 PM
#4
Fanatic Member
Re: Try Catch and Disposing Objects
because "dv = ds.Tables("MP").DefaultView" is in the "try"-block, VB isn't sure that this line will be executed. So it is possible that the "dv.Dispose()" line is executed without dv being initialised.
Try putting "dv.Dispose()" int the try-block.
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
|