Why disposing a object cause an error ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Peter

    #1

    Why disposing a object cause an error ?

    Hi, everybody,

    Error message is "COM object that has been separated from its underlying RCW
    can not be used." When I called the codes below. So I traced the error and
    found it is caused by "oCmd.Despo se". BTW,these codes are included in a .net
    component.

    '-----------------------------------------------------------------
    Dim odr As OleDbDataReader
    Dim oCmd As New OleDbCommand

    If Conn.State = ConnectionState .Closed Then Conn.Open()
    With oCmd
    .Connection = Conn
    .CommandType = CommandType.Sto redProcedure
    .CommandText = "udpMySP"

    .Parameters.Add (New OleDbParameter( "", OleDbType.Integ er))
    .Parameters(0). Value = iID

    End With

    Try
    odr = oCmd.ExecuteRea der
    Do While odr.Read()
    'do something
    Loop

    Catch expOleDb As OleDbException
    'handle error

    Finally
    If Conn.State <ConnectionStat e.Closed Then Conn.Close()

    oCmd.Dispose()

    If Not odr.IsClosed Then odr.Close()
    End Try
    '------------------------------------------------------------------

    Thanks in advance,
    Peter


Working...