Question COM object that has been separated from its underlying RCW cannot be used

Carcus_D

New member
Joined
Dec 11, 2015
Messages
2
Programming Experience
Beginner
Could someone help me figure this out?

"COM object that has been separated from its underlying RCW cannot be used" is the Error Message I'm getting on my Windows Form App.


Here is my code. Always seems to be after I use the Update Button


C#:
//frmEmpMaint.
public partial class frmEmpMaint : Form
    {
        TheConnection connection;
        public frmEmpMaint()
        {
            InitializeComponent();
            connection = new TheConnection();


            lvEmpData.View = View.Details;
            lvEmpData.FullRowSelect = true;
            
            lvEmpData.Items.Clear();
            List<Employee> employee = connection.Load();
            foreach (Employee Employee in employee)
            {
                ListViewItem item = new ListViewItem(Employee.EmpFullName);
                item.SubItems.Add(Employee.EmpShortName.ToString());
                item.SubItems.Add(Employee.EmpDateOfBirth.ToString());
                item.SubItems.Add(Employee.EmpType.ToString());
                item.SubItems.Add(Employee.EmpActive.ToString());
                item.SubItems.Add(Employee.EmpTitle.ToString());
                item.SubItems.Add(Employee.EmpOfficePhone.ToString());
                item.SubItems.Add(Employee.EmpHomePhone.ToString());
                item.SubItems.Add(Employee.EmpMobilePhone.ToString());
                item.SubItems.Add(Employee.EmpEmergContact.ToString());
                item.SubItems.Add(Employee.EmpEmergPhone.ToString());
                lvEmpData.Items.Add(item);
            }
 
        }


        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }


        private void btnSave_Click(object sender, EventArgs e)
        {
            Employee employee = new Employee();
            employee.EmpFullName = tbxFullName.Text;
            employee.EmpShortName = tbxShortName.Text;
            employee.EmpDateOfBirth = Convert.ToDateTime(tbxDob.Text);
            employee.EmpType = cmbType.Text;
            employee.EmpActive = cmbActive.Text;
            employee.EmpTitle = cmbTitle.Text;
            employee.EmpOfficePhone = cmbOfficePhone.Text;
            employee.EmpHomePhone = tbxHomePhone.Text;
            employee.EmpMobilePhone = tbxMobilePhone.Text;
            employee.EmpEmergContact = tbxEmergName.Text;
            employee.EmpEmergPhone = tbxEmergPhone.Text;
            connection.Insert(employee);
         }


        private void btnRefresh_Click(object sender, EventArgs e)
        {
            lvEmpData.Items.Clear();
            List<Employee> employee = connection.Load();
            foreach(Employee Employee in employee)
            {
                
                ListViewItem item = new ListViewItem(Employee.EmpFullName);
                item.SubItems.Add(Employee.EmpShortName.ToString());
                item.SubItems.Add(Employee.EmpDateOfBirth.ToString());
                item.SubItems.Add(Employee.EmpType.ToString());
                item.SubItems.Add(Employee.EmpActive.ToString());
                item.SubItems.Add(Employee.EmpTitle.ToString());
                item.SubItems.Add(Employee.EmpOfficePhone.ToString());
                item.SubItems.Add(Employee.EmpHomePhone.ToString());
                item.SubItems.Add(Employee.EmpMobilePhone.ToString());
                item.SubItems.Add(Employee.EmpEmergContact.ToString());
                item.SubItems.Add(Employee.EmpEmergPhone.ToString());
                lvEmpData.Items.Add(item);
            }
        }


        private void lvEmpData_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lvEmpData.SelectedItems.Count != 0)
            {


                btnSave.Enabled = false;
            }
            else
            {
                btnSave.Enabled = true;
            }
        }


        private void btnUpdate_Click(object sender, EventArgs e)
        {
            Employee updateEmployee = new Employee();
            
            updateEmployee.EmpFullName = tbxFullName.Text;
            updateEmployee.EmpShortName = tbxShortName.Text;
            updateEmployee.EmpTitle = cmbTitle.Text;
            updateEmployee.EmpOfficePhone = cmbOfficePhone.Text;
            updateEmployee.EmpHomePhone = tbxHomePhone.Text;
            updateEmployee.EmpMobilePhone = tbxMobilePhone.Text;
            updateEmployee.EmpDateOfBirth = Convert.ToDateTime(tbxDob.Text);
            updateEmployee.EmpType = cmbType.Text;
            updateEmployee.EmpActive = cmbActive.Text;
            updateEmployee.EmpEmergPhone = tbxEmergPhone.Text;
            updateEmployee.EmpEmergContact = tbxEmergName.Text;
            int index = lvEmpData.SelectedItems[0].Index;
            List<Employee> employeeUpdate = connection.Load();
            updateEmployee.EmpRecordID = employeeUpdate[index].EmpRecordID;
            connection.Update(updateEmployee);
            
        }


        private void lvEmpData_MouseClick(object sender, MouseEventArgs e)
        {
            String cFullName = lvEmpData.SelectedItems[0].SubItems[0].Text;
            String cShortName = lvEmpData.SelectedItems[0].SubItems[1].Text;
            String cDOB = lvEmpData.SelectedItems[0].SubItems[2].Text;
            String cType = lvEmpData.SelectedItems[0].SubItems[3].Text;
            String cActive = lvEmpData.SelectedItems[0].SubItems[4].Text;
            String cTitle = lvEmpData.SelectedItems[0].SubItems[5].Text;
            String cOfficePhone = lvEmpData.SelectedItems[0].SubItems[6].Text;
            String cHomePhone = lvEmpData.SelectedItems[0].SubItems[7].Text;
            String cMobilePhone = lvEmpData.SelectedItems[0].SubItems[8].Text;
            String cEmergContact = lvEmpData.SelectedItems[0].SubItems[9].Text;
            String cEmergPhone = lvEmpData.SelectedItems[0].SubItems[10].Text;
            tbxFullName.Text = cFullName;
            tbxShortName.Text = cShortName;
            tbxDob.Text = cDOB;
            cmbType.Text = cType;
            cmbActive.Text = cActive;
            cmbTitle.Text = cTitle;
            cmbOfficePhone.Text = cOfficePhone;
            tbxHomePhone.Text = cHomePhone;
            tbxMobilePhone.Text = cMobilePhone;
            tbxEmergName.Text = cEmergContact;
            tbxEmergPhone.Text = cEmergPhone;
        }
C#:
//The Connection Class

class TheConnection
    {
        OleDbConnection connection;
        OleDbCommand command;


        public TheConnection()
        {
            connection = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0; ");
            command = connection.CreateCommand();
        }
        public void Insert(Employee employee)
        {
            try
            {
                command.CommandText = "INSERT INTO tblEmployee (empFullName, empShortName, empTitle, empOfficePhone, empHomePhone, empMobilePhone, empDateOfBirth, empType, empActive, empEmergPhone, empEmergContact) VALUES(@fullName, @shortName, @title, @officePhone, @homePhone, @mobilePhone, @dob, @type, @active, @emergPhone, @emergContact)";
                command.Parameters.AddWithValue("@fullName", employee.EmpFullName);
                command.Parameters.AddWithValue("@shortName", employee.EmpShortName);
                command.Parameters.AddWithValue("@title", employee.EmpTitle);
                command.Parameters.AddWithValue("@officePhone", employee.EmpOfficePhone);
                command.Parameters.AddWithValue("@homePhone", employee.EmpHomePhone);
                command.Parameters.AddWithValue("@mobilePhone", employee.EmpMobilePhone);
                command.Parameters.AddWithValue("@dob", employee.EmpDateOfBirth);
                command.Parameters.AddWithValue("@type", employee.EmpType);
                command.Parameters.AddWithValue("@active", employee.EmpActive);
                command.Parameters.AddWithValue("@emergPhone", employee.EmpEmergPhone);
                command.Parameters.AddWithValue("@emergContact", employee.EmpEmergContact);
                command.CommandType = System.Data.CommandType.Text;
                connection.Open();
                command.ExecuteNonQuery();
                System.Windows.Forms.MessageBox.Show("Employee Added Successfully!");
            }
            catch (Exception exceptionInsert)
            {
                System.Windows.Forms.MessageBox.Show(exceptionInsert.Message);
            }
            finally
            {
                connection.Close();
            }
        }
        public List<Employee> Load()
        {
            List<Employee> employees = new List<Employee>();
            employees.Clear();
            try
            {
                
                command.CommandText = "SELECT * FROM tblEmployee";
                command.CommandType = System.Data.CommandType.Text;
                connection.Open();
                OleDbDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    Employee employee = new Employee();
                    employee.EmpRecordID = Convert.ToInt32(reader["empRecordID"].ToString());
                    employee.EmpFullName = reader["empFullName"].ToString();
                    employee.EmpShortName = reader["empShortName"].ToString();
                    employee.EmpTitle = reader["empTitle"].ToString();
                    employee.EmpOfficePhone = reader["empOfficePhone"].ToString();
                    employee.EmpHomePhone = reader["empHomePhone"].ToString();
                    employee.EmpMobilePhone = reader["empMobilePhone"].ToString();
                    employee.EmpDateOfBirth = Convert.ToDateTime(reader["empDateOfBirth"].ToString());
                    employee.EmpType = reader["empType"].ToString();
                    employee.EmpActive = reader["empActive"].ToString();
                    employee.EmpEmergPhone = reader["empEmergPhone"].ToString();
                    employee.EmpEmergContact = reader["empEmergContact"].ToString();
                    employees.Add(employee);


                }
            }
            catch(Exception empList)
            {
                System.Windows.Forms.MessageBox.Show(empList.Message);
            }
            finally
            {
                connection.Close();
            }
            return employees;
        }
        public void Update(Employee updateEmployee)
        {
            try
            {
                //CALLS THE UPDATE QUERY WITH FIELD NAME = "@" VARIABLE.
               command.CommandText = "UPDATE tblEmployee SET empFullName=@empFullName, empShortName=@empShortName, empTitle=@empTitle, empOfficePhone=@empOfficePhone, empHomePhone=@empHomePhone, empMobilePhone=@empMobilePhone, empDateOfBirth=@empDateOfBirth, empType=@empType, empActive=@empActive, empEmergPhone=@empEmergPhone, empEmergContact=@empEmergContact WHERE empRecordID=@empRecordID";
                command.Parameters.AddWithValue("@empFullName", updateEmployee.EmpFullName);
                command.Parameters.AddWithValue("@empShortName", updateEmployee.EmpShortName);
                command.Parameters.AddWithValue("@empTitle", updateEmployee.EmpTitle);
                command.Parameters.AddWithValue("@empOfficePhone", updateEmployee.EmpOfficePhone);
                command.Parameters.AddWithValue("@empHomePhone", updateEmployee.EmpHomePhone);
                command.Parameters.AddWithValue("@empMobilePhone", updateEmployee.EmpMobilePhone);
                command.Parameters.AddWithValue("@empDateOfBirth", updateEmployee.EmpDateOfBirth);
                command.Parameters.AddWithValue("@empType", updateEmployee.EmpType);
                command.Parameters.AddWithValue("@empActive", updateEmployee.EmpActive);
                command.Parameters.AddWithValue("@empEmergPhone", updateEmployee.EmpEmergPhone);
                command.Parameters.AddWithValue("@empEmergContact", updateEmployee.EmpEmergContact);
                command.Parameters.AddWithValue("@empRecordID", updateEmployee.EmpRecordID);
                command.CommandType = System.Data.CommandType.Text;
                connection.Open();
                command.ExecuteNonQuery();
                System.Windows.Forms.MessageBox.Show("Employee Record Updated Successfully!");
            }
            catch(Exception updateEmp)
            {
                System.Windows.Forms.MessageBox.Show(updateEmp.Message);
            }
            finally
            {
                connection.Close();
            }
        }
C#:
//Employee Class

class Employee
    {
        int empRecordID;
        
        public int EmpRecordID
        {
            get { return empRecordID; }
            set { empRecordID = value; }
        }
        string empFullName;


        public string EmpFullName
        {
            get { return empFullName; }
            set { empFullName = value; }
        }
        string empShortName;


        public string EmpShortName
        {
            get { return empShortName; }
            set { empShortName = value; }
        }
        string empTitle;


        public string EmpTitle
        {
            get { return empTitle; }
            set { empTitle = value; }
        }
        string empOfficePhone;


        public string EmpOfficePhone
        {
            get { return empOfficePhone; }
            set { empOfficePhone = value; }
        }
        string empHomePhone;


        public string EmpHomePhone
        {
            get { return empHomePhone; }
            set { empHomePhone = value; }
        }
        string empMobilePhone;


        public string EmpMobilePhone
        {
            get { return empMobilePhone; }
            set { empMobilePhone = value; }
        }
        DateTime empDateOfBirth;


        public DateTime EmpDateOfBirth
        {
            get { return empDateOfBirth; }
            set { empDateOfBirth = value; }
        }
        string empType;


        public string EmpType
        {
            get { return empType; }
            set { empType = value; }
        }
        string empActive;


        public string EmpActive
        {
            get { return empActive; }
            set { empActive = value; }
        }
        string empEmergPhone;


        public string EmpEmergPhone
        {
            get { return empEmergPhone; }
            set { empEmergPhone = value; }
        }
        string empEmergContact;


        public string EmpEmergContact
        {
            get { return empEmergContact; }
            set { empEmergContact = value; }
        }
    }
}
 
There's an awful lot of code there and I'm not much interested in wading through to find out things that you may well already know. Please provide ALL the information from the exception. It will tell you exactly where it is thrown and provide a call stack and more. That information is provided to you for diagnostic purposes so, if you want us to diagnose the issue, you should provide it to us. Also, do you know what COM object it's referring to? Are you using any ActiveX controls or other COM components, i.e. non-.NET components?
 
There's an awful lot of code there and I'm not much interested in wading through to find out things that you may well already know. Please provide ALL the information from the exception. It will tell you exactly where it is thrown and provide a call stack and more. That information is provided to you for diagnostic purposes so, if you want us to diagnose the issue, you should provide it to us. Also, do you know what COM object it's referring to? Are you using any ActiveX controls or other COM components, i.e. non-.NET components?

[h=1]"COM object that has been separated from its underlying RCW cannot be used"[/h]That is the exception that I get when it is thrown. I am a complete newbie at programming, I'm not using any ActiveX controls and the only COM components that I know that I'm using the OLEDB Connection. I did load this code into a newer version of Visual Studio which does code review, and gave me a suggestion of IDispose
 
That is the exception that I get when it is thrown.
No, it isn't. That is the error message. The exception contains much more information than just the error message.
...the only COM components that I know that I'm using the OLEDB Connection.
I was aksing about components you are using directly. Lot's of managed code uses COM under the hood but if you're using something directly then that's a different kettle of fish.
 
Back
Top Bottom