Separate COM object from adjacent RCW

Asked

Viewed 192 times

0

In this Try-catch occurs the following error message:

inserir a descrição da imagem aqui

The code:

        try
        {
            dbConnection.Open();

            cmdQry.CommandText = "SELECT * FROM tbl_ItensEmbaOrdem WHERE idOrdem=?";

            cmdQry.Parameters.Clear();
            cmdQry.Parameters.Add(new OleDbParameter("@id", Convert.ToInt32(cbCodigo.Text)));

            OleDbDataReader RD = cmdQry.ExecuteReader();
            while(RD.Read())
            {
                ListViewItem item = new ListViewItem(RD["nomeEmba"].ToString());
                item.SubItems.Add(RD["unEmba"].ToString());
                item.SubItems.Add(RD["qtEmba"].ToString());
                item.SubItems.Add(RD["loteEmba"].ToString());

                lvEmba.Items.Add(item);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message + "\nCódigo PRO05X137", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        finally
        {
            dbConnection.Close();
        }

This mistake has already happened to me, but I do not remember how I solved, besides I researched about this object COM and its RCW and I found nothing. I would like to know the cause of the error and what are these two objects. Grateful.

P.S: error occurs only at the point OleDbDataReader RD = cmdQry.ExecuteReader(); and ONLY at this point of the code.

EDIT

The complete code:

private void cbCodigo_SelectedIndexChanged(object sender, EventArgs e)
    {
        string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
               + Application.StartupPath + @"\dbProjetoLagune1.mdb";

        OleDbConnection dbConnection = new OleDbConnection(strConnection);

        OleDbCommand cmdQry = dbConnection.CreateCommand();

CODE NOT NEEDED HERE IN THE MIDDLE

        try
        {
            dbConnection.Open();

            cmdQry.CommandText = "SELECT * FROM tbl_ItensEmbaOrdem WHERE idOrdem=?";

            cmdQry.Parameters.Clear();
            cmdQry.Parameters.Add(new OleDbParameter("@id", Convert.ToInt32(cbCodigo.Text)));

            OleDbDataReader RD = cmdQry.ExecuteReader();
            while(RD.Read())
            {
                ListViewItem item = new ListViewItem(RD["nomeEmba"].ToString());
                item.SubItems.Add(RD["unEmba"].ToString());
                item.SubItems.Add(RD["qtEmba"].ToString());
                item.SubItems.Add(RD["loteEmba"].ToString());

                lvEmba.Items.Add(item);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message + "\nCódigo PRO05X137", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        finally
        {
            dbConnection.Close();
        }
    }

End of the code where the error is.

  • Where and how the object cmdQry is created? You apparently opened the connection to the base using the object dbConnection, but at no time is passing this connection to the query object, cmdQry. I think some parts of the code are missing so I can better understand what happens.

  • Edited issue.

2 answers

1

You created and opened your connection, but when creating the OleDbCommand you are not associating it to your open connection, that is, this command refers to what connection? I made a code snippet for you to see the change that should be made. I added using to ensure that the Dispose be called at the end of the use of resources.

using (OleDbConnection dbConnection = new OleDbConnection(connectionString))
{
    dbConnection.Open();

    using (OleDbCommand command = dbConnection.CreateCommand())
    {
        command.CommandText = "SELECT * FROM tbl_ItensEmbaOrdem WHERE idOrdem = @idOrdem";

        command.Parameters.AddRange(new OleDbParameter[]
        {
            new OleDbParameter("@idOrdem",  Convert.ToInt32(cbCodigo.Text))
        });

        OleDbDataReader reader = command.ExecuteReader();

        while (reader.Read())
        {
            ListViewItem item = new ListViewItem(RD["nomeEmba"].ToString());
            item.SubItems.Add(RD["unEmba"].ToString());
            item.SubItems.Add(RD["qtEmba"].ToString());
            item.SubItems.Add(RD["loteEmba"].ToString());

            lvEmba.Items.Add(item);
        }

        reader.Close();
    }
}
  • I edited the question where I show the association, please take a look.

  • What the Dispose() ago?

  • 1

    Dispose is used to release resources from your application, I will leave a link of an answer to that question: https://answall.com/questions/22284/devo-sempre-utilizar-dispose

1


I found the problem:

I simply needed to close the OleDbDataReader that was in another part of the code with the method Close().

I thank those who have invested time in the issue.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.