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 do iterate over that piece of code with while(dr.Read){}… it isnt shown here.
Why do i get that exception and how do 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 that the code is in, and it has a method inside the constructor that runs the code that i presented.
It could be that if the loop runs too many times.. Then the exception is being thrown am i right?
The following part of your code is illegal
You have to call the Read method once (and verify that it returns true) before accessing the indexer of the SqlDataReader, as the documentation states: