I got this error and I don't know what I doing wrong. The code below is in the backrgoundworker.
Copy exception detail to the clipboard :
System.Runtime.InteropServices.InvalidComObjectException was unhandled by user code HResult=-2146233049 Message=COM object that has been separated from its underlying RCW cannot be used. Source=mscorlib
StackTrace: at System.StubHelpers.StubHelpers.StubRegisterRCW(Object pThis) at System.Data.Common.UnsafeNativeMethods.IAccessor.ReleaseAccessor(IntPtr hAccessor, Int32& pcRefCount) at System.Data.OleDb.RowBinding.Dispose() at System.Data.OleDb.OleDbCommand.ResetConnection() at System.Data.OleDb.OleDbCommand.Dispose(Boolean disposing) at System.ComponentModel.Component.Dispose() at AttendanceSystem.Student.frmNewStudent.bgwInsertStudent_DoWork(Object sender, DoWorkEventArgs e) in c:\Users\victorbaccaljr\Desktop\PROGRAM\Event Attendace System\AttendanceSystem\AttendanceSystem\Student\frmNewStudent.cs:line 138 at System.ComponentModel.BackgroundWorker.OnDoWork(DoWorkEventArgs e) at System.ComponentModel.BackgroundWorker.WorkerThreadStart(Object argument) InnerException:
code :
private void bgwInsertStudent_DoWork(object sender, DoWorkEventArgs e)
{
List<object> arg = (List<object>)e.Argument;
bool found = false;
using (OleDbConnection cnn = new OleDbConnection(ConfigurationManager.ConnectionStrings["db"].ConnectionString))
{
using (OleDbCommand cmd = new OleDbCommand())
{
cmd.CommandText = "select top 1 * from [student info] where id=@id";
cmd.Connection = cnn;
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@id", arg[0].ToString());
cnn.Open();
using (OleDbDataReader rdr = cmd.ExecuteReader())
{
rdr.Read();
if (rdr.HasRows)
{
found = true;
}
cnn.Close();
}
}
using(OleDbCommand cmd=new OleDbCommand())
{
if(found)
{
MessageBox.Show("Student ID already inserted.");
}
else
{
cmd.CommandText = "insert into [Student info] values(@id, @firstname, @lastname, @department, @address)";
cmd.Connection = cnn;
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@id", arg[0].ToString());
cmd.Parameters.AddWithValue("@firstname", arg[1].ToString());
cmd.Parameters.AddWithValue("@lastname", arg[2].ToString());
cmd.Parameters.AddWithValue("@department", arg[3].ToString());
cmd.Parameters.AddWithValue("@address", arg[4].ToString());
cnn.Open();
cmd.ExecuteNonQuery();
cnn.Close();
MessageBox.Show("Record inserted!");
this.DialogResult = DialogResult.OK;
}
}
}
}