Results 1 to 4 of 4

Thread: Try Catch and Disposing Objects

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Try Catch and Disposing Objects

    I am curious of why I cannot dispose the dataview (dv) as shown in this code block.
    VB Code:
    1. Private Sub FillMarketingPieceCombo()
    2.         Dim strSQL As String = _
    3.             "Select MarketingPiece From queMarketingPiece"
    4.         Dim con As New OleDbConnection(strCon)
    5.         Dim da As OleDbDataAdapter = _
    6.             New OleDbDataAdapter(strSQL, con)
    7.         Dim ds As DataSet = New DataSet
    8.         Try
    9.             da.Fill(ds, "MP")
    10.             Dim dv As DataView = ds.Tables("MP").DefaultView
    11.             cbMarketing.DataSource = dv
    12.             cbMarketing.DisplayMember = "MarketingPiece"
    13.  
    14.         Catch ex As Exception
    15.             MessageBox.Show( _
    16.                 ex.Message & vbNewLine & _
    17.                 ex.Source, _
    18.                 "Marketing Piece Error", _
    19.                 MessageBoxButtons.OK, _
    20.                 MessageBoxIcon.Error)
    21.         Finally
    22.             da.Dispose()
    23.             ds.Dispose()
    24.             dv.Dispose()
    25.             con.Dispose()
    26.         End Try
    27.     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?

  2. #2
    Fanatic Member robbedaya's Avatar
    Join Date
    Jul 2002
    Location
    Belgium
    Posts
    872

    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:
    1. Private Sub FillMarketingPieceCombo()
    2.         Dim strSQL As String = _
    3.             "Select MarketingPiece From queMarketingPiece"
    4.         Dim con As New OleDbConnection(strCon)
    5.         Dim da As OleDbDataAdapter = _
    6.             New OleDbDataAdapter(strSQL, con)
    7.         Dim ds As DataSet = New DataSet
    8.         [COLOR=Red]Dim dv As DataView [/COLOR]
    9.         Try
    10.             da.Fill(ds, "MP")
    11.             [COLOR=Red]dv = ds.Tables("MP").DefaultView[/COLOR]
    12.             cbMarketing.DataSource = dv
    13.             cbMarketing.DisplayMember = "MarketingPiece"
    14.  
    15.         Catch ex As Exception
    16.             MessageBox.Show( _
    17.                 ex.Message & vbNewLine & _
    18.                 ex.Source, _
    19.                 "Marketing Piece Error", _
    20.                 MessageBoxButtons.OK, _
    21.                 MessageBoxIcon.Error)
    22.         Finally
    23.             da.Dispose()
    24.             ds.Dispose()
    25.             dv.Dispose()
    26.             con.Dispose()
    27.         End Try
    28.     End Sub

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    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.

  4. #4
    Fanatic Member robbedaya's Avatar
    Join Date
    Jul 2002
    Location
    Belgium
    Posts
    872

    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
  •  



Click Here to Expand Forum to Full Width