What does mean this error

Nader

Well-known member
Joined
Oct 9, 2008
Messages
55
Programming Experience
Beginner
"COM object that has been separated from its underlying RCW cannot be used."
 
An RCW is a "runtime callable wrapper". Your code can't talk to a COM component directly. The Framework wraps COM components in managed code that provides the link between your managed code and the COM component's unmanaged code. If the COM component has been disconnected from that wrapper then you can't use it because you can't communicate with it. Most likely you've disposed a form containing an ActiveX control or something similar.
 
The Error occurs in this code when I put the Public cmd AsNew OleDbCommand for both "Add" and "Delete" sub
- When I Click the Addbutton it works well and then Click Deletebutton it casue the error, but if I put the dim cmd As New OleDbCommand to each sub alone it works well.
VB.NET:
  [SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2] conn [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] OleDbConnection([/SIZE][SIZE=2][COLOR=#800000]"Provider=Microsoft.Jet.OLEDB.4.0;"[/COLOR][/SIZE][SIZE=2] _[/SIZE]
[SIZE=2]& [/SIZE][SIZE=2][COLOR=#800000]"Data Source ="[/COLOR][/SIZE][SIZE=2] & Application.StartupPath & [/SIZE][SIZE=2][COLOR=#800000]"\Store.mdb "[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2] cmd [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] OleDbCommand[/SIZE]
 
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] cmdAdd_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] Button1.Click[/SIZE]
[SIZE=2]ListBox1.Items.Clear()[/SIZE]
[SIZE=2]cmd.Connection = conn[/SIZE]
[SIZE=2][COLOR=#008000]'Add text to DB-TBinMaterial[/COLOR][/SIZE]
[SIZE=2]cmd.CommandText = [/SIZE][SIZE=2][COLOR=#800000]"INSERT INTO TBinMaterial(inMaterial) "[/COLOR][/SIZE][SIZE=2] & [/SIZE][SIZE=2][COLOR=#800000]"values ('"[/COLOR][/SIZE][SIZE=2] & txtAddM.Text & [/SIZE][SIZE=2][COLOR=#800000]"') "[/COLOR][/SIZE]
[SIZE=2]cmd.CommandType = CommandType.Text[/SIZE]
[SIZE=2]conn.Open()[/SIZE]
[SIZE=2]cmd.ExecuteNonQuery()[/SIZE]
[SIZE=2][COLOR=#008000]'Load the datat from DB-inMaterial to ListBox1[/COLOR][/SIZE]
[SIZE=2]cmd.CommandText = [/SIZE][SIZE=2][COLOR=#800000]"Select * from TBinMaterial "[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] dr [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] OleDbDataReader = cmd.ExecuteReader[/SIZE]
[SIZE=2][COLOR=#0000ff]Do[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]While[/COLOR][/SIZE][SIZE=2] dr.Read()[/SIZE]
[SIZE=2]ListBox1.Items.Add(dr.Item([/SIZE][SIZE=2][COLOR=#800000]"inMaterial"[/COLOR][/SIZE][SIZE=2]))[/SIZE]
[SIZE=2][COLOR=#0000ff]Loop[/COLOR][/SIZE]
[SIZE=2]conn.Close()[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] cmdDelete_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] Button2.Click[/SIZE]
[SIZE=2][COLOR=#008000]'Delete one data from DB, and load the remainder data[/COLOR][/SIZE]
[SIZE=2]cmd.Connection = conn[/SIZE]
[SIZE=2][COLOR=#008000]'Delete the data according the text of listbox1[/COLOR][/SIZE]
[SIZE=2]cmd.CommandText = [/SIZE][SIZE=2][COLOR=#800000]"DELETE From TBinMaterial where inMaterial = @inMaterial "[/COLOR][/SIZE]
[SIZE=2]cmd.Parameters.AddWithValue([/SIZE][SIZE=2][COLOR=#800000]"@inMaterial"[/COLOR][/SIZE][SIZE=2], ListBox1.Text)[/SIZE]
[SIZE=2]conn.Open()[/SIZE]
[SIZE=2]cmd.ExecuteNonQuery()[/SIZE]
[SIZE=2]ListBox1.Items.Clear()[/SIZE]
[SIZE=2][COLOR=#008000]'Load the data from DB to ListBox1[/COLOR][/SIZE]
[SIZE=2]cmd.CommandText = [/SIZE][SIZE=2][COLOR=#800000]"Select * from TBinMaterial "[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] dr [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] OleDbDataReader = cmd.ExecuteReader[/SIZE]
[SIZE=2][COLOR=#0000ff]Do[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]While[/COLOR][/SIZE][SIZE=2] dr.Read()[/SIZE]
[SIZE=2]ListBox1.Items.Add(dr.Item([/SIZE][SIZE=2][COLOR=#800000]"inMaterial"[/COLOR][/SIZE][SIZE=2]))[/SIZE]
[SIZE=2][COLOR=#0000ff]Loop[/COLOR][/SIZE]
[SIZE=2]conn.Close()[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]

 

Latest posts

Back
Top