1

I'm currently making a stock tracker with C# and Access, I'm currently facing an issue that's been causing me serious grief for the past week.

Without warning, while testing the barcoding system, my code began to throw this error when I attempt to create a new database entity

Exception thrown: 'System.Runtime.InteropServices.InvalidComObjectException' in System.Data.dll
System.Runtime.InteropServices.InvalidComObjectException: COM object that has been separated from its underlying RCW cannot be used.
   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.InitializeCommand(CommandBehavior behavior, Boolean throwifnotsupported)
   at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult)
   at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method)
   at System.Data.OleDb.OleDbCommand.ExecuteNonQuery()
   at Dashboard.MainForm.SubmitNewItemButton_Click(Object sender, EventArgs e) in C:\Users\vitor\source\repos\Dashboard\Dashboard\Form1.cs:line 357
The program '[11752] Dashboard.exe' has exited with code 0 (0x0).

This is the code that Is giving me pain

Access_DB.sql = "insert into SupplyStock (ItemName,UID,Stock,InternalRef,AddedBy,DateCreated,Barcode) values(@it,@uid,@stk,@ir,@adb,@dc,@bc);";
                Access_DB.cmd.Parameters.Clear();
                Access_DB.cmd.CommandText = Access_DB.sql;
                Uniq = GetUniqueID(UniqueIDInput.Text, 0);

                Access_DB.cmd.Parameters.Add("@it", System.Data.OleDb.OleDbType.VarChar).Value = ItemNameInput.Text;
                Access_DB.cmd.Parameters.Add("@uid", System.Data.OleDb.OleDbType.VarChar).Value = UniqueIDInput.Text;
                Access_DB.cmd.Parameters.Add("@stk", System.Data.OleDb.OleDbType.VarChar).Value = StockNumber.Value;
                Access_DB.cmd.Parameters.Add("@ir", System.Data.OleDb.OleDbType.VarChar).Value = Uniq;
                Access_DB.cmd.Parameters.Add("@adb", System.Data.OleDb.OleDbType.VarChar).Value = Username;
                Access_DB.cmd.Parameters.Add("@dc", System.Data.OleDb.OleDbType.Date).Value = DateTime.Now;

                var MyBarCode = IronBarCode.BarcodeWriter.CreateBarcode(Uniq, BarcodeEncoding.Code128);
                Access_DB.cmd.Parameters.Add("@bc", System.Data.OleDb.OleDbType.VarChar).Value ="./Barcodes/ BC_" + Uniq + ".png";
                MyBarCode.SaveAsImage("./Barcodes/BC_" + Uniq + ".png");
                
                try
                { 
                    Access_DB.openConnection();
                    Access_DB.cmd.ExecuteNonQuery();
                    MessageBox.Show("New Item Added", "New Item Success", MessageBoxButtons.OK);
                    Access_DB.closeConnection();
                }
                catch (Exception er)
                {
                    MessageBox.Show("Error loading information onto database |" + er, "Load Error", MessageBoxButtons.OK);
                    Console.WriteLine(er);
                }

line 357 is

Access_DB.cmd.ExecuteNonQuery();

which is where the error flags up.

Any help would be much appreciated.

Bimbles.04
  • 11
  • 3
  • 1
    Does this answer your question? [COM object that has been separated from its underlying RCW cannot be used](https://stackoverflow.com/questions/1567017/com-object-that-has-been-separated-from-its-underlying-rcw-cannot-be-used) – Mark Benningfield Sep 27 '21 at 11:53
  • 1
    You're not closing the connection when an exception is thrown. You should really be wrapping connection and command objects in a `using` block to ensure that they're always properly disposed of. – Richard Deeming Sep 27 '21 at 14:54

0 Answers0