SqlConnection conn = new SqlConnection(AllQuestionsPresented.connectionString); SqlCommand cmd = new SqlCommand(sb.ToString(), conn); cmd.Parameters.Add("@ThreadsID", SqlDbType.Int).Value = commentIDe; cmd.Parameters.Add("@CommentsID", SqlDbType.Int).Value = commentIDe; try { conn.Open(); SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); if (dr != null && dr["Comments"] != null && dr["Name"] != null && dr["Date"] != null && dr["UserID"]!=null)//> Invalid attempt to read when no data is present. { Comment = dr["Comments"].ToString(); UserName = dr["Name"].ToString(); Reputation=Int32.Parse(dr["Reputation"].ToString()); Time = (DateTime)AllQuestionsPresented.TryParse(dr["Date"].ToString()); UserID = (Guid)dr["UserID"]; } dr.Close(); } finally { if (conn.State != ConnectionState.Closed) { conn.Close(); } }
Note. I repeat this piece of code (dr.Read) {} ... it is not shown here.
Why am I getting this exception and how can I get rid of it?
UPDATE:
while (reader.Read())//Command runs through all the ThreadIDs that i have! { Comments allQ = new Comments((int)reader["CommentsID"]); allComments.Add(allQ); }
Comments is the class in which the code is located, and it has a method inside the constructor that runs the code that I presented.
Maybe if the loop runs too many times .. Then the exception is thrown, am I right?
source share