Search code examples
asp.netinnerhtml

Show all comments innerHtml Asp.net


I want write all comments from database but writed last comment..

My Code:

 public void ShowComments(int id)
    {
        using (con = new OracleConnection(ConfigurationManager.ConnectionStrings["connectionString"].ToString()))
        {
            using (cmd = new OracleCommand("select name,comments from comments where id=" + id + "", con))
            {
                con.Open();
                OracleDataReader dr = cmd.ExecuteReader();

                while (dr.Read())
                {

                    comments.InnerHtml = "<br/>" + dr["name"] + ":" + dr["comments"] + "<br/>";
                }

                con.Close();
            }
        }
    }

Write last comment , I want all comment write to < div >..

    comments.InnerHtml = "<br/>" + dr["name"] + ":" + dr["comments"] + "<br/>";

aspx:

<div id="comments" runat="server">

</div>

Solution

  • comments.InnerHtml = "<br/>" + dr["name"] + ":" + dr["comments"] + "<br/>";
    

    That overwrites the string stored in comments.InnerHtml for each row, thus only the last one shows up. Instead of overwriting that string, you need to append to it using the += operator.

    comments.InnerHtml += "<br/>" + dr["name"] + ":" + dr["comments"] + "<br/>";