-1

When I run a Code Analysis against the following code:

Protected Function GetOrderEntry() As IList(Of OE)
    Dim results As IList(Of OE) = New List(Of OE)()
    Using connection As IDbConnection = DbProviderFactories.GetFactory("System.Data.SqlClient").CreateConnection()
        connection.ConnectionString = ConfigurationManager.AppSettings("OrderEnterConnection")
        Using command As IDbCommand = connection.CreateCommand()
            command.CommandTimeout = 120
            command.CommandText = "Exec up_ViewOrderDetail_2012_Order '" & MemberNo1.Value & "','" & CustNo.Value & "','DEFAULT','" & OrderNo.Value & "','" & DeliveryDate.SelectedDate & "','','','','1'"
            connection.Open()
            Try
                Dim reader As IDataReader = command.ExecuteReader()
                results = PopulateGrid(reader)
            Catch ex As SqlException
                results.Clear()
                connection.Close()
            End Try
        End Using
    End Using
    Return results
End Function

...I get,

"CA2202 Do not dispose objects multiple times Object 'connection' can be disposed more than once in method 'OrderConfirm.GetOrderEntry()'. To avoid generating a System.ObjectDisposedException you should not call Dispose more than one time on an object"

The cursor is on the final "End Using" line; howis this seen as a double-disposal of objects? Wouldn't both of the "Using" blocks need to be terminated in this way?

Hadi
  • 36,233
  • 13
  • 65
  • 124
B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
  • 3
    http://stackoverflow.com/q/14706523/1070452 There is no need to close the connection, disposing of it does that. That reader on the other hand, should also be disposed (well, they are IDisposable, anyway) – Ňɏssa Pøngjǣrdenlarp Dec 14 '16 at 21:22

1 Answers1

0

I tried removing connection.createcommand And connection.Close(). And it worked fine

Protected Function GetOrderEntry() As IList(Of OE)
    Dim results As IList(Of OE) = New List(Of OE)()
    Using connection As IDbConnection = DbProviderFactories.GetFactory("System.Data.SqlClient").CreateConnection()
        connection.ConnectionString = ConfigurationManager.AppSettings("OrderEnterConnection")
        Using command As New IDbCommand 
            Command.connection = connection
            command.CommandTimeout = 120
            command.CommandText = "Exec up_ViewOrderDetail_2012_Order '" & MemberNo1.Value & "','" & CustNo.Value & "','DEFAULT','" & OrderNo.Value & "','" & DeliveryDate.SelectedDate & "','','','','1'"
            connection.Open()
            Try
                Dim reader As IDataReader = command.ExecuteReader()
                results = PopulateGrid(reader)
               Reader.close()
            Catch ex As SqlException
                results.Clear()

            End Try
        End Using
    End Using
    Return results
End Function
Hadi
  • 36,233
  • 13
  • 65
  • 124
  • 1
    I don't think that `Using command As New IDbCommand` will even compile, which makes the green check mark curious! ;-) Presumably `connection.CreateCommand()` was being used to make the code provider agnostic, so what was used instead? And was it actually removing the call to `connection.Close()` that got rid of the message, because according to the docs "Close and Dispose are functionally equivalent"? – Mark Dec 14 '16 at 22:41
  • The bit that confuses me even more so is that `IDisposable` is baked into the compiler, but I don't believe the compiler gives two diddly's about the `IDbConnection.Close` method, so removing that shouldn't make any difference... `using` calls `Dispose` on `IDisposable` - it doesn't care about other interfaces. – Charleh Dec 14 '16 at 22:55
  • @Charleh The CA* messages come from code analysis, not the compiler, so I suppose they can be more thorough in what they check. I should have checked [the docs](https://msdn.microsoft.com/en-us/library/ms182334.aspx) first, since they state "A method implementation contains code paths that could cause multiple calls to IDisposable.Dispose or a Dispose equivalent, such as a Close() method on some types, on the same object." - so I think it was the `connection.Close()` call in addition to the `Using` that caused the warning. – Mark Dec 14 '16 at 23:14
  • Ah well that explains that, I guess the CA is being pedantic! – Charleh Dec 15 '16 at 21:31