Here is my code, I close and open the reader, and it still does not work. Several threads can access this function at the same time, but there is a lock. It works several times in the beginning, but sooner or later I get the exception "Invalid attempt to cause Reading when the reader is closed" in
private IList<BursaUser> GetUsers(SqlCommand cmd) { IList<User> users = new List<User>(); User user; lock (thisLock) { SqlDataReader dr = null; try { Conn.Open(); dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); while (dr.Read()) { user = new User { UserId = Convert.ToInt32(dr["WorkerNum"]), CompanyName = dr["CompanyName"].ToString(), WorkerName = dr["WorkerFirstName"] + " " + dr["WorkerFamilyName"], Phone = dr["Phone"].ToString() }; if (dr["QueueNum"] != null && dr["QueueNum"] != DBNull.Value) { user.Queue = new Queue { HasAlreadyEntered = dr["flgAppear"] != null && dr["flgAppear"].ToString() == "Y", IsFromWebsite = dr["TookFrom"].ToString() == "1", IsMelutash = dr["IsMelutash"].ToString() == "1", TimeOrdered = DateTime.Parse(dr["DateTime1"].ToString()), QueueNum = Convert.ToInt32(dr["QueueNum"]), SMS = dr["SMSCode"].ToString() }; } users.Add(user); } } catch (Exception e) { throw e; } finally { if (dr != null) { dr.Close(); dr.Dispose(); } } return users; } }
What gives?
Eitan
source share