I need sub comments to be just below comments I tried in the below manner and it could able to retrieve only first main comment and all its sub comments but couldn’t load from second main comments. The logic I used is
MyConnection.Open();
OdbcCommand cmd = new OdbcCommand("Select * from maincomments", MyConnection);
OdbcDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
string abc1 = dr[0].ToString();
string abc2 = dr[1].ToString();
string abc3 = dr[2].ToString();
string abc4 = dr[3].ToString();
string abc5 = dr[4].ToString();
string abc6 = dr[5].ToString();
string abc7 = dr[6].ToString();
maincomm.Controls.Add(new LiteralControl(abc2 + "<br />" + abc5 + "<br />"));
cmd = new OdbcCommand("Select * from subcomments where acc_id=?", MyConnection);
cmd.Parameters.Add("@email", OdbcType.BigInt, 20).Value = abc1;
dr = cmd.ExecuteReader();
while (dr.Read())
{
string abcd1 = dr[0].ToString();
string abcd2 = dr[1].ToString();
string abcd3 = dr[2].ToString();
string abcd4 = dr[3].ToString();
string abcd5 = dr[4].ToString();
string abcd6 = dr[5].ToString();
string abcd7 = dr[6].ToString();
string abcd8 = dr[7].ToString();
maincomm.Controls.Add(new LiteralControl(abcd3 + "<br />" + abcd6 + "<br />"));
}
}
MyConnection.Close();
In the above code acc_id is serial number of maincomments.
What’s wrong in my code?
While you are using
drto get comments, in the inner loop you overwrite it with the DataReader for getting sub-comments.You should use different variables for the two (e.g.
drCommentsanddrSubComments)That’s what looks wrong from a functional point of view. My opinion is that there are still some more improvements to be done. After you fix the code I strongly suggest to post this code on CodeReview to get some good advices on how to refactor it.