0
In this Try-catch occurs the following error message:
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 objectdbConnection
, 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.– Pedro Gaspar
Edited issue.
– Thales