Why disposing a object cause an error ?

P

Peter

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.Despose". 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.StoredProcedure
.CommandText = "udpMySP"

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

End With

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

Catch expOleDb As OleDbException
'handle error

Finally
If Conn.State <> ConnectionState.Closed Then Conn.Close()

oCmd.Dispose()

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

Thanks in advance,
Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top