Here is the code I made, it usually works, but sometimes fails (1 out of 4 times more or less):
...
List<string> _items = new List<string>();
...
using (SqlCeConnection con = new SqlCeConnection(Globals.conString))
{
string codbultocomp = null;
con.Open();
using (SqlCeCommand cmd = new SqlCeCommand("SELECT codbultocomp FROM envios WHERE codigodestino=@codigodestino AND estado=@pendiente", con))
{
cmd.Parameters.AddWithValue("@codigodestino", codigoDestino);
cmd.Parameters.AddWithValue("@pendiente", "pendiente");
SqlCeDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
codbultocomp = reader["codbultocomp"].ToString();
_items.Add(codbultocomp);
}
reader.Close();
}
listBox1.DataSource = _items;
}
When it fails the application freezes, and if I pause the debug it is stopped in the last brace. I tried to show an error using a try/catch block but it didn’t show anything and stopped in the same place. I also tried to watch the listbox datasource shows this error in the “watch” list:
Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.
Any idea of what I’m doing wrong?
Call it after your
using, all IDisposable objects will be disposed afterusing.