Link to home
Start Free TrialLog in
Avatar of ToolTimeGang
ToolTimeGangFlag for United States of America

asked on

Dispose warnings on SqlAdapter code

I am rewriting a .NET 1.1 app to 4.0.  This code compiles just fine, but I receive numerous warnings  like:
"call System.IDispoable.Dispose on object 'sqlCmd' before all references to it are out of scope".
I also get that warning for da and results.
Is it really important that I clean that up?  And how would I "dispose" of these?  I thought the garbage collector does that for me.  
Thank you for helping!
    Public Function SelectTsql(ByVal qryString As String) As DataTable
        Dim sqlCmd As SqlCommand

        Dim da As SqlDataAdapter
        Dim results As DataTable

        sqlCmd = New SqlCommand

        With sqlCmd
            '.Transaction = transaction
            .CommandTimeout = 240
            .CommandText = qryString
            If _sqlInSrvCnn1.State = ConnectionState.Open Then
                .Connection = _sqlInSrvCnn1
            Else
                .Connection = _sqlInSrvCnn2
            End If
            .CommandType = CommandType.Text
            da = New SqlDataAdapter(sqlCmd)
            results = New DataTable
            da.Fill(results)
        End With

        If results.Rows.Count > 0 Then
            Return results
        Else
            Return Nothing
        End If

    End Function

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ToolTimeGang

ASKER

Thank you!